# khoaphunsonac/assignment_iot_hk261 architecture

> Architecture diagram of the GitHub repository khoaphunsonac/assignment_iot_hk261, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/khoaphunsonac/assignment_iot_hk261
- Repository: https://github.com/khoaphunsonac/assignment_iot_hk261

## Overview

This is an IoT classroom monitoring and environmental-control system. The main path is Node A sensing and classifying conditions, ESP-NOW delivery to Node B, Node B control and MQTT telemetry, then the Python gateway’s CoreIOT and local-UI integrations. Separate capabilities include gateway-side AI/OTA, Node B web/LCD interfaces, and TinyML data collection and training. The README documents system-level links; source excerpts and index references support selected firmware links. Unsampled gateway and webserver implementation details are represented only at subsystem level.

Main entry points: [`App/IOT/gateway/bridge.py`](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/gateway/bridge.py) (CoreIOT bridge and OTA), [`App/IOT/firmware/node_a/src/main.cpp`](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_a/src/main.cpp) (Sampling tasks), [`App/IOT/firmware/node_b/src/net.cpp`](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/net.cpp) (Wi-Fi and MQTT), [`App/IOT/firmware/node_b/lib/node_b_logic/node_b_logic.cpp`](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/lib/node_b_logic/node_b_logic.cpp) (Telemetry and control logic)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_node_a["Node A sensing"]
  node_node_a_main["Sampling tasks<br/>[main.cpp]"]
  node_sensor_driver["Sensor acquisition<br/>[sensors.cpp]"]
  node_node_a_logic["Presence and TX logic<br/>[node_a_logic.cpp]"]
  node_tinyml_chip["TinyML inference<br/>[tinyml_model.h]"]
  node_espnow_link["ESP-NOW pairing and TX<br/>[espnow_link.cpp]"]
end

subgraph group_node_b["Node B control"]
  node_node_b_rx["ESP-NOW receive<br/>[espnow_rx.cpp]"]
  node_node_b_logic["Telemetry and control logic<br/>[node_b_logic.cpp]"]
  node_config_store[("Threshold storage<br/>[config_store.cpp]")]
  node_actuators["Fan and light control<br/>[control.cpp]"]
  node_mqtt_client["Wi-Fi and MQTT<br/>[net.cpp]"]
  node_node_b_web["Node B web interface<br/>[web.cpp]"]
  node_node_b_lcd["LCD display<br/>[lcd.cpp]"]
  node_node_b_ota["Device OTA updater<br/>[ota.cpp]"]
end

subgraph group_gateway["Gateway services"]
  node_gateway_bridge["CoreIOT bridge and OTA<br/>[bridge.py]"]
  node_local_ui["Local gateway UI<br/>[app.py]"]
  node_ai_gateway["Gateway event AI"]
end

subgraph group_ai["AI workflow"]
  node_capture_tools["CSV capture tools"]
  node_training["TinyML training and export<br/>[train_tinyml.py]"]
end

subgraph group_platform["CoreIOT platform"]
  node_coreiot["CoreIOT dashboard and rules"]
end

node_room(("Room sensors"))
node_packet_contract["ESP-NOW packet types<br/>[espnow_packet.h]"]
node_mqtt_broker(("Mosquitto broker"))

