ESP32 Project: CAN Bus, SAE J1939, NMEA2000 Converter With IoT Functionality
It was just a few weeks ago that I received some bad news. Due to the worldwide shortage of electronic components, the NXP LPC1754 processor we use for our SAE J1939 ECU Simulator with USB Port will not be available for several months, maybe even well into 2022.
Consequently, it was time to rethink the design, and I had my eyes on the ESP32 processor for a while. I had enjoyed working with the LPC17xx series but also had to deal with memory restrictions; execution speed was never a problem, though. Of course, like many other processors, the ESP32 offers more memory plus increased speed, but the built-in IoT capabilities, such as Bluetooth/BLE and WiFi, made my decision easy.
The ESP32 is a series of low-cost, low-power system-on-chip microcontrollers with integrated Wi-Fi and dual-mode Bluetooth. The ESP32 series employs a Tensilica Xtensa LX6 microprocessor in both dual-core and single-core variations and includes built-in antenna switches, RF balun, power amplifier, low-noise receive amplifier, filters, and power management modules.
Add to all these precious hardware features that you can program the ESP32 using the Arduino IDE. I have used the Arduino platform whenever I needed a quick prototype. Hardware is readily available, and the coding is easy due to a vast software library. The only downside is debugging, which is painful, but I am counting on the new IDE version to take care of these shortcomings.
Initially, I had developed my SAE J1939 protocol stack using the Arduino Uno and Mega 2560, and the portation to the MCUXpresso IDE was easy. But, ironically, I have just finished the reverse process, i.e., getting the code to run under the Arduino IDE. The firmware for the SAE J1939 Simulator Board consisted not only of the SAE J1939 protocol stack but was extended by the serial communication to a host system, usually a PC. Sticking to good coding with carefully designed interface functions (serial port, CAN Bus interface, timer functions) helped make the transition effortless.
I documented the basic ESP32-CAN programming in another post, CAN Bus, SAE J1939, NMEA 2000 Programming With ESP32, Supporting USB, Bluetooth, BLE, And WiFi. It explains how to install the ESP32 module into the Arduino IDE and how to integrate the CAN Bus library. Another sample program, provided by SK Pang through github, demonstrates the use of the library but also shows how to implement interrupt-driven timer functionality for the ESP32. Timer functionality is important to serve the many timers as required by the SAE J1939 Standard.
However, while I consider the CAN Bus code one of the best available, it was missing one important feature, namely retrieving the CAN controller status, specifically the error counters. It may sound strange, but the SAE J1939 Standard breaks with the CAN Bus Standard (ISO 11898) by allowing message ID collisions during the node address negotiating process. In some rare cases, it is possible that two nodes with the same NAME claim the same node address at the very same time, which may lead to a collision. In this case, the J1939 ECU needs to detect a transmit error and start a random timeout before restarting address negotiation.
Nevertheless, the code demonstrated the access to the error counters during controller initialization, and it was easy to add a new function to check the TX error count.
ESP32 IDE Alternatives
As I mentioned previously, I have been using the MCUXpresso IDE for programming the NXP LPC17xx processors. It is based on on the Eclipse IDE, a programming environment that has been adapted for various software development tools, including the ESP-IDF for the ESP32. I have managed to install the ESP-IDF on my Windows 10 machine but haven't had a chance to play with it, especially after converting the code to the Arduino IDE went so smoothly.
I did notice, however, that a CAN Bus driver software was not part of the installation. Espressif, the maker of the ESP32 processors, has gone through some tremendous efforts of supporting multiple programming environments plus providing extensive documentation and sample codes. However, in view of such an overwhelming task, you must expect some shortcomings.
For instance, I attempted to install the ESP-IDF on my iMac, and it failed miserably. The same is true for VS Code, another Eclipse variant for the ESP32 provided free-of-charge by Microsoft. In all fairness, this may be entirely my own personal shortcoming, but the installation process is cumbersome and extremely time-consuming, and you spend most of the time inside the command line interface (which applies to iOS as well as Windows).
If you have the patience and appropriate knowledge to install VS Code with ESP32 extensions you will get a great development environment. In contrast, however, the Arduino IDE installation took only minutes, and didn't require any extraordinary tricks.
In all consequence, if the Arduino IDE does not meet your standards, I recommend going with the ESP-IDF for Windows. You can download the ESD-IDF Tools Installer and the installation process goes smoothly as any other Windows application. For more information, see Standard Setup of Toolchain for Windows.
ESP32 CAN Bus Board
Regarding the hardware design, I am also going new ways by not developing my own hardware. Instead, I am using the ESP32 WiFi, Bluetooth Classic, BLE, CAN-Bus Module by SK Pang Electronics (UK). We have been selling their product line of Raspberry Pi CAN Bus interfaces for several years. Their new ESP32 CAN Bus module implements all features required for an industrial-strength CAN Bus, SAE J1939, or NMEA2000 gateway.
The board comes with an onboard ESP32 WROOM-32 WiFi, Bluetooth Classic, BLE Module, and a CAN Bus port with a transceiver. Also, onboard is an RGB LED, IO pins on a 0.1" pad.
In addition to the onboard CAN-Bus port, it supports an extended temperature range of -40C to + 85C, and it accepts a wide range of power input, 4.5VDC to 46VDC. The power input range is vital for operating on diesel engines using an SAE J1939 or NMEA2000 network, where power fluctuation is typical.
- 240 MHz dual core Tensilica LX6 microcontroller with 600 DMIPS
- Integrated 512 KB SRAM
- Integrated 802.11b/g/n HT40 WiFi transceiver, baseband, stack and LWIP
- Integrated dual mode Bluetooth (classic and BLE)
- 4 MByte flash included in the WROOM32 module
- On-board PCB antenna
- Programming via USB-C connector
- CAN Bus transceiver
- SPI, IIC, I2S, UART ports accessible through onboard connections
- ADC, DAC channels accessible through onboard connections
- External power supply range of 4.5VDC to 46 VDC (nominal: 12 VDC) with reverse polarity protection
- Extended operating temperature range of -40C to +85C
As I mentioned previously, I have completed the implementation of an SAE J1939 to USB converter, which will lead to the introduction of a new product. Furthermore, I will add IoT functionality, i.e., activating the Bluetooth, BLE, and WiFi ports.
In addition, I will extend the SAE J1939 protocol to support NMEA2000, and I will implement an interface for Classical CAN and CAN FD. All these activities will result in new products, all based on the ESP32 processor.
The ESP32, a low-cost MCU with integrated Wi-Fi and BLE capabilities, comes with a variety of modules and development boards for building IoT applications efficiently. Wi-Fi and BLE are standard network stacks for Internet-of-Things applications providing cost-effective solutions for your business and project requirements.
This book is a fundamental guide for developing ESP32 programs and starts by explaining GPIO (General Purpose I/O) programming with sensor devices. The reader gets up to speed with ESP32 development through several IoT projects such as weather stations, sensor loggers, smart homes, Wi-Fi cams, and Wi-Fi wardriving. The reader learns how to use ESP32 boards to facilitate interactions between mobile applications and cloud servers, such as AWS.
By the end of this book, you'll have learned how to control a range of IoT projects using the ESP32 chip.