Advanced IoT Security & Monitoring for smart homes — ESP8266 (NodeMCU) master + Arduino Mega slave.
This repository contains firmware, examples, and reference files for a small home automation/security solution combining:
- ESP8266 NodeMCU (master): Fingerprint access control, float water sensor, WiFi-to-Firebase sync, I2C master, buzzer
- Arduino Mega (slave): Relay control, SIM800L SMS alerts, I2C slave
- Features
- Hardware
- Wiring / Pinout
- Software Setup
- Firebase Realtime Database Structure
- Flashing / Build (PlatformIO)
- Troubleshooting
- Security & Git
- License
- Fingerprint-based door unlock with user names stored on the device
- Failed attempt counting and lockout after 3 failures (with buzzer)
- Water level monitoring with alerts
- Real-time synchronization to Firebase Realtime Database
- I2C master/slave communication between ESP8266 (master) and Mega (slave)
- SMS alerts from Mega using SIM800L
Minimum parts list:
- ESP8266 NodeMCU (or similar) x1
- Adafruit/GT-521F32 style fingerprint sensor (UART) x1
- Arduino Mega 2560 (for relays and SIM800L) x1
- SIM800L GSM module x1 (requires stable ~4V supply)
- 8-channel relay module (or custom relay board)
- Float water sensor (simple mechanical float switch)
- Active buzzer (active low in code)
- I2C wiring (SDA, SCL) and common ground between devices
- Power supplies as required (Mega, NodeMCU, SIM800L)
Below are the exact pins used by the firmware in this repo. When wiring, match the signal names (GPIO numbers) rather than NodeMCU silk labels to avoid confusion.
3.3V-> 3.3V power (fingerprint sensor, NodeMCU powered from stable 3.3V / USB)GND-> Common groundD1 (GPIO5)-> I2C SCL (Wire.begin(D2, D1) in code usesD2as SDA,D1as SCL)D2 (GPIO4)-> I2C SDAD7 (GPIO13)->BUZZER_PIN(active low; HIGH = off)D6 (GPIO12)-> Float sensor pinFLOAT_PIN(input withINPUT_PULLUP, LOW = water present)
Notes in code:
Wire.begin(D2, D1);— initializes I2C master using SDA=D2 (GPIO4) and SCL=D1 (GPIO5).#define FLOAT_PIN 12— float uses GPIO12 (NodeMCU D6).#define BUZZER_PIN 13— buzzer uses GPIO13 (NodeMCU D7).
The fingerprint management example (examples/manageFingerprint.cpp) expects the fingerprint sensor on SoftwareSerial:
Fingerprint TX -> NodeMCU RX(RX pin inSoftwareSerial mySerial(14, 12);is first arg = RX = D5/GPIO14)Fingerprint RX -> NodeMCU TX(TX pin inSoftwareSerial mySerial(14, 12);is second arg = TX = D6/GPIO12)
So wiring (Sensor -> NodeMCU):
Sensor TX->D5 (GPIO14)(NodeMCU RX for SoftwareSerial)Sensor RX->D6 (GPIO12)(NodeMCU TX for SoftwareSerial)Sensor VCC->3.3V(verify sensor spec; some sensors require 3.6V–5V tolerant)Sensor GND->GND
Important: The fingerprint sensor and NodeMCU must share a common ground. Check your sensor's voltage requirements — many sensors run at 5V while NodeMCU is 3.3V; the code here expects a 3.3V-compatible interface.
I2C SDA-> ESP8266SDA(NodeMCUD2/ GPIO4)I2C SCL-> ESP8266SCL(NodeMCUD1/ GPIO5)GND-> Common ground with ESP8266
Mega hardware serial for SIM800L (as used in examples):
SIM800L TX->Mega RX1 (pin 19)SIM800L RX->Mega TX1 (pin 18)
Relay outputs wired to Mega digital pins (exact mapping used in examples/mega_slave_i2c/mega_slave.ino):
RELAY_BASE_PIN = 22— Relay ID 1 → pin 22, ID 2 → pin 23, ID 3 → pin 24, and so on (ID N → pin22 + (N-1)).MAX_RELAYS = 16(configurable upper bound in code) — ensure your relay board has enough channels.RELAY_ACTIVE_LOW = true— relays are driven active by LOW in the current wiring (code uses inverted logic when writing pins).
Other dedicated pins on the Mega (from examples/mega_slave_i2c/mega_slave.ino):
UNLOCK_RELAY_PIN = 53— dedicated relay used for the door unlock mechanism (default ON at startup;unlockcommand sets it OFF).WATER_RELAY_PIN = 52— dedicated water relay controlled by water sensor logic.WATER_SENSOR_PIN = 48— float/water sensor input (code assumesHIGH= wet,LOW= dry).
When the ESP8266 sends "id:state" (e.g., "1:1"), the Mega will map id → pin as described and apply the state. Special string commands handled by the Mega include "lock", "unlock", "alert", "waterempty", and "waterpresent".
VCC-> stable ~4.0V power supply capable of ~2A peaks (recommended) — do NOT power SIM800L from the Mega 5V regulator directlyGND-> Common ground with Mega and ESPTX-> MegaRX1(pin 19)RX-> MegaTX1(pin 18)
Power note: The SIM800L requires a stable 3.8–4.2V supply that can handle current spikes when the radio transmits. Use a dedicated LiPo or proper regulator and include decoupling capacitors.
- ESP8266 (Master)
SDA=D2(GPIO4)<-> MegaSDA(pins 20) - ESP8266 (Master)
SCL=D1(GPIO5)<-> MegaSCL(pins 21) - Both devices must share common ground.
The ESP8266 acts as I2C master and the Mega is expected to run an
Wire.onReceive(...)handler to parse command strings like"alert","lock","unlock", or relay control strings like"1:1".
- Clone the repo:
git clone https://github.com/rul3zero/SmartHaus-Sensors.git
cd SmartHaus-Sensors- Create a
secrets.hininclude/(this file is ignored by.gitignore) and add your WiFi and Firebase credentials. Examplesecrets.hminimal contents:
// secrets.h - DO NOT COMMIT
#define WIFI_SSID "your-ssid"
#define WIFI_PASSWORD "your-wifi-password"
#define API_KEY "your-firebase-api-key"
#define USER_EMAIL "your-firebase-user@example.com"
#define USER_PASSWORD "your-firebase-password"
#define DATABASE_URL "https://your-database.firebaseio.com"- Open the project with PlatformIO (VS Code) or use the CLI to build:
# From repository root
platformio run
platformio run --target upload- Upload firmware to devices:
ESP8266— upload thesrc/firmwareArduino Mega— upload theexamples/mega_slave_i2c/mega_slave.inofirmware (openexamples/mega_slave_i2c/for the sketch)
The project expects this minimal structure under /devices and /smart_controls:
/devices/fingerprint_door_001/
last_updated: "YYYY-MM-DD HH:MM:SS"
failed_attempts: 0
logs:
YYYY-MM-DD:
HH:MM:SS:
status: "success" | "failed"
user: "username" | "unknown"
/devices/water_level_001/
water_level: true | false
status: "water_present" | "water_empty"
tank_status: "normal" | "alert"
/smart_controls/relays/{1..8}/state
/smart_controls/relays/door/isLocked
The code will write logs as:
/devices/fingerprint_door_001/logs/2025-09-15/01:35:31/status = "success"
and
/devices/fingerprint_door_001/logs/2025-09-15/01:35:31/user = "Jayce"
- Open this folder in VS Code with PlatformIO extension and select the correct environment and board.
- Run
PlatformIO: BuildthenPlatformIO: Uploadfor each target device.
Example CLI (Windows PowerShell):
# Build
C:\Users\<you>\.platformio\penv\Scripts\platformio.exe run
# Upload
C:\Users\<you>\.platformio\penv\Scripts\platformio.exe run --target upload-
Fingerprint sensor not responding:
- Confirm TX/RX are cross-connected (sensor TX -> NodeMCU RX)
- Ensure sensor voltage is compatible with NodeMCU (3.3V recommended)
- Verify
SoftwareSerialpins matchmanageFingerprint.cpp(mySerial(14, 12))
-
SIM800L SMS fails intermittently:
- Ensure SIM800L has a stable 4V power supply that can provide ~2A peaks
- Avoid powering SIM800L from the Mega 5V regulator
- Allow >1 second windows where Mega isn't blocked by I2C interrupts while sending AT commands
-
Firebase operations failing:
- Check
secrets.hfor correctDATABASE_URLand API credentials - Verify device clock if timestamping is required — NTP is used in
setup()
- Check