node_room -->|"provides readings"| node_sensor_driver
node_sensor_driver -->|"returns samples"| node_node_a_main
node_node_a_main -->|"checks samples"| node_node_a_logic
node_node_a_main -->|"pairs and sends"| node_espnow_link
node_node_a_main -.->|"runs inference"| node_tinyml_chip
node_node_a_main -->|"packs data"| node_packet_contract
node_espnow_link -->|"uses packet types"| node_packet_contract
node_espnow_link -->|"sends ESP-NOW"| node_node_b_rx
node_node_b_rx -.->|"validates packets"| node_packet_contract
node_node_b_rx -.->|"processes readings"| node_node_b_logic
node_node_b_logic -.->|"determines outputs"| node_actuators
node_config_store -->|"supplies thresholds"| node_node_b_logic
node_mqtt_client -->|"uses logic"| node_node_b_logic
node_mqtt_client -->|"publishes telemetry"| node_mqtt_broker
node_mqtt_broker -->|"forwards MQTT"| node_gateway_bridge
node_mqtt_client -->|"delivers OTA commands"| node_node_b_ota
node_node_b_ota -->|"queues status"| node_mqtt_client
node_gateway_bridge -->|"sends telemetry"| node_coreiot
node_coreiot -->|"returns rules and commands"| node_gateway_bridge
node_gateway_bridge -.->|"shares gateway data"| node_local_ui
node_gateway_bridge -.->|"runs event detection"| node_ai_gateway
node_gateway_bridge -->|"distributes firmware"| node_node_b_ota
node_node_b_web -.->|"exposes controls"| node_actuators
node_actuators -.->|"provides status"| node_node_b_lcd
node_mqtt_broker -->|"captures telemetry"| node_capture_tools
node_room -.->|"serial capture"| node_capture_tools
node_capture_tools -.->|"provides CSV data"| node_training
node_training -.->|"exports model"| node_tinyml_chip

