Managing multiple MQTT devices via IoT - based on Mir T527 development board
[Copy link]
This review was provided by "JerryZhen", an excellent reviewer from Electronic Engineering World.
This article will introduce the gateway solution test based on the Mir Electronics MYD-LT527 development board.
1. System Overview
A simple IoT gateway is designed based on Mir-Allwinner T527. The gateway can manage multiple MQTT devices and read and write devices through the MQTT protocol. It also provides an HTTP interface, allowing users to interact with the gateway through the HTTP protocol and read and write devices.
2. System Architecture
-
Gateway service: A web service built on the FastAPI framework that provides an HTTP interface.
-
MQTT client: responsible for communicating with MQTT devices, managing device connections, message publishing and subscription.
-
Device management: Maintain a device list and record the basic information and status of the device.
-
Data storage: Use memory or database to store device data to ensure data persistence.
Component Design
-
MQTT components:
-
Device management components:
-
HTTP Components:
4. Interface design
-
Device List:
-
Equipment Details:
-
Device data:
-
equipment control:
5. Data structure design
-
Device Information:
-
Device data:
6. Security Considerations
7. Deployment and Expansion
8. Implementation steps
-
Install required Python libraries: fastapi, uvicorn, paho-mqtt, etc.
-
Create a FastAPI application and define routes.
-
Implement MQTT components, including connection, subscription, and publishing functions with MQTT broker.
-
Implement the device management component, maintain the device list and provide methods for adding, deleting, modifying and checking.
-
Implement HTTP component, call MQTT component and device management component to process user requests.
-
Write test code to verify whether the various functions of the gateway are working properly.
-
Deploy the gateway service and monitor its running status.
This design is just an overview, and the specific implementation details may need to be adjusted and optimized according to actual needs and project environment. In actual development, issues such as exception handling, logging, and performance optimization also need to be considered. Based on the above design, the following is a simplified reference code that shows how to use FastAPI and the paho-mqtt library to create an IoT gateway. It should be noted that the example does not include complete error handling, user authentication, and authorization mechanisms, which are essential in an actual production environment. Dependent main library versions:
fastapi==0.108.0
paho-mqtt==1.6.1
Gateway simulation code gateway.py:
Device 1 simulation code dev1.py:
Device 2 simulation code dev2.py
Run the gateway code and open the web page to get the API interface:
Add device 1 and device 2 through the API respectively.
Run the code for simulated device 1 and simulated device 2 in two other consoles respectively
Send data to device 1 via web API
The data replied by the device is obtained through the web API. The device code simply returns the data sent by the gateway.
We can see the complete data flow in the background of the gateway
So far, a simple gateway has been realized. Next, we will try to implement communication management of the most common bacnet devices in the building.
|