Fault-Tolerant ESP8266 Firmware with Real-Time PPG Heart Rate Sensing, Environmental Telemetry, Fail-Safe Offline Recovery & Blynk Cloud Synchronization
Overview • Security & Resilience • Hardware & Pinout • State Machine • Blynk Telemetry • Build & Flash
Smart Digital Clock with Health & Environmental Monitoring is an embedded IoT telemetry and edge-alerting system built on the ESP8266 NodeMCU (ESP-12E) microcontroller in Embedded C/C++ (1,053 LOC). Designed as the capstone engineering project for IOT102 at FPT University Ho Chi Minh City (Group 8 — Fall 2025), the system unifies precision timekeeping, optical photoplethysmography (PPG) biometric monitoring, ambient environmental sensing, non-volatile alarm persistence, and bidirectional Blynk Cloud synchronization into a single fault-tolerant edge node.
Beyond functional sensing, the firmware (v4.4 FIXED) is engineered with high-availability fail-safe recovery and defensive embedded programming practices:
- Zero-Downtime Standalone Fallback: Automatically detects Wi-Fi or Cloud degradation (
15shandshake timeout,30sheartbeat watchdog) and transitions into a deterministic Standalone Offline Mode (Mode 3) without blocking the real-time sensor polling or local alarm loops. - Hysteresis & Rate-Limited Anomaly Alerting: Suppresses transient optical/thermal sensor noise and prevents cloud event flooding via a 10-second sustained danger-zone window (
< 60 BPMor> 100 BPM) for heart rate warnings and a 30-second cooldown for high-temperature alerts (> 35.0°C). - Secret Isolation & Input Sanitization: Eliminates hardcoded credentials (
CWE-798) via compile-time header isolation (secrets.h), validates remote cloud command payloads against out-of-bounds reads (CWE-125), and sanitizes uninitialized flashEEPROMbytes on cold boot.
From an IoT Security & Embedded Systems Reliability perspective (CIA Triad: Confidentiality, Integrity, Availability), the firmware implements five core defensive controls:
| Security / Resilience Domain | Vulnerability / Failure Mode Mitigated | Implementation in SmartDigitalClock.ino |
|---|---|---|
| 1. Credential Isolation | CWE-798 (Use of Hard-coded Credentials) | Secrets (BLYNK_AUTH_TOKEN, WIFI_SSID, WIFI_PASSWORD) are decoupled from source control using #if __has_include("secrets.h") and .gitignore enforcement, with secrets.example.h provided as a template. |
| 2. Remote Input Bounds Checking | CWE-125 / CWE-129 (Out-of-Bounds Array Read / Improper Array Index Validation) | Cloud handler BLYNK_WRITE(V_SELECT_MODE) strictly validates 0 <= newMode <= 2 before indexing MODE_NAMES[newMode], rejecting malformed payloads and emitting an audit entry to Virtual Terminal V10. |
| 3. Flash EEPROM State Sanitization | Corrupted / Uninitialized Non-Volatile Memory (0xFF) |
loadAlarm() enforces range invariants (alarm.hour > 23 → fallback 07:00; alarm.minute > 59 → fallback 00) across the 512-byte flash-emulated EEPROM sector to prevent undefined alarm behavior after power loss. |
| 4. Optical & Thermal Signal Filtering | Sensor Saturation, NaN Propagation & Transient Spikes |
readHeartRate() enforces valid IR reflectance windows (50,000 < irValue < 200,000), detects sensor saturation (OVERLOAD!), filters physiological BPM (20 < bpm < 200) through a 4-sample sliding window (RATE_SIZE = 4), and guards DHT11 reads with !isnan(h) && !isnan(t). |
| 5. Telemetry Rate-Limiting & Fail-Safe Availability | Alert Fatigue, Cloud Quota DoS & Network Outage Hang | checkHealthWarnings() requires 10s continuous violation (HR_DANGER_DURATION = 10000ms) before firing Blynk.logEvent("health_warning") and applies a 30s thermal cooldown. checkWiFiStatus() gracefully locks the UI into full-telemetry standalone mode when offline. |
| System Block Diagram | Hardware Wiring Schematic (Cirkit Designer) |
|---|---|
![]() |
![]() |
The ESP8266 NodeMCU coordinates six hardware peripherals across I2C, 3-Wire Synchronous Serial, 1-Wire Digital, and GPIO buses:
| ESP8266 NodeMCU Pin | GPIO | Protocol / Bus | Connected Peripheral | Pin Function & Electrical Notes |
|---|---|---|---|---|
GND |
— | Common Ground | All Modules | Shared reference ground across all 6 peripherals |
3V3 |
— | Power Rail (3.3V) |
DS1302, MAX30102, DHT11 |
Regulated 3.3V logic & sensor supply |
VU (5V USB) |
— | Power Rail (5.0V) |
LCD 16x2 I2C (PCF8574) |
5V backlight & contrast supply for LCD1602 |
D1 |
GPIO5 |
I2C (SCL) |
LCD 16x2 I2C + MAX30102 |
Shared I2C Clock line (I2C_SPEED_STANDARD / 100 kHz) |
D2 |
GPIO4 |
I2C (SDA) |
LCD 16x2 I2C + MAX30102 |
Shared I2C Data line (LCD @ 0x27, MAX30102 @ 0x57) |
D3 |
GPIO0 |
1-Wire Digital | DHT11 Sensor |
Ambient Temperature (0–50°C) & Relative Humidity (20–90%) |
D4 |
GPIO2 |
3-Wire Serial | DS1302 RTC (CLK) |
Real-Time Clock serial clock input (battery-backed timekeeping) |
D5 |
GPIO14 |
3-Wire Serial | DS1302 RTC (DAT) |
Bidirectional RTC serial data line |
D8 |
GPIO15 |
3-Wire Serial | DS1302 RTC (RST) |
RTC Chip Enable / Reset strobe |
D6 |
GPIO12 |
Digital Input | Tactile Push Button | INPUT_PULLUP with 50ms software debounce (Short <1s / Long ≥1s) |
D7 |
GPIO13 |
Digital Output | Active Piezo Buzzer | Patterned acoustic alerts (Alarm, HR Danger 3-beep, Mode confirm) |
The firmware operates a non-blocking cooperative event loop (loop()) supporting 3 Display Modes and context-aware button gestures (50ms debounce, 1000ms short/long press discriminator):
| Connectivity State | Active Display Mode | LCD Line 1 (16 chars) |
LCD Line 2 (16 chars) |
Short Press (< 1.0s) |
Long Press (≥ 1.0s) |
|---|---|---|---|---|---|
| 🟢 ONLINE (Wi-Fi + Blynk Connected) | Mode 1 (0): Time + Temp |
HH:MM:SS AHH:MM 1 |
T:25.1C H:51% |
Cycle to next mode (0 → 1 → 2) & disable Auto-Switch |
Displays "Mode 2 only" hint |
| 🟢 ONLINE (Wi-Fi + Blynk Connected) | Mode 2 (1): Heart Rate |
IR:112k [M] 2 |
BPM:78 OK * |
Cycle to next mode (0 → 1 → 2) & disable Auto-Switch |
Toggle Acoustic Mute ([M]) with dual/single beep feedback |
| 🟢 ONLINE (Wi-Fi + Blynk Connected) | Mode 3 (2): Full Info |
HH:MM DD/MM 3 |
25.1C 51% 78BPM |
Cycle to next mode (0 → 1 → 2) & disable Auto-Switch |
Displays "Mode 2 only" hint |
| 🔴 OFFLINE (Fail-Safe Standalone) | Locked to Mode 3 (2) |
HH:MM DD/MM O3 (or [M]) |
25.1C 51% 78BPM |
Displays "OFFLINE Mode / Full Info Only" status notice |
Toggle Acoustic Mute ([M]) directly in standalone mode |
| ⏰ ALARM RINGING (Any State) | Priority Interrupt Screen | *** ALARM! *** |
Press button! |
Immediately silences alarm & logs source (Physical Button) |
Immediately silences alarm |
The firmware synchronizes 14 Virtual Pins (V0–V13) plus two cloud event channels (alarm_event and health_warning) with the Blynk IoT Mobile & Web Dashboard:
| Virtual Pin | Identifier | Direction | Data Type | Functional Description |
|---|---|---|---|---|
V0 |
V_TIME |
MCU → Cloud |
String |
Real-time formatted clock string (HH:MM:SS) from DS1302 |
V1 |
V_DATE |
MCU → Cloud |
String |
Formatted calendar date (DD/MM/YYYY) |
V2 |
V_TEMP |
MCU → Cloud |
Float |
Ambient temperature in °C (streamed every 3,000ms) |
V3 |
V_HUMIDITY |
MCU → Cloud |
Float |
Ambient relative humidity in % |
V4 |
V_HEARTRATE |
MCU → Cloud |
Integer |
Filtered heart rate in BPM (0 when finger removed > 2s) |
V5 |
V_ALARM_HOUR |
Cloud → MCU |
Int (0–23) |
Remote alarm hour configuration (persisted to EEPROM[0]) |
V6 |
V_ALARM_MIN |
Cloud → MCU |
Int (0–59) |
Remote alarm minute configuration (persisted to EEPROM[1]) |
V7 |
V_ALARM_EN |
Cloud → MCU |
Bool (0/1) |
Enable/disable alarm toggle (persisted to EEPROM[2]) |
V8 |
V_STOP_ALARM |
Cloud → MCU |
Bool (0/1) |
Remote alarm dismissal button (stopAlarmSound("Blynk App")) |
V9 |
V_STATUS |
MCU → Cloud |
String |
Live system health banner (🟢 Online | 🔔 Alarm | Mode) |
V10 |
V_TERMINAL |
MCU → Cloud |
String |
Timestamped audit & security event log terminal |
V11 |
V_AUTO_MODE |
Bidirectional |
Bool (0/1) |
Enable/disable 5,000ms automatic LCD mode rotation |
V12 |
V_SELECT_MODE |
Bidirectional |
Int (0–2) |
Direct display mode selector with bounds checking |
V13 |
V_NEXT_MODE |
Cloud → MCU |
Bool (0/1) |
Remote step button to cycle LCD display modes |
Smart_Digital_Clock_with_Arduino/
├── firmware/
│ ├── SmartDigitalClock/
│ │ ├── SmartDigitalClock.ino # Production v4.4 firmware (1,053 LOC, CWE-798 hardened)
│ │ └── secrets.example.h # Template for Blynk & Wi-Fi credentials (copy to secrets.h)
│ └── tests/ # Isolated hardware verification sketches
│ ├── test_DS1302_DHT11/
│ │ └── test_DS1302_DHT11.ino # Unit test for RTC timekeeping, I2C LCD & DHT11 sensing
│ └── test_MAX30102_BUZZER_BUTTON/
│ └── test_MAX30102_BUZZER_BUTTON.ino # Unit test for MAX30102 PPG IR beat detection & buzzer
├── docs/
│ ├── 25FA_3W_02_G8_IEEE_Final_Report.pdf # Full IEEE two-column technical report
│ ├── 25FA_3W_02_G8_Presentation_Slides.pdf # Capstone defense presentation slides
│ └── IEEE_Paper_LaTeX_Source.tex # Overleaf IEEEtran LaTeX manuscript source
├── assets/ # Schematics, flowcharts, dashboard & prototype photography
│ ├── circuit_schematic.png
│ ├── block_diagram.png
│ ├── system_flowchart.png
│ ├── pinout_table.png
│ ├── blynk_dashboard_main.jpg
│ ├── blynk_dashboard_controls.jpg
│ ├── blynk_event_log.jpg
│ ├── prototype_front.jpg
│ ├── prototype_internal.jpg
│ └── prototype_sensors.jpg
├── .gitattributes # Linguist C classification for Arduino .ino firmware
├── .gitignore # Prevents committing secrets.h and build binaries
├── LICENSE # MIT License
└── README.md
Install the ESP8266 Board Package (>= 3.1.2) in Arduino IDE or PlatformIO, along with the following verified libraries:
Blynkby Volodymyr Shymanskyy (>= 1.3.2)SparkFun MAX3010x Pulse and Proximity Sensor Library(MAX30105.h&heartRate.h)DHT sensor libraryby Adafruit +Adafruit Unified SensorLiquidCrystal I2Cby Frank de Brabander (0x27, 16x2)DS1302by Henning Karlsen
git clone https://github.com/phat0x8/Smart_Digital_Clock_with_Arduino.git
cd Smart_Digital_Clock_with_Arduino/firmware/SmartDigitalClock
cp secrets.example.h secrets.hEdit secrets.h (which is gitignored) with your own Blynk Template ID, Auth Token, and Wi-Fi credentials:
#define BLYNK_TEMPLATE_ID "TMPL6xxxxxxxx"
#define BLYNK_TEMPLATE_NAME "Smart Digital Clock"
#define BLYNK_AUTH_TOKEN "your_32_char_blynk_auth_token"
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"- (Optional) Run the isolated hardware unit tests in
firmware/tests/first to verifyI2Cbus addresses (0x27LCD,0x57MAX30102) andDS1302battery backup. - Open
firmware/SmartDigitalClock/SmartDigitalClock.inoin Arduino IDE, select boardNodeMCU 1.0 (ESP-12E Module), upload speed115200, and flash. - Open Serial Monitor at
115200 baudto inspect real-time boot diagnostics ([I2C],[DS1302],[MAX30102],[ALARM],[WIFI],[BLYNK]).
- 📑 Full IEEE Technical Paper:
docs/25FA_3W_02_G8_IEEE_Final_Report.pdf - 📊 Defense Presentation Deck:
docs/25FA_3W_02_G8_Presentation_Slides.pdf - 🎥 Hardware Prototype Video Demo: Google Drive Demonstration Video
| Role | Team Member | Student ID | Core Engineering Responsibilities | Contribution |
|---|---|---|---|---|
| Lead Firmware & Circuit Architect (1st Author) | Nguyen Tan Phat (@phat0x8) |
SE173285 |
Architected & programmed the 1,053-line ESP8266 C/C++ firmware, designed the circuit schematic & Blynk Cloud dashboard, and conducted code verification. | 36% |
| System Designer & Hardware Builder | Doan Thi Thu Kim | SE193122 |
Designed system block diagram & execution flowchart, assembled physical enclosure prototype, and co-verified firmware. | 34% |
| Technical Writer & Presenter | Le Anh Dung | SE150558 |
Authored IEEE report manuscript, prepared defense presentation slides, and conducted functional testing. | 30% |
Academic Advisor: Mr. Dang Ngoc Minh Duc (
ducdnm2@fe.edu.vn) — CourseIOT102(Class3W_02, Group 8), FPT University Ho Chi Minh Campus.
Tan Phat Nguyen (@phat0x8) — Information Security Engineer
🔗 GitHub Profile • LinkedIn





