Site Information

 Loading... Please wait...

Blog

Bridging CAN Bus to the Cloud: Building MQTT or WebSocket Gateways with ESP32

Posted by Wilfried Voss on

Bridging CAN Bus to the Cloud: Building MQTT or WebSocket Gateways with ESP32The Controller Area Network (CAN) is a robust and widely used communication protocol in automotive, industrial, and marine applications. It enables reliable, real-time data exchange between electronic control units (ECUs), sensors, and actuators. With the growing demand for remote monitoring, predictive maintenance, and IoT integration, there is an increasing need to bridge CAN data to the cloud.

The ESP32—a low-cost, high-performance microcontroller with built-in Wi-Fi and Bluetooth—offers a versatile platform for creating CAN-to-cloud gateways. By integrating MQTT or WebSocket protocols, developers can design solutions that transmit CAN data to cloud services for analytics, dashboards, and automation.


Why Use ESP32 for CAN-to-Cloud Gateways

The ESP32 is ideal for CAN-to-cloud applications due to:

  • Integrated Wireless Connectivity: Eliminates the need for additional networking hardware.

  • Multiple I/O Interfaces: Supports SPI, I2C, UART, and CAN (via external transceiver).

  • Strong Processing Capabilities: Dual-core processor and ample RAM for protocol handling and encryption.

  • Low Power Consumption: Suitable for battery-powered remote devices.

  • Cost-Effectiveness: Affordable for large-scale deployment.


CAN Interface Hardware

While the ESP32 itself does not have a native CAN transceiver, many modules use the SJA1000, MCP2515, or SN65HVD230 for physical layer interfacing. A typical setup includes:

  1. ESP32 Development Board – Such as ESP32-DevKitC or similar.

  2. CAN Transceiver Module – Converts ESP32 CAN controller signals to differential CAN bus lines.

  3. Termination Resistor (120 Ω) – Ensures proper signal integrity when at the end of the CAN network.


Gateway Architecture

A CAN-to-cloud gateway based on ESP32 generally consists of three layers:

  1. Data Acquisition Layer

    • Reads raw CAN frames from the transceiver.

    • Parses identifiers, data length codes, and payloads.

  2. Processing & Formatting Layer

    • Filters relevant CAN IDs.

    • Formats messages into JSON or other lightweight formats.

    • Optionally applies encryption or compression.

  3. Transport Layer

    • Uses MQTT or WebSocket to transmit data to the cloud.

    • Manages reconnection, QoS (in MQTT), and buffering for offline scenarios.


MQTT Implementation

MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe protocol optimized for low-bandwidth and high-latency networks.

Key Steps for MQTT Gateway:

  1. Connect ESP32 to Wi-Fi.

  2. Initialize CAN communication with the transceiver.

  3. Create a JSON-formatted message from the CAN frame.

  4. Publish the message to an MQTT broker topic (e.g., vehicle/data/can1).

  5. Subscribe to relevant control topics for remote commands.

Advantages of MQTT:

  • Low overhead.

  • Reliable delivery with QoS levels.

  • Retained messages for last known state.


WebSocket Implementation

WebSockets offer a full-duplex communication channel over a single TCP connection, ideal for real-time streaming of CAN data to a browser or cloud application.

Key Steps for WebSocket Gateway:

  1. Establish a WebSocket connection with the server.

  2. Send CAN messages as they are received.

  3. Receive control or configuration data from the server in real time.

  4. Display live updates in a dashboard without refreshing the page.

Advantages of WebSocket:

  • Real-time bidirectional communication.

  • Compatible with web browsers without additional plugins.

  • Ideal for live dashboards and rapid feedback systems.


Practical Considerations

  • Security: Always use TLS/SSL encryption for MQTT or WebSocket to prevent unauthorized access.

  • Data Filtering: Avoid overloading the network by sending only necessary CAN messages.

  • Offline Operation: Implement buffering to store messages when the network is unavailable.

  • Scalability: Ensure the broker or server can handle the expected message frequency and number of connected devices.


Example Use Cases

  • Fleet Management: Real-time monitoring of vehicle diagnostics and GPS location.

  • Industrial Equipment: Predictive maintenance and performance tracking.

  • Marine Applications: Remote monitoring of shipboard systems.

  • Agricultural Machinery: Cloud-based analysis of operational data for efficiency.


Hardware Solutions

The ESP32-based CAN bus boards, offered by Copperhill Technologies, are purpose-built for bridging local network data to cloud platforms, making them ideal for modern IoT, automotive, industrial, and marine applications. Each board combines powerful processing capability with built-in Wi-Fi and Bluetooth, enabling wireless communication without additional modules. By interfacing directly with CAN, CAN FD, or NMEA 2000 networks, they can collect, process, and transmit real-time data to cloud services using protocols like MQTT or WebSocket. This makes them well-suited for applications such as fleet monitoring, remote diagnostics, predictive maintenance, and live system dashboards—anywhere reliable, secure, and scalable cloud connectivity is essential.


