# th-luan05/master architecture

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

- Interactive diagram: https://gitdiagram.com/th-luan05/master
- Repository: https://github.com/th-luan05/master

## Overview

This repository implements an ESP32 smart-home master that connects MQTT controls to nearby devices over ESP-NOW or UDP, reports device state back to MQTT, and supports configuration, safety, automation, provisioning, and firmware updates. The runtime is concentrated in main/main.c, with main/proto.h defining shared packet and provisioning formats. The excerpts show startup, parts of command and rule handling, and OTA behavior; other links between subsystems are omitted where their call paths were not shown.

Main entry points: [`main/main.c`](https://github.com/th-luan05/master/blob/main/main/main.c) (ESP-NOW / UDP), [`main/proto.h`](https://github.com/th-luan05/master/blob/main/main/proto.h) (Packet protocol)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_connectivity["Connectivity"]
  node_mqtt["MQTT client<br/>[main.c]"]
  node_dispatch["MQTT command routing<br/>[main.c]"]
  node_protocol["Packet protocol<br/>[proto.h]"]
  node_transport["ESP-NOW / UDP<br/>[main.c]"]
end

subgraph group_device["Device control"]
  node_telemetry["Telemetry and alerts<br/>[main.c]"]
  node_configuration["Node configuration<br/>[main.c]"]
  node_safety["Thermal safety<br/>[main.c]"]
end

subgraph group_automation["Automation"]
  node_rules["Automation rules<br/>[main.c]"]
  node_rule_store[("Rule persistence<br/>[main.c]")]
  node_time["Time service<br/>[main.c]"]
end

subgraph group_lifecycle["Device lifecycle"]
  node_ble["BLE provisioning<br/>[main.c]"]
  node_ota["Firmware updates<br/>[main.c]"]
end

node_controller(("Home controller"))
node_master["ESP32 master"]
node_device_nodes["Smart-home nodes"]
node_update_service["Update service"]

node_controller -.->|"publishes commands"| node_mqtt
node_mqtt -.->|"routes messages"| node_dispatch
node_dispatch -.->|"sends device commands"| node_transport
node_transport -.->|"forwards packets"| node_device_nodes
node_device_nodes -.->|"returns packets"| node_transport
node_transport -.->|"delivers telemetry"| node_telemetry
node_telemetry -.->|"publishes state"| node_mqtt
node_master -.->|"uses packet formats"| node_protocol
node_device_nodes -.->|"shares packet formats"| node_protocol
node_master -->|"starts transport"| node_transport
node_master -->|"starts time service"| node_time
node_master -->|"starts automation"| node_rules
node_master -->|"starts client"| node_mqtt
node_dispatch -->|"handles config"| node_configuration
node_dispatch -->|"handles rule updates"| node_rules
node_rules -->|"loads and saves"| node_rule_store
node_rules -->|"publishes retained rules"| node_mqtt
node_rules -.->|"controls nodes"| node_transport
node_device_nodes -.->|"provisions over BLE"| node_ble
node_dispatch -.->|"handles update requests"| node_ota
node_ota -->|"checks and downloads"| node_update_service
node_ota -->|"publishes update status"| node_mqtt
node_ota -.->|"sends node update status"| node_transport

click node_mqtt "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_dispatch "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_protocol "https://github.com/th-luan05/master/blob/main/main/proto.h"
click node_transport "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_telemetry "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_configuration "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_safety "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_rules "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_rule_store "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_time "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_ble "https://github.com/th-luan05/master/blob/main/main/main.c"
click node_ota "https://github.com/th-luan05/master/blob/main/main/main.c"

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_mqtt,node_dispatch,node_protocol,node_transport toneBlue
class node_telemetry,node_configuration,node_safety toneAmber
class node_rules,node_rule_store,node_time toneMint
class node_ble,node_ota toneRose
class node_controller,node_master,node_device_nodes,node_update_service toneIndigo
```

## Components

### Connectivity

- [MQTT client](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`
- [MQTT command routing](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`
- [Packet protocol](https://github.com/th-luan05/master/blob/main/main/proto.h): `main/proto.h`
- [ESP-NOW / UDP](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`

### Device control

- [Telemetry and alerts](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`
- [Node configuration](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`
- [Thermal safety](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`

### Automation

- [Automation rules](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`
- [Rule persistence](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`
- [Time service](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`

### Device lifecycle

- [BLE provisioning](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`
- [Firmware updates](https://github.com/th-luan05/master/blob/main/main/main.c): `main/main.c`

### Other components

- **Home controller**
- **ESP32 master**
- **Smart-home nodes**
- **Update service**

## Connections

- Home controller → MQTT client: publishes commands
- MQTT client → MQTT command routing: routes messages (evidence: `main/main.c`)
- MQTT command routing → ESP-NOW / UDP: sends device commands (evidence: `main/main.c`)
- ESP-NOW / UDP → Smart-home nodes: forwards packets
- Smart-home nodes → ESP-NOW / UDP: returns packets
- ESP-NOW / UDP → Telemetry and alerts: delivers telemetry (evidence: `main/main.c`)
- Telemetry and alerts → MQTT client: publishes state (evidence: `main/main.c`)
- ESP32 master → Packet protocol: uses packet formats
- Smart-home nodes → Packet protocol: shares packet formats
- ESP32 master → ESP-NOW / UDP: starts transport (evidence: `main/main.c`)
- ESP32 master → Time service: starts time service (evidence: `main/main.c`)
- ESP32 master → Automation rules: starts automation (evidence: `main/main.c`)
- ESP32 master → MQTT client: starts client (evidence: `main/main.c`)
- MQTT command routing → Node configuration: handles config (evidence: `main/main.c`)
- MQTT command routing → Automation rules: handles rule updates (evidence: `main/main.c`)
- Automation rules → Rule persistence: loads and saves (evidence: `main/main.c`)
- Automation rules → MQTT client: publishes retained rules (evidence: `main/main.c`)
- Automation rules → ESP-NOW / UDP: controls nodes (evidence: `main/main.c`)
- Smart-home nodes → BLE provisioning: provisions over BLE
- MQTT command routing → Firmware updates: handles update requests (evidence: `main/main.c`)
- Firmware updates → Update service: checks and downloads (evidence: `main/main.c`)
- Firmware updates → MQTT client: publishes update status (evidence: `main/main.c`)
- Firmware updates → ESP-NOW / UDP: sends node update status (evidence: `main/main.c`)