click node_node_a_main "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_a/src/main.cpp"
click node_sensor_driver "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_a/src/sensors.cpp"
click node_node_a_logic "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_a/lib/node_a_logic/node_a_logic.cpp"
click node_tinyml_chip "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/ai/models/tinyml_model.h"
click node_espnow_link "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_a/src/espnow_link.cpp"
click node_packet_contract "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/common/espnow_packet.h"
click node_node_b_rx "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/espnow_rx.cpp"
click node_node_b_logic "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/lib/node_b_logic/node_b_logic.cpp"
click node_config_store "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/config_store.cpp"
click node_actuators "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/control.cpp"
click node_mqtt_client "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/net.cpp"
click node_node_b_web "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/web.cpp"
click node_node_b_lcd "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/lcd.cpp"
click node_node_b_ota "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/ota.cpp"
click node_gateway_bridge "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/gateway/bridge.py"
click node_local_ui "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/gateway/ui/app.py"
click node_coreiot "https://github.com/khoaphunsonac/assignment_iot_hk261/tree/main/App/IOT/coreiot"
click node_ai_gateway "https://github.com/khoaphunsonac/assignment_iot_hk261/tree/main/App/IOT/gateway"
click node_capture_tools "https://github.com/khoaphunsonac/assignment_iot_hk261/tree/main/App/IOT/ai/tools"
click node_training "https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/ai/train/train_tinyml.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_a_main,node_sensor_driver,node_node_a_logic,node_tinyml_chip,node_espnow_link toneBlue
class node_node_b_rx,node_node_b_logic,node_config_store,node_actuators,node_mqtt_client,node_node_b_web,node_node_b_lcd,node_node_b_ota toneAmber
class node_gateway_bridge,node_local_ui,node_ai_gateway toneMint
class node_capture_tools,node_training toneRose
class node_coreiot,node_room,node_mqtt_broker toneIndigo
class node_packet_contract toneTeal
```

## Components

### Node A sensing

- [Sampling tasks](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_a/src/main.cpp): `App/IOT/firmware/node_a/src/main.cpp`
- [Sensor acquisition](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_a/src/sensors.cpp): `App/IOT/firmware/node_a/src/sensors.cpp`
- [Presence and TX logic](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_a/lib/node_a_logic/node_a_logic.cpp): `App/IOT/firmware/node_a/lib/node_a_logic/node_a_logic.cpp`
- [TinyML inference](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/ai/models/tinyml_model.h): `App/IOT/ai/models/tinyml_model.h`
- [ESP-NOW pairing and TX](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_a/src/espnow_link.cpp): `App/IOT/firmware/node_a/src/espnow_link.cpp`

### Node B control

- [ESP-NOW receive](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/espnow_rx.cpp): `App/IOT/firmware/node_b/src/espnow_rx.cpp`
- [Telemetry and control logic](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/lib/node_b_logic/node_b_logic.cpp): `App/IOT/firmware/node_b/lib/node_b_logic/node_b_logic.cpp`
- [Threshold storage](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/config_store.cpp): `App/IOT/firmware/node_b/src/config_store.cpp`
- [Fan and light control](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/control.cpp): `App/IOT/firmware/node_b/src/control.cpp`
- [Wi-Fi and MQTT](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/net.cpp): `App/IOT/firmware/node_b/src/net.cpp`
- [Node B web interface](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/web.cpp): `App/IOT/firmware/node_b/src/web.cpp`
- [LCD display](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/lcd.cpp): `App/IOT/firmware/node_b/src/lcd.cpp`
- [Device OTA updater](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/node_b/src/ota.cpp): `App/IOT/firmware/node_b/src/ota.cpp`

### Gateway services

- [CoreIOT bridge and OTA](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/gateway/bridge.py): `App/IOT/gateway/bridge.py`
- [Local gateway UI](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/gateway/ui/app.py): `App/IOT/gateway/ui/app.py`
- [Gateway event AI](https://github.com/khoaphunsonac/assignment_iot_hk261/tree/main/App/IOT/gateway): `App/IOT/gateway`

### AI workflow

- [CSV capture tools](https://github.com/khoaphunsonac/assignment_iot_hk261/tree/main/App/IOT/ai/tools): `App/IOT/ai/tools`
- [TinyML training and export](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/ai/train/train_tinyml.py): `App/IOT/ai/train/train_tinyml.py`

### CoreIOT platform

- [CoreIOT dashboard and rules](https://github.com/khoaphunsonac/assignment_iot_hk261/tree/main/App/IOT/coreiot): `App/IOT/coreiot`

### Other components

- **Room sensors**
- [ESP-NOW packet types](https://github.com/khoaphunsonac/assignment_iot_hk261/blob/main/App/IOT/firmware/common/espnow_packet.h): `App/IOT/firmware/common/espnow_packet.h`
- **Mosquitto broker**

## Connections

- Room sensors → Sensor acquisition: provides readings
- Sensor acquisition → Sampling tasks: returns samples (evidence: `App/IOT/firmware/node_a/src/main.cpp`)
- Sampling tasks → Presence and TX logic: checks samples (evidence: `App/IOT/firmware/node_a/src/main.cpp`)
- Sampling tasks → ESP-NOW pairing and TX: pairs and sends (evidence: `App/IOT/firmware/node_a/src/main.cpp`)
- Sampling tasks → TinyML inference: runs inference
- Sampling tasks → ESP-NOW packet types: packs data (evidence: `App/IOT/firmware/node_a/src/main.cpp`)
- ESP-NOW pairing and TX → ESP-NOW packet types: uses packet types
- ESP-NOW pairing and TX → ESP-NOW receive: sends ESP-NOW
- ESP-NOW receive → ESP-NOW packet types: validates packets
- ESP-NOW receive → Telemetry and control logic: processes readings
- Telemetry and control logic → Fan and light control: determines outputs
- Threshold storage → Telemetry and control logic: supplies thresholds
- Wi-Fi and MQTT → Telemetry and control logic: uses logic (evidence: `App/IOT/firmware/node_b/src/net.cpp`)
- Wi-Fi and MQTT → Mosquitto broker: publishes telemetry
- Mosquitto broker → CoreIOT bridge and OTA: forwards MQTT
- Wi-Fi and MQTT → Device OTA updater: delivers OTA commands
- Device OTA updater → Wi-Fi and MQTT: queues status (evidence: `App/IOT/firmware/node_b/src/ota.cpp`)
- CoreIOT bridge and OTA → CoreIOT dashboard and rules: sends telemetry
- CoreIOT dashboard and rules → CoreIOT bridge and OTA: returns rules and commands
- CoreIOT bridge and OTA → Local gateway UI: shares gateway data
- CoreIOT bridge and OTA → Gateway event AI: runs event detection
- CoreIOT bridge and OTA → Device OTA updater: distributes firmware
- Node B web interface → Fan and light control: exposes controls
- Fan and light control → LCD display: provides status
- Mosquitto broker → CSV capture tools: captures telemetry (evidence: `App/IOT/ai/tools/mqtt_to_csv.py`)
- Room sensors → CSV capture tools: serial capture
- CSV capture tools → TinyML training and export: provides CSV data
- TinyML training and export → TinyML inference: exports model
