How kingfrl/p_pir_relay works
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: Network.cpp Network service · platformio.ini PlatformIO project · main.cpp Device lifecycle · Storage.cpp Persistent storage
Components 11
Firmware
- Device lifecycle · Initializes subsystems and coordinates recurring device behavior.
src/main.cpp - Firmware settings · Compile-time constants shared across firmware subsystems.
src/Config.h - Hardware I/O · Encapsulates PIR input, relay output, and board I/O setup.
src/Hardware.cpp - Network service · Establishes connectivity and exposes the device-facing web interface.
src/Network.cpp - Persistent storage · Retains device configuration and runtime state.
src/Storage.cpp - Astronomical timing · Provides time-based astronomical switching decisions.
src/Astro.cpp
On-device web UI
- Status page
data/index.html - Setup page
data/setup.html - Setup progress page
data/wait.html - Setup completion page
data/success.html
Build & deployment
- PlatformIO project · Defines board, framework, build, and upload targets.
platformio.ini
Connections 16
- 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