Skip to content

A High-Performance, Bi-Directional Gateway between CAN Bus and MQTT.

License

Notifications You must be signed in to change notification settings

farouk15160/CanMQTT-Bridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

32 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸŒ‰ CanMQTT-Bridge

High-Performance, Bi-Directional Gateway between CAN Bus and MQTT.


πŸ“– Overview

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.


πŸ—οΈ System Architecture

The bridge is optimized for high throughput and low latency using Go concurrency.

βœ”οΈ Worker Pool Design

  • Dedicated CAN workers
  • Dedicated MQTT workers
  • Prevents blocking & CPU starvation

βœ”οΈ SocketCAN Integration

  • Direct Linux kernel CAN interface (e.g., can0)

βœ”οΈ Dynamic Translation

  • JSON configuration maps binary CAN frames β†’ readable JSON

βœ”οΈ Filtering

  • Only subscribes to CAN IDs defined in config
  • Reduces CPU usage

✨ Key Features

1. πŸ”„ Bi-Directional Translation

CAN β†’ MQTT

  • Reads CAN frames
  • Converts binary data to JSON
  • Applies scaling factors
  • Publishes to MQTT topic

MQTT β†’ CAN

  • Receives JSON commands
  • Converts scaled values back to raw binary
  • Sends CAN frames to bus

2. πŸŽ›οΈ Hot-Reload & Runtime Control

Via MQTT topic: translater/run

Controlled at runtime:

  • Reload config instantly
  • Change debug level
  • Set artificial sleep time for throttling

3. ⏱️ CAN Bus Synchronization (Heartbeat)

Bridge acts as time master.

  • CAN ID: 0x5
  • Frequency: 100ms
  • Sends UNIX timestamp (nanoseconds)

Used for: microcontroller time sync.


4. πŸ₯ System Health Monitoring

Publishes telemetry to translater/status:

  • CPU Load
  • RAM Usage
  • CPU Temperature
  • Uptime

Useful for Raspberry Pi deployments.


βš™οΈ Configuration (messages_config.json)

Defines how CAN frames map to JSON.

Example Item

{
  "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 }
  ]
}

Process Example

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}

πŸ“‘ Internal MQTT System Topics

1. translater/status

Direction: Bridge β†’ MQTT

Provides:

  • cpu_load
  • ram_usage
  • cpu_temp
  • uptime

2. translater/run

Direction: MQTT β†’ Bridge

Commands:

  • reload: true
  • debug_level: 1-3
  • sleep_time: ms

3. CAN Clock Sync

CAN ID: 0x5


πŸ‘¨β€πŸ’» Interaction Examples

Reload config & enable debug

mosquitto_pub -t "translater/run" -m '{"reload": true, "debug_level": 3}'

Send control data to CAN

mosquitto_pub -t "motor/data" -m '{"rpm": 500, "torque": 2.5}'

Monitor health

mosquitto_sub -t "translater/status"

πŸš€ Installation

Prerequisites

  • Go 1.18+
  • SocketCAN (can0)
  • MQTT broker

Build

cd CanMQTT-Bridge
go mod tidy
go build -o can2mqtt cmd/can2mqtt/main.go

Bring up CAN bus

sudo ip link set can0 up type can bitrate 500000

Run

./can2mqtt

πŸ› οΈ Development Internals

  • cmd/can2mqtt/main.go – Entry point
  • internal/bridge/convertfunctions.go – Translation engine
  • internal/bridge/canbushandling.go – CAN handling
  • internal/bridge/receivehandling.go – Worker pools

πŸ“œ License

Distributed under the MIT License.

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages