Source Layout
The FIRMWARE
PlatformIO-based ESP32 application in `firmware/` — connects to Wi-Fi, syncs time, and publishes MQTT telemetry and events. application lives in the demo repository
esp32-aws-iot-demo — source repository for firmware, provisioning scripts, Terraform, and dashboard code. Commands run from its root unless stated otherwise. under firmware/, not in this walkthrough site. Browse the source on GitHub: esp32-aws-iot-demo/firmware.
For module behavior details, see Architecture and Modules.
Directory tree
Section titled “Directory tree”firmware/├── platformio.ini├── include/│ ├── config.example.h # template; generated → config.h│ └── certs.example.h # template; generated → certs.h├── src/│ ├── main.cpp # entry point│ ├── wifi_manager.*│ ├── ntp_sync.*│ ├── mqtt_manager.*│ ├── telemetry_publisher.*│ ├── event_publisher.*│ ├── status_led.*│ ├── watchdog_supervisor.*│ ├── payload_codec.h│ ├── logger.h│ └── *_utils.h # shared helpers└── test/ # native unit tests (optional; not part of walkthrough)Entry point (main.cpp)
Section titled “Entry point (main.cpp)”firmware/src/main.cpp orchestrates the runtime.
Compile-time guards — the build fails fast if generated headers are missing:
config.h— run Device Provisioning (generate-headers.shwithWIFI_SSID,WIFI_PASSWORD,THING_NAME)certs.h— same flow after./aws/provision.shandgenerate-headers.sh
setup() init order:
Logger— serial at 115200 baudWatchdogSupervisor— task WDT (30 s)WiFiManager— connect using credentials fromconfig.hNTPSync— SNTP server fromconfig.hMQTTManager— TLS endpoint, thing name, and PEM material fromcerts.hTelemetryPublisher— publish interval fromconfig.hEventPublisher— GPIO0 BOOT buttonStatusLed— onboard RGB
loop() tick (~10 ms):
watchdog.feed()wifi_manager.loop()— reconnect with backoff when neededntp_sync.loop()— when Wi-Fi is up and time not yet syncedmqtt_manager.loop()— maintain TLS MQTT
Message Queuing Telemetry Transport — lightweight publish/subscribe protocol used between ESP32 and AWS IoT Core. sessiontelemetry_publisher.loop()— periodic connectivity publishevent_publisher.loop()— debounced button eventswatchdog.checkConnectivityTimeout()— reset if Wi-Fi + MQTT stallstatus_led.loop()— RGB animation
File reference
Section titled “File reference”| Path | Purpose | Used by |
|---|---|---|
src/main.cpp | Orchestrates setup and loop | Entry |
src/wifi_manager.* | Wi-Fi connect and exponential backoff | main |
src/ntp_sync.* | SNTP sync; |
|
src/mqtt_manager.* | TLS MQTT to IoT Core |
|
src/telemetry_publisher.* | 60 s connectivity payload | main |
src/event_publisher.* | GPIO0 BOOT button, debounced | main |
src/status_led.* | Blue RGB flash on successful publish | telemetry |
src/watchdog_supervisor.* | Task WDT and connectivity timeout reset | main |
src/payload_codec.h |
| publishers |
src/logger.h |
| all modules |
src/backoff_utils.h | Shared exponential backoff math | WiFiManager |
src/debounce_utils.h | Button debounce | EventPublisher |
src/connectivity_timeout_utils.h | Dual-connect timeout logic | WatchdogSupervisor |
src/interval_utils.h | Publish interval timing | TelemetryPublisher |
include/config.h | Wi-Fi, thing name, endpoint, interval (generated) |
|
include/certs.h | PEM certificate material (generated) | MQTTManager |
platformio.ini | Board env | PlatformIO PlatformIO — build, upload, and serial monitor toolchain (`pio`) for ESP32 firmware. |