607 views|0 replies

106

Posts

4

Resources
The OP
 

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

  1. Gateway service: A web service built on the FastAPI framework that provides an HTTP interface.
  2. MQTT client: responsible for communicating with MQTT devices, managing device connections, message publishing and subscription.
  3. Device management: Maintain a device list and record the basic information and status of the device.
  4. Data storage: Use memory or database to store device data to ensure data persistence.

Component Design

  1. MQTT components:

    • Responsible for establishing a connection with the MQTT broker.
    • Subscribe to the device topic and receive messages sent by the device.
    • Publish messages to devices to achieve remote control.

  2. Device management components:

    • Maintain a device list and record the device's unique identifier (such as device ID), MQTT topic, connection status and other information.
    • Provides methods for adding, deleting, modifying and checking devices.

  3. HTTP Components:

    • Define HTTP interface based on FastAPI.
    • Receive user requests and call MQTT components and device management components to perform corresponding operations.
    • Return the operation result to the user.

4. Interface design

  1. Device List:

    • GET /devices: Returns a list of all devices.
    • POST /devices: Add a new device to the gateway.
    • DELETE /devices/{device_id}: Delete the specified device from the gateway.

  2. Equipment Details:

    • GET /devices/{device_id}: Returns detailed information about a specified device.

  3. Device data:

    • GET /devices/{device_id}/data: Get the latest data of the specified device.
    • POST /devices/{device_id}/data: Send data to the specified device.

  4. equipment control:

    • POST /devices/{device_id}/control: Send control commands to the specified device.

5. Data structure design

  1. Device Information:

    • Device ID (device_id): A string that uniquely identifies a device.
    • MQTT topic (mqtt_topic): The topic of the device on the MQTT broker.
    • Connection status (connection_status): A Boolean value indicating whether the device is online.
    • Other device properties (such as name, description, etc.).

  2. Device data:

    • Device ID (device_id): The device ID of the associated device information.
    • Timestamp: The time when data is sent or received.
    • Data content (data): The specific data sent or received by the device, which can be in JSON format or other formats.

6. Security Considerations

  • Provides secure HTTP communication using the HTTPS protocol.
  • Implement user authentication and authorization mechanisms to ensure that only authorized users can access and operate the device.
  • For sensitive operations (such as deleting a device), require secondary confirmation from the user or provide additional security measures.

7. Deployment and Expansion

  • Use Docker container to deploy the gateway service for easy management and expansion.
  • As needed, gateway instances can be scaled horizontally to handle more device connections and requests.

8. Implementation steps

  1. Install required Python libraries: fastapi, uvicorn, paho-mqtt, etc.
  2. Create a FastAPI application and define routes.
  3. Implement MQTT components, including connection, subscription, and publishing functions with MQTT broker.
  4. Implement the device management component, maintain the device list and provide methods for adding, deleting, modifying and checking.
  5. Implement HTTP component, call MQTT component and device management component to process user requests.
  6. Write test code to verify whether the various functions of the gateway are working properly.
  7. 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.
This post is from Domestic Chip Exchange
Personal signature

米尔电子,专注嵌入式处理器

 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list