Site Information

 Loading... Please wait...

Blog

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

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 »


Arduino-Compatible Microsoft Azure Certified IoT Development Kit with Visual Studio Code Support

This Microsoft Azure Certified IoT DevKit (IOT-AZ3166) is an all-in-one kit optimized for prototyping and developing Internet of Things (IoT) applications leveraging Microsoft Azure services. It supplies an Arduino-compatible board with multiple peripherals such as an OLED display, sensors, hardware debugging chip (ST-Link), and security chip. The project collection offers creative examples for learning and reference, plus [...]

Read More »


Arduino-Due-Based SAE J1939 Programming Kit - Quick Start Reference

Unleash the power of the Arduino Due with ARM Cortex-M3 processor to develop and test your SAE J1939 application, may it be an SAE J1939 to USB protocol converter, an SAE J1939 Bridge, an SAE J1939 data monitor, and many more.  The SAE J1939 Programming Kit comes with many programming samples, including a full-blown SAE J1939 [...]

Read More »


A Beginner's Guide to SAE J1939 Embedded Software Development

Introduction Welcome to my beginner's guide! By opening this page, you have entered the first and probably most crucial stage toward developing your SAE J1939 project: Reading. Over the years, I dealt with many newcomers to the J1939 technology, some of them motivated by great product ideas. Others were thrown into a project because they were [...]

Read More »


Arduino Compatible Industrial Controller Supports I/O Expansion Modules

The open-source ProductivityOpen platform presents all the features of a conventional Arduino plus an industrial controller. The processor circuit of the P1AM-100 Arduino-compatible CPU simulates the Arduino MKRZero microcontroller. The P1AM-100 is compatible with multiple available Arduino MKR format shields, plus the ProductivityOpen shields, and can employ most Arduino sketch programs found on open-source websites. Using the [...]

Read More »


Practical Guides To Mastering Embedded Linux Programming

Mastering Embedded Linux Programming: Unleash the full potential of Embedded Linux with Linux 4.9 and Yocto ProjectThis complete guide shows the technologies and techniques needed to incorporate Linux into embedded systems. The reader will begin by learning about the essential elements that underpin all embedded Linux projects: the toolchain, the bootloader, the kernel, and the [...]

Read More »


Raspberry Pi 4 Step-By-Step Guides To Innovative Linux Projects

RASPBERRY PI 4 COMPLETE MANUAL: A Step-by-Step Guide to the New Raspberry Pi 4 and Set Up Innovative ProjectsThe Raspberry Pi 4 can accomplish a remarkable variety of embedded applications. Amateur technology enthusiasts use Raspberry Pi boards as media centers, file servers, retro games consoles, routers, and network-level ad-blockers, for starters. However, that is just [...]

Read More »


CANopen Library For Linux Based Application Development

The CANopen Magic Pro Library by Embedded Systems Academy is a system supporting customized CANopen based PC applications for quick and easy development without the need to know all details of the CANopen protocol stack. The kit consists of a professional CANopen library along with documentation, a C header file and makefile for gcc. The CANopen library achieves [...]

Read More »


Programming The Atmega328 And The Arduino Uno Board Using The Atmel Studio IDE

The AVR microcontroller by Atmel (now Microchip) is one of the most widely used 8-bit microcontrollers. The popular Arduino Uno board utilizes an AVR microcontroller. It is inexpensive and widely available around the world.This book combines this combination. The authors apply a step-by-step and methodical approach to demonstrate the programming of the AVR chip. Examples are presented [...]

Read More »