Project Overview
Related Article: Prepaid Energy Meter Blog
IoT Prepaid Energy Meter: ESP32 + PZEM-004T for Real-Time Prepaid Billing
Design, implementation, and evaluation of an ESP32-based IoT prepaid energy meter using a PZEM-004T module, cloud backend (GCP/Firestore), a...Technical Implementation
Hardware Architecture
- Measurement Core: The PZEM-004T module measures RMS voltage, RMS current, active power, frequency, energy, and power factor. It communicates with the ESP32 via UART, utilizing optocouplers for electrical isolation from the high-voltage mains.
- Microcontroller: An ESP32 manages the local billing calculations, monitors the Wi-Fi connection, updates the local display, and writes safety logs.
- Power Disconnection: A high-current latching relay is used to connect or disconnect the load. Latching relays only require a short current pulse to switch states, minimizing power consumption.
- Local Feedback: A 16x2 I2C LCD screen displays the current voltage, power, and active balance. A buzzer provides low-balance warnings.
Firmware & Cloud Infrastructure
- Local State Loop: To handle Wi-Fi drops, all billing and credit deduction calculations run locally on the ESP32. The current balance is updated and saved in the ESP32's non-volatile storage (NVS) every second, ensuring the state survives power cuts.
- Cloud Backend: Google Cloud Functions and Firestore manage account states, user credentials, and charge histories.
- Consumer Interface: A React-based Progressive Web App (PWA) provides users with live charts of their energy consumption, transaction logs, and the ability to recharge their meter balance.
Key Features
- Decentralized Billing: The billing engine runs on-device, disconnecting the load immediately when credit is exhausted even if the network is down.
- Secure Control: Relay operations and balance top-ups are validated server-side in Firestore before sending commands to the device.
- Oversampled Current Sensing: The CT clamp captures high-resolution current data, which is averaged in firmware to reduce line noise.
Challenges and Solutions
- Network Reliability: If the meter relied on cloud API calls to deduct credit, a Wi-Fi drop would allow users to consume unpaid electricity. I resolved this by keeping the billing engine entirely inside the ESP32's firmware, restricting the cloud connection to reporting telemetry and checking for recharges.
- NVS Wear: Writing to the ESP32's flash memory too frequently (every second) can wear out the flash sector. To solve this, the firmware keeps the active balance in RAM and only writes to the NVS flash when the balance changes by a full unit (e.g., 0.1 Wh) or when a sudden power drop is detected by an interrupt line on the input rails.