# ducvinh2910/lab_iot architecture

> Architecture diagram of the GitHub repository ducvinh2910/lab_iot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ducvinh2910/lab_iot
- Repository: https://github.com/ducvinh2910/lab_iot

## Overview

##### Project Type and Purpose
- This is an IoT firmware project specifically built for embedded microcontroller development using PlatformIO.
- The project is designed to run on devices such as the M5Stack Atom or YOLO Uno (as suggested by the board files) and provides functionalities such as a web server and MQTT connectivity.
- It leverages libraries like ESPAsyncWebServer, SPIFFS for filesystem operations, and Adafruit_MQTT_Library to communicate with external MQTT brokers.

##### File Structure for Architectural Clues
- Top-Level Files and Directories:
- The presence of directories like src, include, lib, and boards indicates separation between source code, header files, libraries, and board configuration files.
- The src folder is organized into a modular structure with further subdirectories:
▪ src/app contains application-level code, e.g., reset.cpp and web_Server_Task.cpp.
▪ src/tasks contains individual tasks, such as background_Task.cpp/h.
▪ src/utils holds utility code, e.g., version.h.
- The data directory includes web content files (HTML and CSS) which are likely to be served by the embedded web server using SPIFFS.
- Configuration files like platformio.ini, project_config.h, and boards JSON definitions help manage the build environment and target hardware.

##### Insights from the README File
- The README clearly states this is an IoT Starter Kit and outlines the flashing and filesystem upload process.
- Included libraries confirm that the key functionalities include asynchronous web services and MQTT connectivity.
- The README instructions indicate how to enter bootloader mode, flash firmware, and build/upload SPIFFS images, all pointing towards an embedded, full-stack (firmware + web interface) system design.

a. Main Components:
- Hardware Platform:
▪ Microcontroller Board (e.g., M5Stack Atom, YOLO Uno) – as defined in the boards folder.
- Firmware Core:
▪ Bootloader (mode handling, not explicitly present in code but implied by instructions).
▪ Main Application (src/main.cpp) – the entry point that initializes the firmware.
- Application Modules:
▪ App Module (src/app): Responsible for hardware reset management and handling web server tasks (web_Server_Task.cpp).
▪ Tasks Module (src/tasks): Runs background processes (background_Task.cpp/h).
▪ Utility Module (src/utils): Holds utility functions such as retrieving version information (version.h).
- External Interfaces:
▪ Web Server Component (leveraging ESPAsyncWebServer) – to serve static content stored in SPIFFS (data folder with index.html and style.css).
▪ MQTT Communication (using Adafruit_MQTT_Library) – for IoT connectivity with external brokers.
- Filesystem Storage:
▪ SPIFFS for storing the web interface, updated through PlatformIO’s Tools.

b. Relationships and Interactions:
- The Firmware (main.cpp) initializes the hardware and sets up the different application modules.
- The Web Server Task (under src/app) interacts with the SPIFFS storage to serve content from the data directory.
- The Background Task (under src/tasks) runs alongside to manage periodic or asynchronous tasks.
- External connectivity is handled via MQTT and web client connections, where data flows from the device to external servers or services.

c. Important Architectural Patterns/Design Principles:
- Modular design: Code is clearly separated into distinct modules (app, tasks, utils) for clarity and maintainability.
- Asynchronous processing: Use of ESPAsyncWebServer implies that the design relies on event-driven asynchronous handling of requests.
- Separation of concerns: The codebase is organized to separate data (SPIFFS), network interfaces (MQTT/WebServer), and core application logic.

d. Relevant Technologies and Frameworks:
- PlatformIO for project management and build/deployment.
- ESPAsyncWebServer for the web server handling.
- SPIFFS for internal filesystem management.
- Adafruit_MQTT_Library for MQTT communication.
- The board definitions and configuration files tie the code to specific hardware.

##### Elements
- Hardware (with boards like M5Stack-Atom and YOLO Uno illustrated).
- Web Server block and File System (SPIFFS) block.
- MQTT Communication block connecting to external systems.
- Initialization flow from main firmware to individual modules.
- Data flow from the web server to SPIFFS and back.
- Communication routes from the firmware to external MQTT brokers.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Hardware Platform Subgraph
    subgraph "Hardware Platform"
        A1["M5Stack Atom"]:::hardware
        A2["YOLO Uno"]:::hardware
    end

    %% Firmware Core
    F["Firmware Core (src/main.cpp)"]:::firmware

    %% Application Modules Subgraph
    subgraph "Application Modules"
        AM["App Module (src/app)"]:::module
        TM["Tasks Module (src/tasks)"]:::module
        UM["Utility Module (src/utils/version.h)"]:::module
    end

    %% External Interfaces
    WS["SPIFFS Storage (data/)"]:::storage
    MQTT["MQTT Communication"]:::external
    Broker["MQTT Broker (External)"]:::external

    %% Build & Configuration Management
    BC["Build & Config Management (platformio.ini & project_config.h)"]:::buildConfig

    %% Connections
    A1 -->|"initializes"| F
    A2 -->|"initializes"| F
    F -->|"loads modules"| AM
    F -->|"loads modules"| TM
    F -->|"loads modules"| UM
    AM -->|"serves content"| WS
    F -->|"handles"| MQTT
    MQTT -->|"communicates with"| Broker
    BC -->|"builds"| F

    %% Click Events
    click A1 "https://github.com/ducvinh2910/lab_iot/blob/main/boards/m5stack-atom.json"
    click A2 "https://github.com/ducvinh2910/lab_iot/blob/main/boards/yolo_uno.json"
    click F "https://github.com/ducvinh2910/lab_iot/blob/main/src/main.cpp"
    click AM "https://github.com/ducvinh2910/lab_iot/tree/main/src/app/"
    click TM "https://github.com/ducvinh2910/lab_iot/tree/main/src/tasks/"
    click UM "https://github.com/ducvinh2910/lab_iot/blob/main/src/utils/version.h"
    click WS "https://github.com/ducvinh2910/lab_iot/tree/main/data/"
    click BC "https://github.com/ducvinh2910/lab_iot/blob/main/platformio.ini"

    %% Styles
    classDef hardware fill:#ffcccb,stroke:#b30000,stroke-width:2px;
    classDef firmware fill:#cce5ff,stroke:#003d99,stroke-width:2px;
    classDef module fill:#d5f5e3,stroke:#1d8348,stroke-width:2px;
    classDef external fill:#fff2cc,stroke:#b38f00,stroke-width:2px;
    classDef buildConfig fill:#d6eaf8,stroke:#2874a6,stroke-width:2px;
    classDef storage fill:#fcf3cf,stroke:#b7950b,stroke-width:2px;
```
