Skip to content

akashdip2001/Cross-Domain-Protocol-Translator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Cross-Domain-Protocol-Translator : MQTT - CAN Bus

Project Title: Development of a Cross-Domain Communication Bridge between MQTT Protocol and In-Vehicle CAN Bus System


Student Name:

[Your Name Here] Department: Mechanical Engineering with Interest in Embedded Systems / IoT Advisor: [Professor's Name] Date of Submission: [Insert Date]


1. Introduction

Modern vehicles are becoming increasingly connected, with the integration of cloud-based services, mobile applications, and real-time telemetry. Most vehicles use the CAN (Controller Area Network) bus protocol for internal communication between Electronic Control Units (ECUs). On the other hand, MQTT (Message Queuing Telemetry Transport) is a lightweight protocol widely used in IoT and remote monitoring applications.

This project proposes the development of a Cross-Domain Communication Bridge that enables two-way communication between MQTT and CAN bus, providing real-time control and monitoring of vehicle systems remotely.


2. Objective

To design and implement a hardware-software system that:

  • Subscribes to MQTT topics and converts messages into CAN frames.
  • Sends CAN bus messages to MQTT for remote monitoring.
  • Acts as a secure, scalable bridge between cloud/mobile apps and vehicle systems.

3. System Architecture

3.1 Block Diagram

[ Mobile App / MQTT Client ]
          |
          v
[ MQTT Broker (Cloud) ]
          |
          v
[ MQTT-CAN Gateway (Raspberry Pi or MCU) ]
          |
          v
[ CAN Bus ]
          |
          v
[ In-Vehicle ECUs (e.g., door lock, engine control) ]

3.2 Components Used

  • Hardware:

    • Raspberry Pi (or ESP32 / STM32 MCU)
    • MCP2515 CAN transceiver
    • 12V to 5V voltage converter
    • OBD-II port connection for CAN
  • Software:

    • Python 3
    • paho-mqtt (for MQTT communication)
    • python-can (for CAN communication)
    • JSON for message payloads

4. Working Principle

  1. The gateway device connects to a public or private MQTT broker.
  2. It subscribes to control topics like car/control.
  3. Incoming MQTT messages are parsed and translated into CAN bus messages.
  4. The translated CAN frames are sent to the vehicle's CAN bus.
  5. The system also listens to the CAN bus and publishes readable data (e.g., speed, fuel) to an MQTT topic like car/status.

5. MQTT to CAN Python Code Explanation

import can
import paho.mqtt.client as mqtt
import json

MQTT_BROKER = "broker.hivemq.com"
MQTT_TOPIC_SUB = "car/control"
MQTT_TOPIC_PUB = "car/status"

can_interface = 'can0'
bus = can.interface.Bus(channel=can_interface, bustype='socketcan')

def on_message(client, userdata, msg):
    data = json.loads(msg.payload.decode())
    can_id = data.get("id", 0x123)
    payload = bytes(data.get("data", [0x00]))
    message = can.Message(arbitration_id=can_id, data=payload, is_extended_id=False)
    bus.send(message)
    print(f"Sent to CAN: ID={hex(can_id)}, Data={payload.hex()}")

def can_to_mqtt(client):
    while True:
        message = bus.recv()
        if message:
            payload = {
                "id": message.arbitration_id,
                "data": list(message.data)
            }
            client.publish(MQTT_TOPIC_PUB, json.dumps(payload))
            print(f"Published to MQTT: {payload}")

client = mqtt.Client()
client.on_message = on_message
client.connect(MQTT_BROKER)
client.subscribe(MQTT_TOPIC_SUB)
client.loop_start()

try:
    print("Bridge is running...")
    can_to_mqtt(client)
except KeyboardInterrupt:
    print("Stopping bridge.")
    client.loop_stop()

6. Message Format Examples

  • MQTT to CAN message example:
{
  "id": 291,
  "data": [1, 0, 255]
}
  • CAN to MQTT (published message):
{
  "id": 291,
  "data": [1, 0, 255]
}

7. Advantages & Applications

  • Enables remote diagnostics and control
  • Useful in fleet management systems
  • Can be extended to automated emergency services
  • Forms the base for connected autonomous vehicle architectures

8. Future Scope

  • Add support for other protocols like Modbus, FlexRay, or LIN
  • Add encryption, authentication, and logging
  • Mobile app interface for real-time control and status

9. Conclusion

This project successfully demonstrates the bridging of two different communication domains: MQTT (cloud/mobile) and CAN bus (automotive embedded). The proposed MQTT-CAN bridge can become a key component in modern IoT-enabled automotive systems.


10. References

  • ISO 11898 Standard (CAN Protocol)
  • MQTT.org Documentation
  • Python python-can and paho-mqtt libraries
  • Raspberry Pi CAN interface guides

About

CAN bus to MQTT

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published