High-Performance, Bi-Directional Gateway between CAN Bus and MQTT.
CanMQTT-Bridge is a production-ready Go application designed to bridge low-level CAN (Controller Area Network) hardware with high-level MQTT IoT networks.
It lets you control motors, read sensors, and monitor robots/vehicles using simple JSON over MQTT.
The bridge handles all binary translation, scaling, and protocol conversion automatically.
The bridge is optimized for high throughput and low latency using Go concurrency.
- Dedicated CAN workers
- Dedicated MQTT workers
- Prevents blocking & CPU starvation
- Direct Linux kernel CAN interface (e.g.,
can0)
- JSON configuration maps binary CAN frames β readable JSON
- Only subscribes to CAN IDs defined in config
- Reduces CPU usage
- Reads CAN frames
- Converts binary data to JSON
- Applies scaling factors
- Publishes to MQTT topic
- Receives JSON commands
- Converts scaled values back to raw binary
- Sends CAN frames to bus
Via MQTT topic: translater/run
Controlled at runtime:
- Reload config instantly
- Change debug level
- Set artificial sleep time for throttling
Bridge acts as time master.
- CAN ID:
0x5 - Frequency: 100ms
- Sends UNIX timestamp (nanoseconds)
Used for: microcontroller time sync.
Publishes telemetry to translater/status:
- CPU Load
- RAM Usage
- CPU Temperature
- Uptime
Useful for Raspberry Pi deployments.
Defines how CAN frames map to JSON.
{
"topic": "motor/data",
"canid": "0x63",
"length": 8,
"payload": [
{ "key": "rpm", "type": "int16_t", "place": [2, 4], "factor": 1.0 },
{ "key": "torque", "type": "float", "place": [4, 8], "factor": 0.001 }
]
}Incoming CAN (ID 0x63):
[0x00, 0x00, 0xE8, 0x03, 0x00, 0x40, 0x1C, 0x46]
Extracted:
- RPM = 1000
- Torque = 10.0
Outgoing MQTT:
{"rpm": 1000, "torque": 10.0}Direction: Bridge β MQTT
Provides:
- cpu_load
- ram_usage
- cpu_temp
- uptime
Direction: MQTT β Bridge
Commands:
reload: truedebug_level: 1-3sleep_time: ms
CAN ID: 0x5
mosquitto_pub -t "translater/run" -m '{"reload": true, "debug_level": 3}'
mosquitto_pub -t "motor/data" -m '{"rpm": 500, "torque": 2.5}'
mosquitto_sub -t "translater/status"
- Go 1.18+
- SocketCAN (
can0) - MQTT broker
cd CanMQTT-Bridge
go mod tidy
go build -o can2mqtt cmd/can2mqtt/main.go
sudo ip link set can0 up type can bitrate 500000
./can2mqtt
cmd/can2mqtt/main.goβ Entry pointinternal/bridge/convertfunctions.goβ Translation engineinternal/bridge/canbushandling.goβ CAN handlinginternal/bridge/receivehandling.goβ Worker pools
Distributed under the MIT License.