# priess0503/wxmqtt architecture

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

- Interactive diagram: https://gitdiagram.com/priess0503/wxmqtt
- Repository: https://github.com/priess0503/wxmqtt

## Overview

##### Project type and purpose
- This is a WeChat Mini Program (front-end client) for IoT device remote control and monitoring, built on the MQTT protocol.
- It supports multiple sensor/actuator types (temperature, humidity, switches, motors, WS2812b LED strips, soil, water quality, wind speed) and can integrate with Home Assistant.

##### File structure
- Top-level:
- app.js / app.json / app.wxss        – global app configuration & styles
- pages/…                             – each page is an MVC triad: .wxml (view), .wxss (styles), .js (logic), .json (config)
- utils/mqtt.js                       – MQTT client wrapper (connect, subscribe, publish)
- utils/util.js, utils/wxcharts.js    – helpers (formatting, chart rendering)
- images/, package-*, node_modules/   – assets & dependencies
- project.config*.json, jsconfig.json – IDE/build configs
- No backend code here: the Mini Program directly uses wss:// to connect to an external MQTT broker.

##### README for additional insights
- Describes supported device data formats (JSON payloads, command topics).
- Mentions prerequisites: WeChat appid, custom MQTT server over wss, domain备案.
- Shows sample UI flows and Home Assistant sync.

##### Main components
a. WeChat Mini Program (Client)
- UI Layer (pages/index, pages/device, pages/logs, pages/mqtt, pages/view)
- Business Logic (page controllers, util.js)
- MQTT Module (utils/mqtt.js)
b. External MQTT Broker (wss://)
c. IoT Devices
- Sensors (temp/humidity, water, soil, wind)
- Actuators (relays, motors, WS2812b LEDs, servos)
d. Third-Party Integration – Home Assistant (optional subscriber/publisher)

##### Relationships and interactions
- The client’s MQTT Module establishes a secure WebSocket (wss://) connection to the MQTT Broker.
- UI pages publish commands to device-specific topics; devices subscribe to those topics and act.
- Devices publish telemetry to topics; client subscribes and updates the UI (charts, logs).
- Home Assistant also subscribes/publishes to the same broker for cross-integration.

##### Architectural patterns & design principles
- MVC pattern in each Mini Program page (View: .wxml/.wxss, Controller: .js, Model: JSON data)
- Single responsibility: utils/mqtt.js encapsulates all MQTT logic; util.js for data formatting; wxcharts.js for visualization.
- Event-driven & pub/sub via MQTT topics.

##### Relevant technologies & libraries
- WeChat Mini Program framework (WXML, WXSS, JS)
- MQTT over Secure WebSockets (wss://)
- wxCharts for charting
- JSON for configuration & payloads

## Diagram (Mermaid)

```mermaid
graph TB
    %% WeChat Mini Program Client
    subgraph "WeChat Mini Program" 
        subgraph "UI Layer" 
            Home["Home Page"]:::client
            Device["Device Detail Page"]:::client
            Logs["Logs Page"]:::client
            MQTTDebug["MQTT Debug Page"]:::client
            View["Chart/View Page"]:::client
        end
        subgraph "Business Logic"
            Util["Data Formatting\n(utils/util.js)"]:::client
            WXCharts["Chart Rendering\n(utils/wxcharts.js)"]:::client
        end
        MQTTMod["MQTT Module\n(utils/mqtt.js)"]:::client
        AppConf["App Config\n(app.js, app.json, app.wxss)"]:::client
        Assets["Static Assets\n(images/)"]:::client
        Configs["Project Configs\n(jsconfig.json, ...)"]:::client
        Deps["Dependencies\n(package-lock.json, node_modules/)"]:::client
    end

    %% External Services
    Broker["MQTT Broker\n(wss://)"]:::external
    HA["Home Assistant"]:::external

    %% IoT Devices
    subgraph "IoT Devices" 
        Sensors["Sensors\n(temp, humidity, water, soil, wind)"]:::device
        Actuators["Actuators\n(relays, motors, LEDs, servos)"]:::device
    end

    %% Interactions
    Home -->|uses| Util
    Home -->|renders| WXCharts
    Device -->|uses| Util
    Logs -->|uses| Util
    View -->|renders| WXCharts
    MQTTDebug -->|uses| MQTTMod
    Util -->|formats| MQTTMod
    WXCharts -->|charts| MQTTMod

    %% MQTT flows
    MQTTMod -->|"CONNECT\nwss://"| Broker
    MQTTMod -->|"PUBLISH cmd\ntopic/device/+/cmd"| Broker
    Broker -->|"MESSAGE data\ntopic/device/+/data"| MQTTMod

    Broker <-->|"PUB/SUB"| Sensors
    Broker <-->|"PUB/SUB"| Actuators
    Broker <-->|"PUB/SUB"| HA

    %% Configuration and assets
    AppConf -->|configures| Home
    AppConf -->|configures| Device
    AppConf -->|configures| Logs
    Assets -.-> Home
    Assets -.-> Device
    Assets -.-> Logs

    Configs -->|IDE/build| AppConf
    Deps -->|provides libs| AppConf

    %% Click Events
    click Home "https://github.com/priess0503/wxmqtt/tree/master/pages/index/"
    click Device "https://github.com/priess0503/wxmqtt/tree/master/pages/device/"
    click Logs "https://github.com/priess0503/wxmqtt/tree/master/pages/logs/"
    click MQTTDebug "https://github.com/priess0503/wxmqtt/tree/master/pages/mqtt/"
    click View "https://github.com/priess0503/wxmqtt/tree/master/pages/view/"
    click MQTTMod "https://github.com/priess0503/wxmqtt/blob/master/utils/mqtt.js"
    click Util "https://github.com/priess0503/wxmqtt/blob/master/utils/util.js"
    click WXCharts "https://github.com/priess0503/wxmqtt/blob/master/utils/wxcharts.js"
    click Assets "https://github.com/priess0503/wxmqtt/tree/master/images/"
    click AppConf "https://github.com/priess0503/wxmqtt/blob/master/app.js"
    click Configs "https://github.com/priess0503/wxmqtt/blob/master/jsconfig.json"
    click Deps "https://github.com/priess0503/wxmqtt/blob/master/package-lock.json"

    %% Styles
    classDef client fill:#E3F2FD,stroke:#2196F3,color:#0D47A1
    classDef external fill:#E8F5E9,stroke:#4CAF50,color:#1B5E20
    classDef device fill:#FFF3E0,stroke:#FF9800,color:#E65100
```
