# vulptex1234/esp32-networksys architecture

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

- Interactive diagram: https://gitdiagram.com/vulptex1234/esp32-networksys
- Repository: https://github.com/vulptex1234/esp32-networksys

## Overview

The project is a Python-based system meant to run on an ESP32 (as suggested by the README’s “Import this folder to your esp32” note) and appears to implement a clustering protocol where each node can be either a cluster head (CH) or a cluster member (CM). In addition, there’s a monitoring component (CM_monitor) and a set of functionalities for data collection and calculation (e.g., normalization and Euclidean distance computation) provided by calc.py. The structure is split across multiple folders (CH, CM, CM_monitor, and integrated) that mirror similar code but may be used for different node roles or integrated testing.

##### Main Components Identification
- Boot Module: Every folder has a boot.py which initializes the device. This module covers:
- Establishing a Wi-Fi connection (via connect_*_wifi() after performing a Wi-Fi scan).
- Deciding the node’s role by reading from flag.txt (the flag function chooses whether to launch ch_main.py or cm_main.py).
- Cluster Head Components (CH):
- The CH folder includes boot.py, calc.py (for data processing and sim_score calculation), and ch_main.py which likely manages operations when the node is a cluster head.
- Cluster Member Components (CM and CM_monitor):
- The CM folder contains boot.py and cm_main.py.
- The CM_monitor folder extends this by providing additional debugging, monitoring (get_current), and logging functionalities.
- Integrated Folder:
- Consolidates files from both CH and CM (including boot.py, calc.py, ch_main.py, cm_main.py, and several monitoring or data files). It acts as an all-in-one version for the ESP32 deployment, using files like ID.txt, cumulative_energy.txt, normalized_node_data.csv, and others to maintain node state and experiment data.
- Data Calculation Module (calc.py):
- normalize(): Reads battery and connectivity data from node_data.csv, normalizes these values, and writes to normalized_node_data.csv.
- extract_from_csv() and sim_score(): Processes normalized results to calculate a Euclidean distance which may contribute to role selection or performance evaluation.

##### Architectural Patterns & Design Principles
- Modularity: Each folder (CH, CM, CM_monitor, integrated) encapsulates a role with its specific set of functionalities.
- Data Flow: Emphasize the sequential data processing from reading raw sensor/node data to normalization and then to calculation of a similarity score.

##### Technologies and Frameworks
- Python is the primary language.
- The project runs on an ESP32 device (embedded target) – highlight the microcontroller environment.
- CSV files as primary data storage for node data and calculated results.

##### Final Advice
- Be very detailed—don’t hesitate to break down boot.py into its constituent responsibilities (network configuration, connection establishment, role decision).
- Dependency file interactions (like reading/writing CSVs, ID.txt, flag.txt) are shown so that it is clear how runtime configuration and state persist across sessions.
- Remember that the visual representation should map closely to the project’s physical file structure while clearly depicting the logical flow of control and data.

## Diagram (Mermaid)

