Blog
Recent Posts
SAE J1939 Protocol Stack Sketch for ESP32 Using the Arduino IDE
Posted by
onThe 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.
Furthermore, the processor provides the means to easily and quickly create applications like CAN/J1939 gateways, CAN Bridges, J1939 ECUs, J1939 Data Logger, and many more. However, the Controller Area Network (CAN) port on the ESP32 provides only TTL levels, and you will need additional hardware (transceivers) to make them hardware-compliant with the CAN Bus standard ISO-11898.
For that reason, I am using our ESP32 WiFi, Bluetooth Classic, BLE, CAN Bus Module. This board comes not only with an onboard ESP32 WROOM-32 WiFi, Bluetooth Classic, BLE Module, but also a CAN Bus port with a transceiver.
CAN Bus Driver Software
Naturally, in order to implement an SAE J1939 protocol stack, you need to have a good CAN Bus driver software. I chose the ESP32 Protocol Demo on giothub.com. However, I made some minor modifications for better usability under SAE J1939, most prominently the implementation of CAN Bus error detection. The modifications are:
- Added CANError to:
- CAN.c
- CAN.h
- ESP32CAN.cpp
- ESP32CAN.h
- CAN.c:
- Renamed to CAN.cpp
- CAN.cpp -> Corrected declaration of CAN_read_frame_phy
- CAN.cpp -> CAN_isr -> Added cast to interrupt = MODULE_CAN->IR.U;
ARD1939 - SAE J1939 Protocol Stack for Arduino
The ARD1939 SAE J1939 protocol stack for Arduino was originally developed for the Arduino Uno and Arduino Mega 2560. In the meantime, I added versions for the Arduino Due and the ESP32.
All technical information about ARD1939, the SAE J1939 protocol stack for Arduino, plus detailed description of implementation and available function calls is included in my book SAE J1939 ECU Programming & Vehicle Bus Simulation with Arduino.
Arduino sketches (software projects) include a J1939 network scanner, and a simple SAE J1939 to USB Gateway application with associated Windows GUI (Visual Studio C# project). The collection of sketches is concluded by the ARD1939 project, a fully functional SAE J1939 protocol stack for the Arduino Uno and Mega 2560. And while the documentation refers to these processors, the protocol features apply to the ESP32 as well.
ARD1939 for ESP32
The ARD1939 code itself is directly compatible with the ESP32 when used with the Arduino IDE, however, the Arduino Due CAN Bus driver software required some modifications as discussed previously. Also, I needed to adjust the CAN Bus function calls to the same functionality as used for the Uno, Mega 2560, and Due version.
Please note: I had contemplated releasing ARD1939 in form of the original source code but ultimately decided against it, mostly out of respect for those small businesses that make a living from selling SAE J1939 devices and software tools. Instead, I provide a pre-compiled code.
Download the Software
Disclaimer: All Arduino sketches and other code samples and projects as introduced here are free software; you can redistribute and/or modify them. The programs are introduced in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. With downloading these programs, you confirm that these code samples and projects were created for demonstration and educational purpose only.
=> SAE J1939 Protocol Stack for ESP32 (.zip file)...
Last, but not least, sorry, but there is no technical support for this software. After all, it is free-of-charge.
Further Resources
- How to install ESP32 Board in Arduino IDE - Hackster.io...
- CAN Bus Development with ESP32-WROOM32 Development Board - Copperhill (copperhilltech.com)...
Electronics Projects with the ESP8266 and ESP32: Building Web Pages, Applications, and WiFi Enabled Devices
Copperhill Technologies highly recommends using this book for your ESP32 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 DC Motor PWM Speed Control (Pulse Width Modulation) - The Hidden Facts
Lately, I have worked on two projects requiring DC motor control per PWM (Pulse Width Modulation). What both projects had in common was a profound lack of documentation regarding PWM control, specifically the side effects that have a significant impact on the programming. This post aims to point to these quirks that may cause considerable headaches [...]
ESP32 Development Board with NMEA 2000 & NMEA 0183 HAT
The ESP32 processor, integrating peripherals such as UART, CAN Bus, WIFI, and Bluetooth, allows a wide range of applications, and the low price adds to its popularity. There are various ESP32 development boards available in the market, such as the ESP32-WROOM-32, as shown to the left. However, compared to popular systems like the Raspberry Pi or [...]
Extending ESP32 Functionality by Using Raspberry Pi HATs
The ESP32 processor, integrating peripherals such as UART, CAN Bus, WIFI, and Bluetooth, allows a wide range of applications, and the low price adds to its popularity. There are various ESP32 development boards available in the market, such as the ESP32-WROOM-32, as shown to the left. However, compared to popular systems like the Raspberry Pi or [...]
ESP32 Triple CAN Bus Application Through Adding Two MCP2515 Ports
The first question that may arise when talking about accessing the MCP2515 CAN Bus controller per ESP32 may be, "Why would you need an MCP2515 controller when the ESP32 comes with an internal CAN port?" Yes, I found this question in one of the online forums while researching this particular topic. The answer is easy: [...]
ESP32 Project: USB to Bluetooth Gateway
The ESP32 processor allows the integration of Wi-Fi, Bluetooth, and Bluetooth LE (BLE) for a wide range of IoT (Internet of Things) applications. Using Wi-Fi ensures connectivity within a large radius. At the same time, Bluetooth allows the user to easily detect (with low-energy beacons) a module and connect it to an Android/iOS smartphone or [...]
ESP32 Based CAN Bus, SAE J1939 and NMEA 2000 to Bluetooth Gateway
The following is a follow-up on our post Controller Area Network (CAN), SAE J1939, NMEA 2000 Wireless Module for IoT and ELD Applications. The CAN Bus, SAE J1939 and NMEA 2000 to Bluetooth Gateway is the first product of a new series of wireless gateways for IoT (Internet of Things), ELD (Electronic Logging Devices), and other applications. At the [...]
A New Generation of SAE J1939 Gateway, ECU Simulator, and Starter Kit
Like many other businesses, we have to deal with the global shortage of electronic components. For instance, the NXP LPC 17xx processors we used for our SAE J1939 gateways and the starter kit are not available at this time. Even worse, there is no reliable information if/when production resumes. This situation forced us to rethink [...]
ESP32 Starter and Development Kits for Building Bluetooth and WiFi Enabled IoT Devices
The integration of Wi-Fi, Bluetooth, and Bluetooth LE allow a wide range of applications with ESP32 modules. Using Wi-Fi ensures connectivity within a large radius, while using Bluetooth allows the user to easily detect (with low-energy beacons) a module and connect it to a smartphone. The chips add priceless functionality and versatility to your applications [...]
SAE J1939 Starter Kit and Network Simulator - New Hardware Variant Using the ESP32 Processor
Our SAE 1939 Starter Kit And Network Simulators allow the experienced engineer and the beginner to experiment with SAE J1939 data communication without connecting to a real-world J1939 network, i.e., a diesel engine. To establish a network, you need at least two nodes. That fact applies, especially to CAN/J1939 nodes. A single CAN Bus controller [...]