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 

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...

ESP32: CAN Bus Programming with MCP2515 and MCP2517FD

For good reasons, the ESP32 processor is a prevalent choice for embedded hardware development. Besides considerable memory resources, it provides various hardware features for many applications, most prominently the Internet of Things (IoT). All that comes with more than reasonable price tags, specifically when you use one of the multiple ESP32 development modules. And since [...]

Read More »

Discover IoT Development with ESP32 to Create Production-Grade Smart Devices

The ESP32 is a low-cost, low-power SoC (System on Chip) microcontroller with integrated WiFi and dual-mode Bluetooth. It has shipped over 100 million units as of 2018 and powers many WiFi devices on the market. This book supplies end-to-end coverage of building an IoT system from the ground up to secure data communication techniques from [...]

Read More »

ESP32 Application: Galvanically Isolated CAN Bus Repeater and Baud Rate Converter

With this application, I am adding yet another example to the vast CAN library we developed over the years. The use of the Dual Isolated CAN Bus HAT with the ESP32 opens the door to a number of possible applications. The development of a CAN repeater and/or Baud Rate Converter came to mind first, because the [...]

Read More »

ESP32 Serial Ports - UART0, UART1, UART2 Access Using the Arduino IDE

In the following, I am using the ESP32 WROOM Devkit for this tutorial in combination with the Arduino IDE. For more information on programming the ESP32 using the Arduino IDE, please refer to “Getting started with ESP32 development board and Arduino.” The Arduino IDE uses the Serial class (assigned to UART0) to access the USB port [...]

Read More »

Arduino IDE Boards Manager - Do NOT Install ESP32 by Espressif Systems Version 3.00-alpha1

Today, I experienced a little scare. As usual, I opened the Arduino IDE on my Windows-11 machine, and as a first action, I allowed the recommended updates. Not suspecting any significant problems, I added some code and compiled it, only to end up with multiple error messages.  I found nothing wrong with the code I had [...]

Read More »

Dual-Channel Linear Actuator Control Module with ESP32 Processor

As part of a customer project, I, with the help of a capable hardware designer, developed an embedded system to control two linear actuators, specifically the Thomson Electrac HD model with SAE J1939 interface.  The Electrac series of actuators supports two higher-layer protocols (HLP) based on Controller Area Network (CAN): CANopen, and SAE J1939. CANopen is [...]

Read More »

ESP32, ESP32-S2 - Serial Port, Native USB Access using Arduino IDE

The test setup, as shown in the image, represents the hardware of a new project that requires reading CAN (Controller Area Network) data frames, combining them with real-time information plus GPS position, and storing the result onto an SD card. This post will focus on the ESP32 communicating with the GPS module delivering NMEA 0183 [...]

Read More »

ESP32 with Dual Isolated CAN Port Controls Thomson Electrac Linear Actuator

The above image shows part of a hardware setup for a customer project to control and synchronize two Thomson Electrac linear actuators. See also my posts:Thomson Electrak Linear Actuator with SAE J1939 Interface for Railway and Industrial Applications Thomson... Electrak HD Linear Actuator - Multiple Actuators in the Same Network Will Empty the Batteries... Thomson Electrac HD [...]

Read More »

espBerry Project: ESP32 with CH9102F USB-UART Chip for Serial Speed up to 3Mbit/s

I have a project that involves the ESP32-DevKitC development board where the application requires the fast transport of data per USB. The onboard USB-UART bridge chip provides transfer rates of up to 3 Mbps. In addition, the ESP32 has three UART interfaces, i.e., UART0, UART1, and UART2, which provide asynchronous communication, communicating at a speed of [...]

Read More »