# kingfrl/p_pir_relay architecture

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

- Interactive diagram: https://gitdiagram.com/kingfrl/p_pir_relay
- Repository: https://github.com/kingfrl/p_pir_relay

## Overview

#### Purpose and execution model
This is a PlatformIO-based embedded firmware project, likely for a PIR-triggered relay device with local web-based setup/status pages. Firmware starts from `src/main.cpp`, coordinates hardware, networking, persistence, and astronomical/time-based behavior, then serves static UI content from `data/`.

#### Application orchestration
`src/main.cpp` is the primary control-flow entry point. It composes the subsystem interfaces and owns device lifecycle behavior: initialization, recurring processing, and cross-component decisions.

Key boundaries:
- Device/application orchestration: `src/main.cpp`
- Shared firmware settings and compile-time constants: `src/Config.h`
- Platform build/runtime definition: `platformio.ini`

#### Hardware and relay domain
Hardware-specific access is isolated behind the Hardware component. This is the boundary for PIR input, relay output, and any board-level I/O initialization rather than exposing pin-level behavior throughout the application.

Primary paths:
- `src/Hardware.h`
- `src/Hardware.cpp`
- `src/Config.h`

This separation allows application logic in `main.cpp` to make relay decisions without directly coupling to the underlying embedded GPIO implementation.

#### Network and device-facing HTTP flow
Networking is encapsulated in the Network component, which likely establishes connectivity and exposes the device’s setup/status interface. The browser-facing assets are stored separately under `data/`, consistent with deployment to an embedded filesystem by the PlatformIO target.

Primary paths:
- `src/Network.h`
- `src/Network.cpp`
- `data/index.html`

The UI flow appears to include setup, waiting/progress, and successful-completion states:
- `data/setup.html`
- `data/wait.html`
- `data/success.html`

#### Persistent storage boundary
`Storage` provides a dedicated persistence layer for device configuration or runtime state. Other subsystems should use this API rather than depend on the physical storage mechanism directly.

Primary paths:
- `src/Storage.h`
- `src/Storage.cpp`
- `src/Config.h`

This component is the likely bridge between web/setup input, retained device settings, and boot-time behavior.

#### Time and astronomical decision support
The Astro component isolates astronomical calculations, presumably supporting time-of-day or sunrise/sunset-based relay behavior. It forms a domain boundary between raw clock/network time inputs and higher-level switching policy.

Primary paths:
- `src/Astro.h`
- `src/Astro.cpp`
- `src/main.cpp`

Control flow likely combines motion/relay signals from Hardware with persisted settings and Astro-derived timing conditions.

#### Build, deployment, and runtime dependencies
The project uses PlatformIO as its embedded build and upload environment, defined by `platformio.ini`. The presence of `data/` indicates a second deployment artifact: static filesystem assets in addition to compiled firmware.

Architecturally material runtime areas are:
- Embedded firmware runtime: `src/main.cpp`
- Board/framework and upload configuration: `platformio.ini`
- On-device static web content: `data/`
- Network service integration: `src/Network.cpp`