```mermaid
flowchart TD
    PowerUp["Device Power Up"]:::core

    subgraph "Boot Process"
        NP["Network Configuration\n(using passwords.py)"]:::net
        FD["Flag Decision\n(reads flag.txt)"]:::config
    end
    PowerUp --> NP
    NP --> FD

    subgraph "Cluster Head Path"
        CHBoot["boot.py (CH)"]:::boot
        CHMain["ch_main.py (CH)"]:::main
        CHCalc["calc.py (CH - data processing)"]:::process
        NodeCSV["node_data.csv"]:::data
        NormCSV["normalized_node_data.csv"]:::data
    end
    FD -->|"CH"| CHBoot
    CHBoot --> CHMain
    CHMain --> CHCalc
    CHCalc -->|"reads"| NodeCSV
    CHCalc -->|"writes"| NormCSV

    subgraph "Cluster Member Path"
        CMBoot["boot.py (CM)"]:::boot
        CMMain["cm_main.py (CM)"]:::main
    end
    FD -->|"CM"| CMBoot
    CMBoot --> CMMain

    subgraph "CM Monitor"
        CMM_Boot["boot.py (CM_monitor)"]:::boot
        CMM_Main["cm_main.py (CM_monitor)"]:::main
        Debug["debug.py"]:::monitor
        GetCurrent["get_current.py"]:::monitor
        Logging["logging.py"]:::monitor
        Ina219["ina219.py"]:::monitor
    end
    FD -->|"CM_monitor"| CMM_Boot
    CMM_Boot --> CMM_Main
    CMM_Main --> Debug
    CMM_Main --> GetCurrent
    CMM_Main --> Logging
    CMM_Main --> Ina219

    subgraph "Integrated Deployment"
        IntBoot["boot.py (Integrated)"]:::boot
        IntCHMain["ch_main.py (Integrated)"]:::main
        IntCMMain["cm_main.py (Integrated)"]:::main
        IntCalc["calc.py (Integrated)"]:::process
        ID["ID.txt"]:::data
        CumEnergy["cumulative_energy.txt"]:::data
        IntNormCSV["normalized_node_data.csv (Integrated)"]:::data
        RemBat["remaining_battery.csv"]:::data
        IntGetCurrent["get_current.py (Integrated)"]:::monitor
        IntIna219["ina219.py (Integrated)"]:::monitor
        IntLogging["logging.py (Integrated)"]:::monitor
        IntInit["init.py (Integrated)"]:::aux
        IntDelete["delete.py (Integrated)"]:::aux
        TempRun["tempCodeRunnerFile.py"]:::aux
    end
    FD -->|"Integrated"| IntBoot
    IntBoot --> IntCHMain
    IntBoot --> IntCMMain
    IntBoot --> IntCalc
    IntCalc --> IntNormCSV
    IntCHMain --- IntCalc
    IntCMMain --- IntCalc
    IntBoot --> ID
    IntBoot --> CumEnergy
    IntBoot --> RemBat
    IntBoot --> IntGetCurrent
    IntBoot --> IntIna219
    IntBoot --> IntLogging
    IntBoot --> IntInit
    IntBoot --> IntDelete
    IntBoot --> TempRun

    click CHBoot "https://github.com/vulptex1234/esp32-networksys/blob/main/CH/boot.py"
    click CMBoot "https://github.com/vulptex1234/esp32-networksys/blob/main/CM/boot.py"
    click CMM_Boot "https://github.com/vulptex1234/esp32-networksys/blob/main/CM_monitor/boot.py"
    click IntBoot "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/boot.py"
    click CHMain "https://github.com/vulptex1234/esp32-networksys/blob/main/CH/ch_main.py"
    click CHCalc "https://github.com/vulptex1234/esp32-networksys/blob/main/CH/calc.py"
    click CMMain "https://github.com/vulptex1234/esp32-networksys/blob/main/CM/cm_main.py"
    click CMM_Main "https://github.com/vulptex1234/esp32-networksys/blob/main/CM_monitor/cm_main.py"
    click Debug "https://github.com/vulptex1234/esp32-networksys/blob/main/CM_monitor/debug.py"
    click GetCurrent "https://github.com/vulptex1234/esp32-networksys/blob/main/CM_monitor/get_current.py"
    click Logging "https://github.com/vulptex1234/esp32-networksys/blob/main/CM_monitor/logging.py"
    click Ina219 "https://github.com/vulptex1234/esp32-networksys/blob/main/CM_monitor/ina219.py"
    click IntCHMain "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/ch_main.py"
    click IntCMMain "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/cm_main.py"
    click IntCalc "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/calc.py"
    click ID "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/ID.txt"
    click CumEnergy "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/cumulative_energy.txt"
    click IntNormCSV "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/normalized_node_data.csv"
    click RemBat "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/remaining_battery.csv"
    click IntGetCurrent "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/get_current.py"
    click IntIna219 "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/ina219.py"
    click IntLogging "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/logging.py"
    click IntInit "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/init.py"
    click IntDelete "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/delete.py"
    click TempRun "https://github.com/vulptex1234/esp32-networksys/blob/main/integrated/tempCodeRunnerFile.py"

    classDef core fill:#f9f,stroke:#333,stroke-width:2px;
    classDef boot fill:#bbf,stroke:#333,stroke-width:2px;
    classDef net fill:#afa,stroke:#333,stroke-width:2px;
    classDef config fill:#fea,stroke:#333,stroke-width:2px;
    classDef main fill:#eef,stroke:#333,stroke-width:2px;
    classDef process fill:#cfc,stroke:#333,stroke-width:2px;
    classDef data fill:#fcc,stroke:#333,stroke-width:2px;
    classDef monitor fill:#fcf,stroke:#333,stroke-width:2px;
    classDef aux fill:#ddd,stroke:#333,stroke-width:2px;
```
