TCP/IP tunnel for CAN bus data
Supports UDP and TCP communication, use of UDP is recommended
Sopports standard CAN frames only
Based on project cannelloni: https://github.com/mguentner/cannelloni
This is beta stage software. Don't use in productive environment.
Project cannelloni provides bidirectional transfer of CAN frames between two (linux) machines using IP protocols UDP, TCP or SCTP.
This projects runs one cannelloni node on an ESP32 controller. A typical setup can look like this:
{physical can bus} <-> {can tranceiver} <-> {ESP32} <-> WiFi <-> {linux machine} <-> {virtual can bus} <-> {application}
For linux implementation please refer to documentaion of project cannelloni. Another very good description is available here.
Beside the linux machine, e.g. a raspberry pi, you need
- ESP32 dev kit, e.g. this
- CAN transceiver, e.g. this or this
- Connect ESP32 to CAN transceiver: 3.3V, GND, GPIO21 <-> TX, GPIO22 <-> RX. If you use different GPIOs, pls. adapt assignments in
user_config.h
accordingly. - Connect CAN_L and CAN_H of transceiver to the physical CAN bus. It's good idea to use twisted pair cable. Conncetion of GND is optional. Check, if a termination is needed (120 Ohm).
To build and flash VSCode with PlatformIO or Arduino IDE can be used.
- This project is using VScode
- If you prefer to use Arduino IDE please create a new project, copy content of file
main.cpp
to your sketch and copy fileuser_config.h
to the same folder.
- Install CAN library by Sandeep Mistry
- Edit
user_config.h
- decide to use UDP (default) or TCP protocol (undefine
UDP_PROTOCOL
by commenting out the coressesponing line) for data exchange with cannelloni host. - set ssid and password four your WiFi
- adapt host address and port of cannelloni server on linux side
- check pin assignments for connection to can transceiver
- adapt CAN baudrate
- limit range of CAN IDs to what you really need (speed and resources of a ESP32 are limited!).
- decide to use UDP (default) or TCP protocol (undefine
- Build the project and flash it to the ESP32. In the web interface an option for OTA update is available. It's also possible to trigger a restart of ESP32.
- Start virtual CAN bus and cannelloni server on you linux host. Please refer to cannelloni project for details.
- When using UDP protocol:
- Power on the ESP32
- Start cannelloni as suggested in cannelloni docs, e.g. for vcan0, ESP32 running on 192.168.178.43, port 2000 & 2001:
cannelloni -I vcan0 -R 192.168.178.43 -r 2001 -l 2000
- The correct command line will be shown on web interface after start up of ESP32
- When using TCP protocol:
- Start cannelloni as server for TCP protocol, e.g. for vcan0 and port 2001:
cannelloni -I vcan0 -C s -p -t 20000 -l 2001
- cannelloni now will wait for connection of a client
- Power on the ESP32. After a couple of seconds it should connect to the cannelloni server
- Start cannelloni as server for TCP protocol, e.g. for vcan0 and port 2001:
- ESP32cannelloni provides a simple web interface reachable under
http://ESP32cannelloni/
orhttp://<IP-address-of-ESP32>/
To do an update via web interface you need the binary of the code.
- When using VScode the file is availabe after a successful build:
<PATH_TO_YOUR_PROJECT>/.pio/build/<YOUR_ENV>/firmware.bin
. - When using Arduino IDE choose menu
Sketch->Export Compiled Binary
. After a successful build the file is located at<PATH_TO_YOUR_PROJECT>/build/<YOUR_ENV>/<YOUR_INO_FILE_NAME>.bin
. - Press button
Upload Firmware
, select binary and pressUpdate Firmware
. After some time a messageUpdate Success! Rebooting...
should show up. If this does not happen within about 30 seconds, repeat the procedure.
- (MyHomeMyData) Hint for start of cannelloni corrected
- (MyHomeMyData) Added option for Web-Updates, added Reset-Button
- (MyHomeMyData) Force restart of ESP32 after 5 consecutive failures of UDP send attempts
- (MyHomeMyData) Bugfix: Avoid resets during startup
- (MyHomeMyData) Added support for UDP protocol
- (MyHomeMyData) Bugfix: Buffer pointes were wrong after reconnection to server
- (MyHomeMyData) Force attempt to reconnect to cannelloni server in case of TCP communication error
- (MyHomeMyData) Force restart of ESP32 in case of stalled TCP communication
- (MyHomeMyData) Reset CAN buffer on connection to cannelloni server
- (MyHomeMyData) Initial version