ESP32 WiFi, Bluetooth Classic, BLE, CAN Bus ModuleESP32 Wi-Fi, Bluetooth Classic, BLE, CAN Bus Module
This compact and versatile development module brings together the renowned ESP32-WROOM-32 microcontroller—dual-core Xtensa LX6, Wi‑Fi (802.11 b/g/n), Bluetooth Classic, and BLE—with a built-in CAN transceiver. Designed for seamless integration into IoT and embedded systems, it delivers real-time CAN bus connectivity alongside wireless communication, allowing developers to quickly prototype gateways, diagnostics tools, or telemetry systems in agricultural, industrial, and automotive environments. More information...


ESP32S3 Board with CAN FD and Classical CAN PortsESP32-S3 Board with CAN FD and Classical CAN Ports
Built around the high-performance ESP32‑S3-WROOM-1‑N8R8—featuring a dual-core Xtensa LX7 at up to 240 MHz, 8 MB flash, and 8 MB PSRAM—this board supports both legacy Classical CAN (through the ESP32-S3’s built-in TWAI controller) and modern CAN FD via an onboard MCP2518FD controller. With native USB support, Wi‑Fi, Bluetooth 5 (LE and mesh), flexible power input (7–24 V with reverse-polarity protection), and useful peripherals such as RGB LED, I²C port, boot/reset buttons, and USB‑C connector, this board is ideal for building robust, high-speed IoT gateways, industrial automation systems, and hybrid CAN communication solutions. More information...


ESP32S3 CAN-Bus Board NMEA2000 ConnectorESP32-S3 CAN Bus Board with NMEA 2000 Connector
Purpose-built for marine and edge IoT applications, this board integrates the powerful ESP32‑S3-WROOM-1‑N8R8 SoC (dual‑core LX7 at 240 MHz, 8 MB flash, 8 MB PSRAM) with Wi‑Fi and Bluetooth LE, and features a built-in CAN transceiver wired directly to an industry-standard NMEA 2000 Micro‑C connector. Coupled with user-friendly hardware such as USB‑C programming interface, BOOT and RESET buttons, RGB status LED, and rugged 7–24 V power input with reverse-polarity protection, it enables plug-and-play marine data logging, wireless NMEA 2000 bridging, and vessel system integration with ease and professionalism. More information...


Conclusion

Bridging CAN bus systems to the cloud using an ESP32-based MQTT or WebSocket gateway provides a flexible, cost-effective, and powerful solution for modern connected applications. Whether for automotive diagnostics, industrial automation, or IoT deployments, the ESP32 enables developers to build secure, scalable, and real-time data pipelines between field devices and cloud platforms.


Hands-on ESP32 with Arduino IDE: Unleash the power of IoT with ESP32 and build exciting projects with this practical guide

Hands-on ESP32 with Arduino IDE: Unleash the power of IoT with ESP32 and build exciting projects with this practical guideThe ESP32 is a powerful and versatile microcontroller, ideal for those venturing into the world of IoT (Internet of Things). While it offers a wealth of capabilities—including Wi-Fi and Bluetooth connectivity, camera support, and sensor interfacing—its initial configuration and integration with external components can be daunting for newcomers. Fortunately, the Arduino Integrated Development Environment (IDE) simplifies programming, code uploading, and access to ESP32’s rich feature set, making it easier for beginners to bring their ideas to life.

This book is designed to guide you through the fundamentals of sensing, networking, data processing, and real-world IoT applications using the ESP32. It begins with the essentials of working with ESP32 and Arduino IDE 2.0, providing step-by-step instructions for setting up your development environment.

You’ll then dive into hands-on projects, learning how to interface various sensors, as well as ESP32-compatible camera and display modules. These practical examples lay the groundwork for understanding more advanced topics, such as IoT networking protocols (e.g., MQTT, HTTP, WebSocket) and their roles in building connected devices.

As you progress, you'll apply your skills to build projects ranging from smart devices to data loggers and automation systems. Through these engaging applications, you’ll develop a solid understanding of how to design, prototype, and deploy your own IoT solutions.

By the end of this book, you’ll be equipped to:

  • Confidently develop and troubleshoot ESP32-based projects

  • Choose appropriate IoT communication protocols for your applications

  • Build and deploy functional IoT systems with real-world relevance

Whether you're a student, hobbyist, or aspiring engineer, this book provides a practical, project-based pathway to mastering the ESP32 and its role in the ever-expanding world of IoT. More information...

Teensy 4.1 Triple CAN Bus Board: Bridge Vehicle Networks to the Cloud with Ethernet and CAN FD

Modern vehicles rely heavily on Controller Area Network (CAN) systems to manage communication among ECUs, sensors, actuators, and diagnostic modules. As connectivity becomes pervasive—enabling remote diagnostics, over-the-air updates, and fleet management—the need to securely and efficiently bridge CAN bus data to cloud infrastructure becomes critical. The Teensy 4.1 Triple CAN Bus Board with 240×240 LCD and Ethernet [...]

Read More »