# dino-rider/esp-idf-mqtt-device architecture

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

- Interactive diagram: https://gitdiagram.com/dino-rider/esp-idf-mqtt-device
- Repository: https://github.com/dino-rider/esp-idf-mqtt-device

## Overview

##### Main Components
- IoT Device Firmware – At the heart of the project is the firmware running on the ESP device.
- MQTT Client Module – This is encapsulated in both the “components/esp_mqtt_cxx” module and parts of the “main” code (MqttClient.cpp/hpp). It handles MQTT connection, automatic reconnections, and subscription/publication logic.
- WiFi Connectivity – Although not explicitly a directory, the README and configuration files (like Example_Connection_Configuration and Kconfig.projbuild) indicate that WiFi handling and reconnection logic are integral.
- Peripheral Handling – The “main/Peripherals.cpp/hpp” files, alongside the dedicated task for blinking an onboard LED on connection loss, represent the peripheral management aspect.
- Sensor Data Collection – Using the Sensor base class (as mentioned in the README) to capture and publish WiFi signal strength when it changes.

##### Break Down the Module Responsibilities and Their Interactions
- The Firmware Core (main/main.cpp, IotDevice.cpp) acts as the entry point orchestrating the system.
- The MQTT Client Component (MqttClient.cpp/hpp) handles MQTT connections, publishes messages to various subtopics, and processes incoming messages using callbacks defined by inheriting the “MqttCaller” interface.
- The “esp_mqtt_cxx” component provides C++ abstraction over the native esp-idf MQTT functionalities. It externalizes MQTT communication logic (see its CMakeLists.txt, component.mk, and header files).
- Peripheral management (Peripherals.cpp/hpp) manages hardware aspects such as LED blinking and potentially other hardware functions.
- Configuration and Build Setup – The CMakeLists.txt and Makefile files indicate how the project is built and integrated with the ESP-IDF environment. They are part of the overarching system context.

##### Architectural Patterns
- Component-based Design – Separating core MQTT functionality into its own component (“components/esp_mqtt_cxx”) while leaving the overall firmware orchestration in “main” shows modularity.
- Object-Oriented Extensibility – Inheriting from abstract interfaces (like MqttCaller) to receive onMessage callbacks ensures that the architecture is extensible.
- Task-Based Concurrency – The use of a dedicated xTask for LED blinking indicates a separation where time-critical or asynchronous tasks are decoupled from main processing.
- Configuration Driven – The firmware configuration via “idf.py menuconfig” and the project’s Kconfig entries (e.g., main/Kconfig.projbuild) indicate that many parameters (like main topic, WiFi credentials) are set at build time.

##### Relevant Technologies & Frameworks
- ESP-IDF (version 4.4) – The core development framework for ESP devices.
- C++ – For application logic, as indicated by file extensions and object-oriented design emphasis.
- MQTT – For messaging and IoT device communication.
- CMake/Makefile – As build configuration tools to support a modular build structure.
- FreeRTOS Tasks – For scheduling concurrent tasks like LED blinking.

## Diagram (Mermaid)

```mermaid
flowchart TD
    FirmwareCore["Firmware Core"] 
    WiFi["WiFi Connectivity"]
    MQTTClient["MQTT Client Module"]
    Sensor["Sensor Data Collection"]
    Peripheral["Peripheral Control"]
    Broker["MQTT Broker"]

    FirmwareCore -->|"initializes"| MQTTClient
    FirmwareCore -->|"establishes"| WiFi
    FirmwareCore -->|"manages"| Peripheral
    WiFi -->|"monitors"| Sensor
    Sensor -->|"sendsData"| MQTTClient
    WiFi -->|"supports"| MQTTClient
    MQTTClient -->|"updates"| WiFi
    MQTTClient -->|"publishesTo"| Broker
    Broker -->|"commands"| MQTTClient

    classDef firmware fill:#FFC0CB,stroke:#333,stroke-width:2px;
    classDef module fill:#87CEEB,stroke:#333,stroke-width:2px;
    classDef connectivity fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef sensor fill:#FFFF99,stroke:#333,stroke-width:2px;
    classDef peripheral fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef broker fill:#D3D3D3,stroke:#333,stroke-width:2px;

    class FirmwareCore firmware;
    class MQTTClient module;
    class WiFi connectivity;
    class Sensor sensor;
    class Peripheral peripheral;
    class Broker broker;

    click FirmwareCore "https://github.com/dino-rider/esp-idf-mqtt-device/blob/main/main/main.cpp"
    click FirmwareCore "https://github.com/dino-rider/esp-idf-mqtt-device/blob/main/main/IotDevice.cpp"
    click MQTTClient "https://github.com/dino-rider/esp-idf-mqtt-device/tree/main/components/esp_mqtt_cxx"
    click MQTTClient "https://github.com/dino-rider/esp-idf-mqtt-device/blob/main/main/MqttClient.cpp"
    click WiFi "https://github.com/dino-rider/esp-idf-mqtt-device/blob/main/main/Kconfig.projbuild"
    click Peripheral "https://github.com/dino-rider/esp-idf-mqtt-device/blob/main/main/Peripherals.cpp"
```
