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.
I quoted this image from here.
You can visualize CAN-Frame using a JavaScript library such as Epoch.
This is using the postman application.
ESP-IDF V4.4/V5.x.
ESP-IDF V5.1 is required when using ESP32C6.
-
SN65HVD23x CAN-BUS Transceiver
SN65HVD23x series has 230/231/232.
They differ in standby/sleep mode functionality.
Other features are the same. -
Termination resistance
I used 150 ohms.
SN65HVD23x | ESP32 | ESP32-S2/S3 | ESP32-C3/C6 | ||
---|---|---|---|---|---|
D(CTX) | -- | GPIO21 | GPIO17 | GPIO0 | (*1) |
GND | -- | GND | GND | GND | |
Vcc | -- | 3.3V | 3.3V | 3.3V | |
R(CRX) | -- | GPIO22 | GPIO18 | GPIO1 | (*1) |
Vref | -- | N/C | N/C | N/C | |
CANL | -- | To CAN Bus | |||
CANH | -- | To CAN Bus | |||
RS | -- | GND | GND | GND | (*2) |
(*1) You can change using menuconfig. But it may not work with other GPIOs.
(*2) N/C for SN65HVD232
+-----------+ +-----------+ +-----------+
| 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.
git clone https://github.com/nopnop2002/esp-idf-can2mqtt
cd esp-idf-can2mqtt
chmod 777 getpem.sh
./getpem.sh
idf.py set-target {esp32/esp32s2/esp32s3/esp32c3/esp32c6}
idf.py menuconfig
idf.py flash
MQTT broker is specified by one of the following.
- IP address
192.168.10.20
- mDNS host name
mqtt-broker.local
- Fully Qualified Domain Name
broker.emqx.io
You can download the MQTT broker from here.
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.
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.
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".
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.
mosquitto_sub -h broker.emqx.io -p 1883 -t '/can/#' -F %X -d
1011121314151617 indicates 8 bytes of CAN-BUS data at 0x11-0x12-0x13-0x14-0x15-0x16-017.
- Send standard frame data with CANID = 0x201.
echo -ne "\x01\x02\x03" | mosquitto_pub -h broker.emqx.io -p 1883 -t '/can/std/201' -s
- Send extended frame data with CANID = 0x201.
echo -ne "\x11\x12\x13" | mosquitto_pub -h broker.emqx.io -p 1883 -t '/can/ext/201' -s
python3 -m pip install -U paho-mqtt
python3 mqtt_sub.py
Example code in various languages.
https://github.com/emqx/MQTT-Client-Examples
There is a lot of information on the internet about the Python + visualization library.
There is a lot of information on the internet about the node.js + real time visualization library.
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.
There is a module of SN65HVD230 like this.
There is a 120 ohms terminating resistor on the left side.
I have removed the terminating resistor.
And I used a external resistance of 150 ohms.
A transmission fail is fixed.
If the transmission fails, these are the possible causes.
- There is no receiving app on CanBus.
- The speed does not match the receiver.
- There is no terminating resistor on the CanBus.
- There are three terminating resistors on the CanBus.
- The resistance value of the terminating resistor is incorrect.
- Stub length in CAN bus is too long. See here.
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