Site Information

 Loading... Please wait...

ESP32 Programming - Classical CAN to Bluetooth Gateway

Posted by Wilfried Voss on

In this post, I will present a CAN to Bluetooth gateway based on the ESP32 processor. The above image shows my test setup using our ESP32 WiFi, Bluetooth Classic, BLE, CAN Bus Module, a CAN-Bus Hub With 7 Ports And DC Power Connection, and the PCAN-USB Pro. As its description implies, the ESP32 module provides all necessary ports, i.e., a CAN Bus controller plus transceiver and a Bluetooth connection. Of course, the ESP32 also supports Bluetooth Low Energy (BLE) and WiFi, but in this post, I will focus on Bluetooth and address the others in a future post. 

I will not go too deep into the ESP32 capabilities, assuming that the reader is already familiar with the topic. Furthermore, there are multiple references and posts on this website. Just check out the information on our ESP32 product pages. However, I would like to mention that I am still excited about how easy it is to program wireless applications with the ESP32. My "bible" for Bluetooth and BLE is Electronics Projects with the ESP8266 and ESP32: Building Web Pages, Applications, and WiFi Enabled Devices as referenced on the bottom of this page. My Arduino Sketch for the Bluetooth gateway is based on the code in this book.

The Arduino Sketch

The code for the Bluetooth gateway is wrapped around the Bluetooth code as mentioned above plus a CAN driver by Thomas Barth, available on github.com. 

The code receives CAN data frames, converts them into an ASCII string and transmits the string per Bluetooth. There are also prints on the Serial Monitor. The format is as follows:

I chose ASCII code because it's easy to display the data on a terminal software, such as RealTerm. RealTerm also allows to send an ASCII string representing a CAN data frame to be transmitted as described in the next paragraph.

As usual, I will not go into detail of the code, but need to mention that it was designed for the ESP32 module as referenced above. Any modifications to work with other ESP32 boards should be easy to accomplish, though. The code represents a basic sample for a CAN to Bluetooth gateway. It may not be 100% fool-proof, and I did not include features such as baud rate setting (I use a default baud rate that can be changed within the code), filters, or more. However, the code gives the reader a great basis for further developments.

Click her to download the code (zip file).

Connecting the Bluetooth Device under Windows

Go to Settings->Bluetooth & devices -> Devices.

Click on "Add device."

Select Bluetooth.

You should see the ESP32-BT device. Click on it. As soon as the message "Your device is ready to go!" click "Done."

The device is still not connected. Now, we need to find out which port has been assigned to the Bluetooth device:

Scroll down the screen and look for "More Bluetooth Settings," and click on it.

The Bluetooth Settings appear:

Look for the "Outgoing" port, which in our example is COM8.

The easiest way to monitor the data traffic is to start a terminal software, such as RealTerm (as shown below):

Select Port 8 and click the "Change" command button. As displayed above, you can see the CAN data frames scrolling.

Transmitting CAN Data Frames per Bluetooth

Now it's time to send some data back to the network: Using the same terminal software, I designed two CAN data frames in the "Send" section as shown below.

The first data frame is in extended format, i.e., with 29-bit message identifier. The second frame is standard with an 11-bit message identifier. I sent the frames by clicking on the corresponding "Send ASCII" command button and verified the result with PCAN View:


Electronics Projects with the ESP8266 and ESP32: Building Web Pages, Applications, and WiFi Enabled DevicesElectronics Projects with the ESP8266 and ESP32: Building Web Pages, Applications, and WiFi Enabled Devices

Copperhill Technologies highly recommends using this book for your wireless application projects. Yes, many good books and free online resources are available these days, but this is the book we are using. It made our approach to Bluetooth, BLE, and WIFI a breeze. Programming wireless applications without hassles was fun, and we will share them on this web page.

Projects throughout the book utilize the wireless functionality and processing power of the ESP microcontrollers. Projects are built in the Arduino IDE, so you don't need to download other programming software. In addition, mobile apps are now ubiquitous, making the app build projects of the book very relevant, as are the web page design projects.

In Electronics Projects with the ESP8266 and ESP32, you'll see how easy and practical it is to access information over the internet, develop web pages, build mobile apps to remotely control devices with speech recognition, or incorporate Google Maps in a GPS route tracking app. More Information...