SAE J1939 Protocol Stack Sketch for ESP32 Using the Arduino IDE
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.
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:
- 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.
- 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...
The Two Elements of the SAE J1939 Protocol Stack
Any SAE J1939 hardware must support SAE J1939/1x and SAE J1939/21; otherwise, they’re useless because these standards describe the CAN bus physical layer and the basic protocol features. That part is sufficiently covered (i.e., for demonstration purposes) by our CAN shield, while the cabling and connectors may not necessarily J1939-compliant. A fully functional SAE J1939 protocol software [...]
Guide to SAE J1939 - Transport Protocol (TP) Message Packaging and Reassembly
The following is an excerpt from A Comprehensible Guide To J1939 by Wilfried Voss. Certain parameter groups may require more than the eight data bytes supported by the CAN standard. The SAE J1939 standard, namely the Transport Protocol Function, supports message lengths up to 1785 bytes. If a program group requires more than eight data bytes (9…1785 bytes) and [...]
SAE J1939 Transport Protocol (TP) Functions
The following is an excerpt from A Comprehensible Guide To J1939 by Wilfried Voss. Even though extremely useful in passenger cars and small industrial applications, CAN alone was not suitable to meet the requirements of truck and bus communications, mainly since its communication between devices is limited to only 8 bytes per message. However, it is possible [...]