Main entry points: [`src/Network.cpp`](https://github.com/kingfrl/p_pir_relay/blob/main/src/Network.cpp) (Network service), [`platformio.ini`](https://github.com/kingfrl/p_pir_relay/blob/main/platformio.ini) (PlatformIO project), [`src/main.cpp`](https://github.com/kingfrl/p_pir_relay/blob/main/src/main.cpp) (Device lifecycle), [`src/Storage.cpp`](https://github.com/kingfrl/p_pir_relay/blob/main/src/Storage.cpp) (Persistent storage)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_firmware["Firmware"]
  node_main["Device lifecycle<br/>application entry point<br/>[main.cpp]"]
  node_config["Firmware settings<br/>shared configuration<br/>[Config.h]"]
  node_hardware{{"Hardware I/O<br/>GPIO boundary<br/>[Hardware.cpp]"}}
  node_network["Network service<br/>connectivity and HTTP<br/>[Network.cpp]"]
  node_storage[("Persistent storage<br/>persistence API<br/>[Storage.cpp]")]
  node_astro["Astronomical timing<br/>domain service<br/>[Astro.cpp]"]
end

subgraph group_web["On-device web UI"]
  node_index["Status page<br/>static UI asset<br/>[index.html]"]
  node_setup["Setup page<br/>static UI asset<br/>[setup.html]"]
  node_wait["Setup progress page<br/>static UI asset<br/>[wait.html]"]
  node_success["Setup completion page<br/>static UI asset<br/>[success.html]"]
end

subgraph group_deployment["Build &amp; deployment"]
  node_platformio["PlatformIO project<br/>build definition<br/>[platformio.ini]"]
end

node_platformio -->|"builds firmware"| node_main
node_platformio -->|"deploys filesystem assets"| node_index
node_platformio -->|"deploys filesystem assets"| node_setup
node_platformio -->|"deploys filesystem assets"| node_wait
node_platformio -->|"deploys filesystem assets"| node_success
node_main -->|"reads motion and controls relay"| node_hardware
node_main -->|"runs device web service"| node_network
node_main -->|"loads and updates settings"| node_storage
node_main -->|"uses timing conditions"| node_astro
node_config -.->|"I/O settings"| node_hardware
node_config -.->|"storage settings"| node_storage
node_network -->|"persists setup input"| node_storage
node_network -->|"serves"| node_index
node_network -->|"serves"| node_setup
node_network -->|"serves"| node_wait
node_network -->|"serves"| node_success

click node_platformio "https://github.com/kingfrl/p_pir_relay/blob/main/platformio.ini"
click node_main "https://github.com/kingfrl/p_pir_relay/blob/main/src/main.cpp"
click node_config "https://github.com/kingfrl/p_pir_relay/blob/main/src/Config.h"
click node_hardware "https://github.com/kingfrl/p_pir_relay/blob/main/src/Hardware.cpp"
click node_network "https://github.com/kingfrl/p_pir_relay/blob/main/src/Network.cpp"
click node_storage "https://github.com/kingfrl/p_pir_relay/blob/main/src/Storage.cpp"
click node_astro "https://github.com/kingfrl/p_pir_relay/blob/main/src/Astro.cpp"
click node_index "https://github.com/kingfrl/p_pir_relay/blob/main/data/index.html"
click node_setup "https://github.com/kingfrl/p_pir_relay/blob/main/data/setup.html"
click node_wait "https://github.com/kingfrl/p_pir_relay/blob/main/data/wait.html"
click node_success "https://github.com/kingfrl/p_pir_relay/blob/main/data/success.html"

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_main,node_config,node_hardware,node_network,node_storage,node_astro toneBlue
class node_index,node_setup,node_wait,node_success toneAmber
class node_platformio toneMint
```

## Components

### Firmware

- [Device lifecycle](https://github.com/kingfrl/p_pir_relay/blob/main/src/main.cpp): application entry point · Initializes subsystems and coordinates recurring device behavior. · `src/main.cpp`
- [Firmware settings](https://github.com/kingfrl/p_pir_relay/blob/main/src/Config.h): shared configuration · Compile-time constants shared across firmware subsystems. · `src/Config.h`
- [Hardware I/O](https://github.com/kingfrl/p_pir_relay/blob/main/src/Hardware.cpp): GPIO boundary · Encapsulates PIR input, relay output, and board I/O setup. · `src/Hardware.cpp`
- [Network service](https://github.com/kingfrl/p_pir_relay/blob/main/src/Network.cpp): connectivity and HTTP · Establishes connectivity and exposes the device-facing web interface. · `src/Network.cpp`
- [Persistent storage](https://github.com/kingfrl/p_pir_relay/blob/main/src/Storage.cpp): persistence API · Retains device configuration and runtime state. · `src/Storage.cpp`
- [Astronomical timing](https://github.com/kingfrl/p_pir_relay/blob/main/src/Astro.cpp): domain service · Provides time-based astronomical switching decisions. · `src/Astro.cpp`

### On-device web UI

- [Status page](https://github.com/kingfrl/p_pir_relay/blob/main/data/index.html): static UI asset · `data/index.html`
- [Setup page](https://github.com/kingfrl/p_pir_relay/blob/main/data/setup.html): static UI asset · `data/setup.html`
- [Setup progress page](https://github.com/kingfrl/p_pir_relay/blob/main/data/wait.html): static UI asset · `data/wait.html`
- [Setup completion page](https://github.com/kingfrl/p_pir_relay/blob/main/data/success.html): static UI asset · `data/success.html`

### Build & deployment

- [PlatformIO project](https://github.com/kingfrl/p_pir_relay/blob/main/platformio.ini): build definition · Defines board, framework, build, and upload targets. · `platformio.ini`

## Connections

- PlatformIO project → Device lifecycle: builds firmware
- PlatformIO project → Status page: deploys filesystem assets
- PlatformIO project → Setup page: deploys filesystem assets
- PlatformIO project → Setup progress page: deploys filesystem assets
- PlatformIO project → Setup completion page: deploys filesystem assets
- Device lifecycle → Hardware I/O: reads motion and controls relay
- Device lifecycle → Network service: runs device web service
- Device lifecycle → Persistent storage: loads and updates settings
- Device lifecycle → Astronomical timing: uses timing conditions
- Firmware settings → Hardware I/O: I/O settings
- Firmware settings → Persistent storage: storage settings
- Network service → Persistent storage: persists setup input
- Network service → Status page: serves
- Network service → Setup page: serves
- Network service → Setup progress page: serves
- Network service → Setup completion page: serves
