Building a Distributed API-Driven Relay Control System with Arduino, ESP32, .NET Core, RabbitMQ, and Next.js

Building a Distributed API-Driven Relay Control System with Arduino, ESP32, .NET Core, RabbitMQ, and Next.js

Hello everyone,

I'm thrilled to share my ambitious project that combines several technologies: an Arduino Uno, an ESP32, an 8-channel relay module, a .NET Core Web API, RabbitMQ for messaging, and a Next.js frontend application. The goal of my project is to create a distributed system where multiple Arduinos, located in different houses, can control their respective relays remotely through a centralized API. Each Arduino will be listening for event-driven commands from the server to update the states of its specific relays.

Project Overview

Here's a brief outline of the key components involved in this project:

  1. Multiple Arduino Uno Units: Each Arduino will be set up in different locations (different houses), responsible for controlling its own 8-channel relay module. Each unit will listen for commands from the server to change the state of its relays.

  2. ESP32 Modules: Each Arduino setup will include an ESP32 to connect to the internet, enabling communication with the centralized API server and RabbitMQ for receiving messages.

  3. 8-Channel Relay Modules: Each relay module in the different locations will control various devices locally, with each relay representing a specific function or device.

  4. .NET Core Web API: I will develop a robust API that will act as the central point of communication, handling requests from the Next.js frontend and facilitating control of the distributed Arduino units.

  5. RabbitMQ: This messaging broker will be used for event-driven communication across all locations. It will publish messages to different Arduinos, indicating which relay to control and the desired action.

  6. Next.js Frontend Application: I plan to create a user-friendly interface using Next.js that will allow users to send commands to any Arduino setup in any location through the centralized API. It will display the status of each relay for all connected locations.

Technical Stack

To bring this project to life, I will utilize the following technologies:

  • .NET Core Web API: For creating RESTful endpoints that the frontend and all distributed Arduino units will communicate with.
  • RabbitMQ: To handle messaging and event-driven architecture, ensuring that commands and status updates are efficiently communicated to each Arduino, no matter where it's located.
  • Next.js: As my frontend framework, I will develop a responsive and dynamic web application that provides a seamless user experience while interacting with the API and controlling relays across different locations.

Workflow

Here’s a high-level overview of how I envision the entire distributed system workflow:

  1. API Development: The centralized .NET Core Web API will provide endpoints for controlling relays (e.g., turn on/off) and retrieving their current states for each distributed Arduino unit.
  2. Frontend Consumption: The Next.js application will fetch data from the API to display the status of each relay for all connected locations. Users will interact with the UI to send commands, which will be directed to the appropriate Arduino setup.
  3. Event Broadcasting: When the API receives a command for a specific relay, it will publish a message to RabbitMQ to indicate which Arduino and which relay to control, along with the desired action.
  4. Arduino Listener: Each Arduino, connected via its ESP32, will listen for messages from RabbitMQ. Upon receiving a command, it will control the appropriate relay and send a confirmation back to the API.
  5. Status Updates: Each Arduino will also periodically report its status to the API, allowing the Next.js application to refresh and display the current states of all relays in real-time for all locations.

Challenges and Considerations

While I am excited about this project, I expect to encounter a few challenges, including:

  • Scalability: Ensuring the system remains scalable as more Arduinos are added in different locations.
  • Network Reliability: Maintaining stable communication between the distributed Arduinos, the API server, and RabbitMQ, especially when dealing with different network environments.
  • Security: Implementing secure communication between the API, RabbitMQ, and the Arduinos to prevent unauthorized access.
  • Real-Time Synchronization: Ensuring that the Next.js frontend reflects the state of each relay across all distributed locations in real-time without delay.

Request for Guidance

I am reaching out to this vibrant community for help with the following:

  1. Best Practices: Any best practices for setting up a centralized API, RabbitMQ, and distributed Arduino communication across multiple locations would be immensely helpful.
  2. Code Examples: If anyone has code snippets or project examples that align with this setup, particularly for handling multiple devices in different locations, I would greatly appreciate your sharing.
  3. Common Pitfalls: Insights into common challenges others have faced when integrating these technologies in a distributed system would be valuable.

Thank you for taking the time to read about my project! I look forward to your insights, suggestions, and feedback as I embark on this exciting journey of building a distributed, event-driven relay control system with a modern web interface.

Best regards,
Ali Yorulmaz

Why do you need the Arduino Uno? The ESP32 has enough GPIO pins for 8 relays.

Oh sorry, i misspelled that part. I can use Arduino and esp8266. I have seperate esp32 to test it as second device on seperate relays.

So, what's your coding experience level?

I have pretty good knowledge of .net core web api, nextjs. Never played with mqtt stuff(rabbitmq etc) before but i have general information about event driven architecture.

The Arduino ecosystem of boards are programmed in C++.

Yes ive played some on arduino ide since i bought card not that much ago. I don’t think i will have problem about programming card beside of api and frontend application.

So far, i designed some UI for managing houses(cards), relays for cards, toggling relays, watching logs, managing secret key and deviceIds.
https://www.youtube.com/watch?v=zd3thNvxinI

On backend i tried to MOCK and arduino using some node code. Created mock servers which toggles relays and give responses to relay status requests.

Created gateway(i will convert express js to .net core web api after i fnish MOCKS.) Which will routes all requests to spesific(device based) MQTT(RabbitMQ) topics.
https://www.youtube.com/watch?v=AmS_tx6dMhk&ab_channel=AliYorulmaz

The thing of project is i will try to create a complete application to manage all my seperate cards with own relays. Easily manage them using same gateway. Creating device codes(hard coded arduino stuff with c++) automaticly, simply download them for uploading devices).
After that i can exactly log every action card makes with mqtt and store it on some kind of database.

I will try to figure out MQTT messages with device based secret keys now. If anyone used some encryption on arduino, please post some examples.

Thank you.

Project Update: Secure Communication and Encryption Mechanism with MQTT for Arduino Devices

Hello everyone,

I wanted to share a quick update on the progress I've made with my project. Previously, I mentioned that I was working on a complete application to manage multiple relay boards (cards) using a unified gateway, along with mock servers simulating Arduino responses and a Node.js gateway.

New Updates:

  1. Device-Specific Encryption Keys: I have successfully implemented a secure communication mechanism where each device (e.g., Device1 and Device2) now has its own unique SECRET_KEY and decryption logic embedded within the device itself.

  2. Gateway-Side Encryption: I've added an encryption mechanism on the gateway (the server responsible for sending requests to all devices). The gateway generates a test code in the format ${device}-${timestamp} and encrypts it using the aes-256-cbc algorithm before sending it to the device-specific MQTT channel. The request structure looks like this:

    {
      "relayIndex": "0",
      "state": "1",
      "secretKey": "7x!A%D*G-KaPdSgVkYp3s6v9y$B&E)H2"
    }
    
  3. Device Authentication: Each device receives the encrypted message via its MQTT channel, decrypts it, and verifies its authenticity using the embedded decryption logic:

    • If the secretKey is incorrect, the device rejects the request and returns an error message.
    • If the secretKey is valid, the device performs the requested action and sends a success response.

To see a practical demonstration of how this works, including tests with Device1 (true key) and Device2 (false key), please check out the following video:
https://youtu.be/klCNXMPnHHM

Next Steps

My next focus will be on developing an interface that allows configuration of the MQTT server settings easily. I'll keep you all posted on further updates as I continue refining the system.

If anyone has experience with encryption mechanisms on Arduino or suggestions for improving this setup, I'd love to hear your thoughts!

Thank you for your time, and I look forward to any feedback or suggestions!

Project Update: Interface for MQTT Server Configuration and Next Steps

Hello everyone,

I have completed the development of an interface that allows for easy configuration of MQTT server settings. You can check out the demonstration of the new interface in the video below:

Next Steps:

My upcoming focus will be on establishing a database connection through an Arduino board. The goal is to listen to the MQTT server and log all activities from each device, ensuring that every action is recorded and stored properly.

If anyone has experience or insights on integrating Arduino with a database for logging purposes or any suggestions on how to approach this setup, I would greatly appreciate your thoughts!

Thank you in advance, and I'm looking forward to any ideas or feedback you might have!

my thought is:

leave away arduinos

ESP32:

More RAM, more Flash, more clock-speed, UARTS, higher-baudrate software-serials

including I2C-Bus, I2S-Bus, SPI-bus, CAN-Bus can be driven directly

Why on earth do you want to add Arduino-Unos or Arduino-Megas or Arduino-Nanos
when ESP32-boards have 8 to 43 IO-pins?

To be really honest
@aliyrlmz

You are one of these guys that are sooooo proud of what they are doing
Your video is nothing more than an arrogant

"look how fast I can click the mouse between multiple applications"

In your Video
Sound: no sound at all
You explain: nothing
switching relays: not visible

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.