Skip to content

nopnop2002/esp-idf-can2mqtt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

98 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 V5.0 or later.
ESP-IDF V4.4 release branch reached EOL in July 2024.

Hardware requirements

  • 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.

Wireing

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

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.

  • 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.

This project supports TCP,SSL/TLS,WebSocket and WebSocket Secure Port.

  • Using TCP Port.
    config-mqtt-1

  • Using SSL/TLS Port.
    SSL/TLS Port uses the MQTTS protocol instead of the MQTT protocol.
    config-mqtt-2

  • Using WebSocket Port.
    WebSocket Port uses the WS protocol instead of the MQTT protocol.
    config-mqtt-3

  • Using WebSocket Secure Port.
    WebSocket Secure Port uses the WSS protocol instead of the MQTT protocol.
    config-mqtt-4

Note for using secure port.
The default MQTT server is broker.emqx.io.
If you use a different server, you will need to modify getpem.sh to run.

chmod 777 getpem.sh
./getpem.sh

WebSocket/WebSocket Secure Port may differ depending on the broker used.
If you use a different server, you will need to change the port number from the default.

Note for using MQTTS/WS/WSS transport.
If you use MQTTS/WS/WSS transport, you can still publish and subscribe using MQTT transport.

+----------+                   +----------+           +----------+
|          |                   |          |           |          |
|  ESP32   | ---MQTTS/WS/WSS-->|  Broker  | ---MQTT-->|Subsctiber|
|          |                   |          |           |          |
+----------+                   +----------+           +----------+

+----------+                   +----------+           +----------+
|          |                   |          |           |          |
|  ESP32   | <--MQTTS/WS/WSS---|  Broker  | <--MQTT---|Publisher |
|          |                   |          |           |          |
+----------+                   +----------+           +----------+

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-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

  • 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

Receive CANbus using UNO.
can2mqtt-12

  • 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

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.

Using mqttx

MQTTX is an open source cross-platform MQTT 5.0 desktop client tool.
Can be used with macOS, Linux, and Windows.
mqttx

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.

  • 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.

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