⚡ IoT-Based Prepaid Energy Meter with RF Mesh and Firebase Cloud
Managing power consumption efficiently is critical, especially in shared environments like hostels, rental homes, and industrial setups. This project presents a wireless prepaid energy metering system using nRF24L01+, ESP8266, PZEM004T, and Firebase Firestore. The system allows real-time energy monitoring, cloud-based recharge, and automatic cut-off when balance is depleted.
๐ง System Overview
- Child Node: Arduino Nano + PZEM004T + Relay + nRF24L01+
- Gateway (Parent Node): ESP8266 (NodeMCU) + nRF24L01+
- Cloud Backend: Firebase Firestore (NoSQL)
Key Features
- ⚡ Real-time energy measurement (Voltage, Current, Power, kWh)
- ๐ฐ Prepaid balance logic with relay cut-off
- ๐ถ RF communication in scalable tree topology
- ☁️ Cloud integration with Firebase Firestore
- ๐ฑ Recharge via Web/App or Firebase Console
๐ถ Multi-Level RF Topology
The system uses an RF mesh where a central ESP8266 Gateway (Parent Node) communicates with multiple child meter nodes.
[ Firebase Firestore ] ↑ [ ESP8266 Gateway ] ↑ ┌──────────────┼──────────────┐ ↓ ↓ ↓ [Child A1] [Child B1] [Child C1] + Relay + Relay + Relay + PZEM + PZEM + PZEM
Each child node reports energy usage and balance to the gateway. The gateway then updates the Firebase Firestore document.
๐ Communication Protocols
- UART (Serial): Arduino ↔ PZEM004T (Energy data)
- SPI: Arduino/ESP8266 ↔ nRF24L01+ (RF transmission)
- RF (2.4 GHz): nRF24L01+ between nodes
- HTTP + REST: ESP8266 ↔ Firebase Firestore
- JSON: For structuring data across all layers
{
"voltage": 230.5,
"current": 0.71,
"power": 165.4,
"energy": 5.21,
"balance": 2.34,
"relay": true
}
๐ก nRF24L01+ Module Variants
1️⃣ Standard nRF24L01+ (PCB Antenna)
- ๐ข Compact and budget-friendly
- ๐ถ Range: ~10–30 meters (indoor)
- ๐ Operates at 3.3V (not 5V tolerant)
- ๐ก Ideal for short-range and testing
2️⃣ nRF24L01+ PA+LNA (with External Antenna)
- ๐ก Long-range with Power Amplifier + Low Noise Amplifier
- ๐ถ Range: Up to 100–1000 meters (LOS)
- ⚡ Needs stable 3.3V regulator (e.g., AMS1117)
- ๐ง Recommended for large buildings or campus setups
Tip: Always use a 10ยตF–47ยตF capacitor across VCC-GND to prevent brownouts and instability in PA+LNA modules.
๐ Firestore Document Example
/energyNodes/childA1 { voltage: 229.8, current: 0.71, power: 162.5, energy: 4.32, balance: 2.87, relay: true, lastRecharge: "2021-09-05T14:00:00Z" }
The ESP8266 gateway updates this document after receiving data from each child node. Recharge values are updated from the app or Firebase Console.
๐ง Child Node Logic (Arduino + Relay)
if (energyUsed > 0) {
balance -= energyUsed;
}
digitalWrite(relayPin, balance <= 0 ? LOW : HIGH); // Relay cutoff
// Send data to gateway
radio.write(&dataPacket, sizeof(dataPacket));
๐ ESP8266 Gateway (Firestore Update)
FirebaseJson json;
json.set("voltage", data.voltage);
json.set("energy", data.energy);
json.set("balance", data.balance);
json.set("relay", data.relayStatus);
Firebase.Firestore.patchDocument(&fbdo, projectID, "",
"energyNodes/childA1", json.raw(),
"voltage,power,energy,balance,relay");
๐ฐ Recharge Process
You can recharge the balance using a web/app interface or directly from the Firebase Console by updating:
/energyNodes/childA1/balance = current_balance + recharge_amount
The updated balance is pulled and applied during the next communication cycle by the gateway and passed to the child node.
๐ Applications
- ๐ Prepaid metering in hostels and PGs
- ๐ข Machine-level energy management in factories
- ๐ก Smart sub-metering in apartments
- ๐ Rural and off-grid electrification
- ๐ Multi-tenant commercial buildings
๐ Optional Future Upgrades
- ๐ Low-balance notifications to mobile app
- ๐ Daily energy logs with timestamps in Firestore
- ๐ OTA firmware updates via Wi-Fi
- ๐ UPI-based recharge with transaction history
- ๐ถ Replace nRF24L01+ with LoRa for long-range use
- PZEM004T TX ↔ Arduino RX
- Relay IN ↔ Digital Output Pin
- nRF24L01+ ↔ SPI (MOSI, MISO, SCK, CSN, CE)
- Use AMS1117 or HT7333 to power nRF module
๐ก️ Shadow Master Node (Failover ESP8266)
To improve reliability, especially in large-scale or critical deployments, a Shadow Master Node (another ESP8266) can be used. This node listens for heartbeat signals from the main ESP8266 gateway. If the heartbeat is lost for more than 15 seconds, it automatically takes over gateway responsibilities (polling meters and sending data to Firebase).
⚙️ Features
- ๐ Auto switch-over from main gateway
- ⏱️ Heartbeat detection every 5 seconds
- ๐ค Uploads data to Firebase during failover
- ๐ Reverts to passive when primary gateway is back
๐ง Failover Logic
// If heartbeat is received:
lastHeartbeat = millis();
// If heartbeat timeout (15 seconds):
if (millis() - lastHeartbeat > 15000) {
isMaster = true; // Shadow takes over as master
}
๐ก RF Communication
- Master sends
"HEARTBEAT"
over RF every 5 seconds - Shadow listens using nRF24L01+ on pipe
"HBART"
- If missed for 15s, Shadow becomes master
- On receiving heartbeat again, it returns to standby
๐งฉ Deployment Tip
Place the Shadow Node in a different power line or area to ensure redundancy in case of hardware/power failure on the main ESP8266.
๐ฅ Want the Full Project?
Comment below or contact me to get:
- ✅ Full Arduino + ESP8266 code (Node + Gateway + Shadow)
- ✅ Firestore database rules and JSON templates
๐งฉ Tip:
Use multiple child nodes with unique addresses. The ESP8266 gateway can handle 10+ meters efficiently using RF polling and Firestore batching.
Let’s make energy metering smarter, scalable, and cloud-connected!
Comments
Post a Comment