Home

Awesome

esp-idf-can2mqtt

CANbus to mqtt bridge using esp32.
It's purpose is to be a bridge between a CAN-Bus and a MQTT-Broker.
You can map CAN-ID to MQTT-Topics and map each payload to a message.
I inspired from here.

can2mqtt

I quoted this image from here.

You can visualize CAN-Frame using a JavaScript library such as Epoch.

slide0001

This is using the postman application.
postman

Software requirement

ESP-IDF V4.4/V5.x.
ESP-IDF V5.1 is required when using ESP32C6.

Hardware requirements

Wireing

SN65HVD23xESP32ESP32-S2/S3ESP32-C3/C6
D(CTX)--GPIO21GPIO17GPIO0(*1)
GND--GNDGNDGND
Vcc--3.3V3.3V3.3V
R(CRX)--GPIO22GPIO18GPIO1(*1)
Vref--N/CN/CN/C
CANL--To CAN Bus
CANH--To CAN Bus
RS--GNDGNDGND(*2)

(*1) You can change using menuconfig. But it may not work with other GPIOs.

(*2) N/C for SN65HVD232

Test Circuit

   +-----------+   +-----------+   +-----------+ 
   | Atmega328 |   | Atmega328 |   |   ESP32   | 
   |           |   |           |   |           | 
   | Transmit  |   | Receive   |   | 21    22  | 
   +-----------+   +-----------+   +-----------+ 
     |       |      |        |       |       |   
   +-----------+   +-----------+     |       |   
   |           |   |           |     |       |   
   |  MCP2515  |   |  MCP2515  |     |       |   
   |           |   |           |     |       |   
   +-----------+   +-----------+     |       |   
     |      |        |      |        |       |   
   +-----------+   +-----------+   +-----------+ 
   |           |   |           |   | D       R | 
   |  MCP2551  |   |  MCP2551  |   |   VP230   | 
   | H      L  |   | H      L  |   | H       L | 
   +-----------+   +-----------+   +-----------+ 
     |       |       |       |       |       |   
     +--^^^--+       |       |       +--^^^--+
     |   R1  |       |       |       |   R2  |   
 |---+-------|-------+-------|-------+-------|---| BackBorn H
             |               |               |
             |               |               |
             |               |               |
 |-----------+---------------+---------------+---| BackBorn L

      +--^^^--+:Terminaror register
      R1:120 ohms
      R2:150 ohms(Not working at 120 ohms)

NOTE
3V CAN Trasnceviers like VP230 are fully interoperable with 5V CAN trasnceviers like MCP2551.
Check here.

Installation

git clone https://github.com/nopnop2002/esp-idf-can2mqtt
cd esp-idf-can2mqtt
idf.py set-target {esp32/esp32s2/esp32s3/esp32c3/esp32c6}
idf.py menuconfig
idf.py flash

Configuration

config-main config-app

CAN Setting

config-can

WiFi Setting

config-wifi

MQTT Server Setting

MQTT broker is specified by one of the following.

You can download the MQTT broker from here.

config-mqtt-1

Specifies the username and password if the server requires a password when connecting.
Here's how to install and secure the Mosquitto MQTT messaging broker on Debian 10.

config-mqtt-2

You can use a secure MQTT server.
A secure MQTT server uses the MQTTS protocol instead of the MQTT protocol.
The default secure MQTT server is mqtt.eclipseprojects.io.
If you use a different server, you will need to modify getpem.sh to run.

chmod 777 getpem.sh
./getpem.sh

config-mqtt-11

Definition from CANbus to MQTT

When CANbus data is received, it is sent by MQTT according to csv/can2mqtt.csv.
The file can2mqtt.csv has three columns.
In the first column you need to specify the CAN Frame type.
The CAN frame type is either S(Standard frame) or E(Extended frame).
In the second column you have to specify the CAN-ID as a hexdecimal number.
In the last column you have to specify the MQTT-Topic.
Each CAN-ID and each MQTT-Topic is allowed to appear only once in the whole file.

S,101,/can/std/101
E,101,/can/ext/101
S,103,/can/std/103
E,103,/can/ext/103

When a Standard CAN frame with ID 0x101 is received, it is sent by TOPIC of "/can/std/101".
When a Extended CAN frame with ID 0x101 is received, it is sent by TOPIC of "/can/ext/101".

Definition from MQTT to CANbus

When MQTT data is received, it is sent by CANbus according to csv/mqtt2can.csv.
Same format as can2mqtt.csv.

S,201,/can/std/201
E,201,/can/ext/201
S,203,/can/std/203
E,203,/can/ext/203

When receiving the TOPIC of "/can/std/201", send the Standard CAN frame with ID 0x201.
When receiving the TOPIC of "/can/ext/201", send the Extended CAN frame with ID 0x201.

Receive MQTT data using mosquitto_sub

mosquitto_sub -h broker.emqx.io -p 1883 -t '/can/#' -F %X -d

1011121314151617 indicates 8 bytes of CAN-BUS data at 0x10-0x11-0x12-0x13-0x14-0x15-0x16-017.

can2mqtt-1

Transmit MQTT data using mosquitto_pub

Receive CANbus using UNO.
can2mqtt-12

Receive CANbus using UNO.
can2mqtt-14

Receive MQTT data using python

python3 -m pip install -U paho-mqtt
python3 mqtt_sub.py

python-screen

MQTT client Example

Example code in various languages.
https://github.com/emqx/MQTT-Client-Examples

Visualize CAN-Frame

Using python

There is a lot of information on the internet about the Python + visualization library.

Using node.js

There is a lot of information on the internet about the node.js + real time visualization library.

Using postman application

postman-1 postman-2

Postman works as a native app on all major operating systems including Linux (32-bit/64-bit), macOS, and Windows (32-bit/64-bit).
Postman supports MQTT visualization.
You do not need to create a application for visualization.
Here's how to get started with MQTT with Postman.

Troubleshooting

There is a module of SN65HVD230 like this.
SN65HVD230-1

There is a 120 ohms terminating resistor on the left side.
SN65HVD230-22

I have removed the terminating resistor.
And I used a external resistance of 150 ohms.
A transmission fail is fixed.
SN65HVD230-33

If the transmission fails, these are the possible causes.

Reference

https://github.com/nopnop2002/esp-idf-candump

https://github.com/nopnop2002/esp-idf-can2http

https://github.com/nopnop2002/esp-idf-can2usb

https://github.com/nopnop2002/esp-idf-can2websocket

https://github.com/nopnop2002/esp-idf-can2socket

https://github.com/nopnop2002/esp-idf-CANBus-Monitor

https://github.com/nopnop2002/esp-idf-mqtt-client