Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ESP8266 AC power monitor with real-time web interface, automatic data logging, and PZEM-014/016 sensor support for home energy tracking

License

Notifications You must be signed in to change notification settings

tsaplia/energy-controller-refactored

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ Energy Consumption Controller

PlatformIO ESP8266 PZEM License: MIT

Professional-grade AC power monitoring system for home and industrial applications. Monitor voltage, current, power consumption, and energy usage with real-time web interface and historical analytics.

✨ Features Overview

πŸ“Š Real-time Power Monitoring

  • Live voltage, current, power, energy, frequency, and power factor readings
  • Interactive charts showing consumption trends over time
  • Day/night energy consumption tracking with separate statistics

🌐 Smart Web Interface

  • Fully responsive design - works on mobile, tablet, and desktop
  • WebSocket-based real-time updates (no page refreshing needed)
  • WiFi setup portal with automatic fallback to Access Point mode
  • Real-time system logs

πŸ’Ύ Intelligent Data Management

  • Automatic CSV data logging with configurable intervals
  • Configurable data retention periods to manage storage
  • Energy statistics export for day/night consumption analysis
  • FTP server for bulk file operations and remote access

πŸ”§ Advanced System Features

  • Over-the-air (OTA) firmware updates without USB cable
  • Automatic NTP time synchronization with timezone support
  • Dual WiFi mode operation (Access Point + Station)
  • System reliability with watchdog protection and auto-restart

🎯 Why Choose This Controller?

βœ… Industrial Accuracy - 0.5% measurement precision with PZEM sensors
βœ… Real-time Monitoring - Live data streaming with interactive charts
βœ… Smart Data Management - Automatic CSV logging with configurable retention
βœ… Mobile-First Design - Responsive web interface works on any device
βœ… Professional Features - OTA updates, FTP access, advanced analytics
βœ… Easy Setup - WiFi portal setup, automatic network recovery
βœ… Reliable Operation - 24-hour auto-restart, memory monitoring, error recovery
βœ… Developer Friendly - Full API, WebSocket support, modular architecture

πŸ“‹ Table of Contents


⚠️ Safety & Prerequisites

⚑ Important Safety Notice

Working with AC Power
This project measures AC mains voltage. While the PZEM sensors provide isolation, basic electrical safety is important. Turn off power when making connections and double-check your wiring.

Basic Safety Steps:

  • ⚑ Turn off circuit breaker when connecting wires
  • πŸ”Œ Use proper wire connections (no loose wires)
  • πŸ“¦ Keep electronics in a suitable enclosure
  • πŸ” Double-check connections before powering on

πŸ“‹ What You Need to Know

Coding Experience:

  • None required! Just follow the setup instructions
  • Copy and paste - the code is ready to use
  • Basic computer skills (downloading, uploading files)

Basic Electronics:

  • Connecting wires (positive, negative, data lines)
  • Using a USB cable to program the ESP8266
  • Basic understanding that electricity can be dangerous

⏱️ Time Estimate

  • Hardware assembly: 1-2 hours (simple wire connections)
  • Software setup: 15-30 minutes (mostly downloading and uploading)
  • Getting it working: Usually works right away if wired correctly

πŸ›‘οΈ Quick Safety Checklist

  • I'll turn off power when connecting wires
  • I'll double-check connections before turning power back on
  • I understand this involves electrical work (but it's manageable)
  • I'll ask for help if I'm unsure about anything

πŸ› οΈ Hardware Requirements

πŸ“¦ Shopping List

Core Components

ESP8266 Development Board

  • Any ESP8266 board with accessible GPIO pins
  • Examples: NodeMCU, Wemos D1 Mini, ESP-01 (with adapter), etc.
  • Must have 3.3V and 5V power available
  • USB programming interface recommended for easy setup

Power Monitoring Sensor:

Model Max Current Best For
PZEM-014 10A Household circuits, small appliances
PZEM-016 100A Main panels, large loads (needs external CT)

πŸ“– Detailed specifications: See PZEM-014/016 Technical Manual for complete electrical specifications and communication protocol details.

Note: This project is specifically designed for PZEM-014/016 sensors due to their Modbus RTU communication protocol. Other power sensors would require code modifications.

Additional Components

  • Jumper wires (male-to-female, 4 pieces minimum)
  • 5V Power supply (USB phone charger works fine)
  • Project enclosure (optional but recommended for permanent installation)

πŸ”Œ Component Selection Guide

Choose PZEM-014 if:

  • βœ… Monitoring individual appliances or circuits under 10A
  • βœ… Want simpler installation (built-in current measurement)
  • βœ… Most household applications (lights, outlets, small appliances)

Choose PZEM-016 if:

  • βœ… Monitoring main electrical panel or large loads
  • βœ… Need to measure over 10A (up to 100A)
  • βœ… Already have or can install current transformer (CT)

πŸ”§ Wiring & Connections

ESP8266 to PZEM Connection

ESP8266                PZEM-014/016
==================    ============
5V or VIN        -->  VCC (+5V)
GND              -->  GND  
GPIO13 (D7)*     -->  TX (Data out from PZEM)
GPIO15 (D8)*     -->  RX (Data in to PZEM)

*Pin numbers may vary by board - check your specific ESP8266 board pinout

PZEM Power Connections

⚑ IMPORTANT: Turn off circuit breaker before making these connections!

For PZEM-014:

  • Connect L (Live/Hot) wire through the device
  • Connect N (Neutral) wire through the device
  • Built-in current sensing - no external wires needed

For PZEM-016:

  • Connect L and N for voltage sensing
  • Install current transformer (CT) around the main wire
  • CT connects to designated terminals on PZEM-016

πŸ”— Detailed wiring diagrams: See figures 4.1 and 4.2 in the PZEM Technical Manual for complete wiring instructions and safety guidelines.

πŸ“‹ Optional Accessories

  • Project enclosure: Protect electronics (DIN rail mount available)
  • Heat shrink tubing: Professional wire connections
  • Terminal blocks: Easier wire connections
  • Current transformer: Required for PZEM-016 (usually included)

πŸš€ Quick Start

For experienced users - Get running in 30 minutes. Need more detail? Jump to Detailed Setup.

Prerequisites: PlatformIO installed (VS Code extension or CLI)

Step 1: Wire Hardware

ESP8266 β†’ PZEM-014/016
5V/VIN  β†’ VCC
GND     β†’ GND  
GPIO13  β†’ TX
GPIO15  β†’ RX

Connect PZEM to your AC circuit (⚑ power off first). See PZEM Technical Manual for detailed wiring instructions.

Step 2: Clone & Setup

git clone https://github.com/tsaplia/energy-controller-refactored.git
cd energy-controller-refactored

Step 3: Build & Upload

platformio run --target upload          # Upload firmware via USB
platformio run --target uploadfs        # Upload web interface files

Or use VS Code PlatformIO extension buttons

Step 4: Configure WiFi

  1. ESP creates AP ESP8266-Sensor (password: password)
  2. Connect and enter your WiFi credentials
  3. Device connects to your network and shows IP address
  4. AP automatically disconnects - reconnect to your home WiFi network
  5. Access web interface using the displayed IP address

Step 5: Access Interface

  • Find ESP IP address (router admin or serial monitor)
  • Open http://ESP_IP_ADDRESS in browser
  • Start monitoring power consumption!

⚑ Quick Troubleshooting

  • No sensor data? β†’ Check wiring, verify PZEM power
  • Can't connect to WiFi? β†’ Reset and try AP mode again
  • Upload failed? β†’ Check USB cable, try different port
  • Web interface not loading (Error 404)? β†’ Verify filesystem upload completed

πŸ“– Need help? See Detailed Setup or Troubleshooting sections.


πŸ“± Detailed Setup

Step-by-step guide for beginners - No prior experience needed!

Step 1: Install Development Environment

Option A: VS Code + PlatformIO (Recommended)

  1. Download VS Code: Go to code.visualstudio.com and install
  2. Install PlatformIO extension:
    • Open VS Code
    • Click Extensions icon (left sidebar)
    • Search "PlatformIO IDE"
    • Click Install on the official extension
    • Wait for PlatformIO Core to download and install (this may take several minutes)
    • Restart VS Code when prompted

⚠️ Important: Wait for PlatformIO to fully initialize before opening projects. If you encounter project configuration issues, try: View β†’ Command Palette β†’ "PlatformIO: Rebuild IntelliSense Index"

Option B: PlatformIO CLI

  1. Install Python (if not already installed)
  2. Install PlatformIO Core:
    pip install platformio

Step 2: Download Project Files

If you have Git:

git clone https://github.com/tsaplia/energy-controller-refactored.git
cd energy-controller-refactored

If you don't have Git:

  1. Go to the project's GitHub page
  2. Click green "Code" button β†’ "Download ZIP"
  3. Extract ZIP file to a folder
  4. Remember the folder location

Step 3: Hardware Assembly

Gather Your Components

  • ESP8266 board (any model with GPIO pins)
  • PZEM-014 or PZEM-016 sensor
  • 4 jumper wires (male-to-female)
  • USB cable for ESP8266

Make Connections

⚑ IMPORTANT: Do this with power OFF!

  1. Connect ESP8266 to PZEM:

    ESP8266 Pin    β†’    PZEM Pin
    ============        =========
    5V or VIN      β†’    VCC
    GND            β†’    GND  
    GPIO13 (D7)*   β†’    TX
    GPIO15 (D8)*   β†’    RX
    

    *Pin labels may vary - check your board's pinout

  2. Connect PZEM to AC Power (⚑ Turn off circuit breaker first!):

    • PZEM-014: Connect Live and Neutral wires through the device
    • PZEM-016: Connect L/N for voltage, install CT for current
    • See PZEM Technical Manual for detailed wiring diagrams
  3. Double-check all connections before powering on

Step 4: Configure & Upload Software

Open Project

  • VS Code: File β†’ Open Folder β†’ Select your project folder
  • CLI: Navigate to project folder in terminal

Upload Firmware

Using VS Code:

  1. Click PlatformIO icon in left sidebar
  2. Under "PROJECT TASKS" β†’ "nodemcuv3" β†’ "General"
  3. Click "Upload" (this compiles and uploads firmware)
  4. Wait for "SUCCESS" message

Using CLI:

platformio run --target upload

Upload Web Interface Files

Using VS Code:

  1. Same location as above
  2. Click "Upload Filesystem Image"
  3. Wait for completion

Using CLI:

platformio run --target uploadfs

Step 5: First Boot & WiFi Setup

What to Expect

  1. ESP8266 starts and creates its own WiFi network
  2. Network name: ESP8266-Sensor (password: password)
  3. Connection process:
    • Connect your phone/computer to ESP8266's WiFi
    • Open any webpage - it will redirect to setup page
    • Enter your home WiFi name and password
    • Device connects to your network and displays IP address on the page
    • AP automatically disconnects - you must reconnect to your home WiFi network to access the web interface

Connect to WiFi interface

Find Your Device

Method 1 - Router Admin:

  • Log into your router (usually 192.168.1.1 or 192.168.0.1)
  • Look for new device in connected devices list

Method 2 - Serial Monitor:

  • Keep USB cable connected
  • Open serial monitor in PlatformIO (9600 baud)
  • Device will print its IP address

Step 6: Access Web Interface

  1. Open browser and go to: http://YOUR_ESP_IP_ADDRESS
  2. You should see the energy monitoring dashboard
  3. If sensor is connected properly, you'll see live voltage/current/power readings

πŸŽ‰ Success! What Now?

  • Dashboard: View real-time power consumption
  • Settings: Configure data intervals, timezones, export your energy data, etc.
  • Logs: Monitor system operation

❌ Something Not Working?

No sensor data showing?

  • Check wiring connections
  • Verify PZEM has power (AC connected)
  • Try different GPIO pins if needed

Can't connect to WiFi?

  • Reset ESP8266 (power cycle)
  • Try connecting to AP again
  • Check WiFi password

Upload failed?

  • Check USB cable connection
  • Try different USB port
  • Make sure ESP8266 drivers are installed

Still stuck? Check the Troubleshooting section below!


🌐 Web Interface Guide

Once your device is connected to WiFi, open its IP address in your browser to access the monitoring interface.

Dashboard Overview

The main dashboard provides real-time monitoring with:

Live Data Display

  • Real-time sensor readings updated via WebSocket connection
  • Voltage, current, power, energy consumption metrics
  • Frequency and power factor measurements
  • Interactive charts showing voltage and power trends over time

Dashboard Features

  • πŸ“± Fully responsive design β€” works perfectly on mobile phones, tablets, and desktop
  • πŸ”„ Auto-updating data β€” no need to refresh the page
  • πŸ“Š Visual charts β€” easily spot power consumption patterns

Dashboard Screenshot

Settings Configuration

Access the settings page at /settings to configure your controller:

Data Collection Settings

  • Data Save Interval: How often to record sensor data (default: 30 seconds)
  • Data Retention: How long to keep historical data (default: 1 day)
  • Timezone Offset: Set your local timezone for accurate timestamps

Day/Night Phases

  • Day Phase Start: When "day" energy tracking begins (default: 7:00 AM)
  • Night Phase Start: When "night" energy tracking begins (default: 11:00 PM)
  • Automatically calculates separate day and night energy consumption

System Settings

  • Log Level: Control detail level of system logs (DEBUG, INFO, WARNING, ERROR)
  • WiFi Credentials: Update network connection settings
  • Energy Reset: Reset energy counter to start fresh measurements

Data Export

  • Download sensor data: Export CSV with all recorded measurements
  • Download energy stats: Export day/night energy consumption summaries

⚠️ WiFi Changes: After updating WiFi credentials, the device will disconnect and connect to the new network. You'll need to find the new IP address to access the interface again.

Settings Screenshot

System Logs

The logs page at /logs provides:

Real-time Log Monitoring

  • Live log stream via WebSocket connection
  • Historical logs from device storage
  • Timestamp information for troubleshooting

Log Categories

  • DEBUG: Detailed technical information
  • INFO: General operational messages
  • WARNING: Important notices and state changes
  • ERROR: Problems that need attention

πŸ”„ API Reference

The controller provides a comprehensive HTTP API for automation and integration with other systems.

πŸ”§ System Control Routes

Route Method Description Response
/api/restart GET Restart the ESP8266 controller {"detail": "Restarting..."}
/api/pause GET Pause data collection and processing {"detail": "Pausing..."}
/api/resume GET Resume normal operation {"detail": "Resuming..."}
/api/system-info GET Get detailed system information JSON with chip, memory, filesystem data
/api/clear-old GET Remove old data based on retention settings {"detail": "Cleared old data"}

βš™οΈ Configuration Routes

Route Method Description Body Response
/api/settings GET Get current configuration - JSON with all settings
/api/settings POST Update configuration JSON with settings to change {"detail": "OK"}
/api/reset-energy PUT Reset energy measurement baseline - {"detail": "OK"}
/api/connect-wifi POST Update WiFi credentials {"ssid": "...", "password": "..."} {"ip": "192.168.x.x"}

πŸ“‘ WebSocket Endpoints

Socket Path Purpose Message Format
/ws/data Real-time sensor data {"type": "data", "data": {...}}
/ws/logs Live system logs {"type": "log", "data": "log message"}

WebSocket Data Format

Sensor Data Messages:

{
  "type": "data",
  "data": {
    "time": 1640995200,
    "voltage": 230.5,
    "current": 2.150,
    "power": 495.6,
    "energy": "12.5",
    "frequency": 50.0,
    "pf": 0.95
  }
}

Log Messages:

{
  "type": "log",
  "data": "2024-01-01 12:00:00 [INFO] WiFi connected on IP: 192.168.1.100"
}

πŸ“‚ File Access Routes

Route Description
/sensor-data.csv Raw sensor measurements
/energy-stats.csv Day/night energy summaries
/configs.json Current device configuration
/logs.txt System log file

πŸ“₯ FTP Access

Files can also be accessed via FTP for bulk operations:

  • Host: ESP8266 IP address
  • User: esp
  • Password: password
  • Port: 21 (standard FTP)

πŸ“‚ Project Structure

Understanding the project layout helps you navigate the codebase and customize functionality as needed.

πŸ—‚οΈ Root Directory

energy-consumption-controller/
β”œβ”€β”€ πŸ“ .vscode/                    # VS Code configuration
β”œβ”€β”€ πŸ“ data/                       # Web interface files (uploaded to ESP8266)
β”œβ”€β”€ πŸ“ docs/                       # Documentation and images
β”œβ”€β”€ πŸ“ external-data/              # Stores large external resources not kept on the ESP8266
β”œβ”€β”€ πŸ“ include/                    # C++ header files
β”œβ”€β”€ πŸ“ lib/FTPClientServer-master/ # FTP server library
β”œβ”€β”€ πŸ“ src/                        # C++ source code
β”œβ”€β”€ πŸ“„ .gitignore                  # Git ignore rules
β”œβ”€β”€ πŸ“„ LICENSE                     # Project license
β”œβ”€β”€ πŸ“„ README.md                   # This file
└── πŸ“„ platformio.ini              # PlatformIO configuration

🌐 Web Interface Files (/data)

These files are uploaded to the ESP8266's filesystem and served by the web server:

data/
β”œβ”€β”€ πŸ“„ auth.html          # WiFi setup page (AP mode)
β”œβ”€β”€ πŸ“„ data.js            # WebSocket data handling
β”œβ”€β”€ πŸ“„ index.html         # Main dashboard page
β”œβ”€β”€ πŸ“„ logs.html          # System logs viewer
β”œβ”€β”€ πŸ“„ logs.js            # Log WebSocket handling
β”œβ”€β”€ πŸ“„ settings.html      # Configuration page
β”œβ”€β”€ πŸ“„ settings.js        # Settings management
β”œβ”€β”€ πŸ“„ shared.js          # Common JavaScript functions
└── πŸ“„ style.css          # All page styling
external-data/
└── πŸ“„ bootstrap.min.css  # Bootstrap styling, loaded via GitHub CDN instead of controller storage

🧠 Core Source Code (/src)

Main application logic organized by functionality:

src/
β”œβ”€β”€ πŸ“„ main.cpp           # Application entry point & setup
β”œβ”€β”€ πŸ“„ configs.cpp        # Configuration management
β”œβ”€β”€ πŸ“„ globals.cpp        # Global variables initialization
β”œβ”€β”€ πŸ“„ logger.cpp         # Logging system implementation
β”œβ”€β”€ πŸ“„ loop-handlers.cpp  # Main loop logic handlers
β”œβ”€β”€ πŸ“„ sensor.cpp         # PZEM sensor communication
β”œβ”€β”€ πŸ“„ utils.cpp          # Utility functions (time, files, etc.)
β”œβ”€β”€ πŸ“„ wifi-tools.cpp     # WiFi management (AP/STA modes)
└── πŸ“ web/               # Web server components
    β”œβ”€β”€ πŸ“„ api-handlers.cpp    # HTTP API endpoint handlers
    β”œβ”€β”€ πŸ“„ web-server.cpp      # Web server setup & routing
    └── πŸ“„ web-utils.cpp       # Web utility functions

πŸ”§ Header Files (/include)

Interface definitions and constants:

include/
β”œβ”€β”€ πŸ“„ configs.h          # Configuration class definition
β”œβ”€β”€ πŸ“„ constants.h        # System constants and defaults
β”œβ”€β”€ πŸ“„ globals.h          # Global variable declarations
β”œβ”€β”€ πŸ“„ logger.h           # Logging system interface
β”œβ”€β”€ πŸ“„ loop-handlers.h    # Loop handler function declarations
β”œβ”€β”€ πŸ“„ sensor.h           # Sensor data structures & functions
β”œβ”€β”€ πŸ“„ utils.h            # Utility function declarations
β”œβ”€β”€ πŸ“„ wifi-tools.h       # WiFi management interface
└── πŸ“ web/               # Web server headers
    β”œβ”€β”€ πŸ“„ api-handlers.h      # API handler declarations
    β”œβ”€β”€ πŸ“„ web-server.h        # Web server declarations
    └── πŸ“„ web-utils.h         # Web utility declarations

πŸ” Important Files to Know

For Customization:

  • platformio.ini - Build configuration, board settings, libraries
  • main.cpp - Add new sensors or modify startup behavior
  • constants.h - Modify default settings, intervals, passwords
  • style.css - Customize web interface appearance

For Troubleshooting:

  • logger.cpp - Understand logging system
  • wifi-tools.cpp - WiFi connection issues
  • sensor.cpp - PZEM communication problems

For API Integration:

  • api-handlers.cpp - Add new API endpoints
  • web-server.cpp - Modify routing or WebSocket behavior

πŸ’Ύ Runtime Files (Created on ESP8266)

When running, the controller creates these files in its filesystem:

ESP8266 Filesystem:
β”œβ”€β”€ πŸ“„ /configs.json          # Saved configuration
β”œβ”€β”€ πŸ“„ /sensor-data.csv       # Historical sensor readings  
β”œβ”€β”€ πŸ“„ /energy-stats.csv      # Daily/nightly energy summaries
└── πŸ“„ /logs.txt              # System log history

πŸ”§ Troubleshooting

🌐 Web Interface Issues

Pages loading slowly or not fully:

  • Try reloading the page (Ctrl+F5 or Cmd+Shift+R)
  • Wait a few seconds between page requests - ESP8266 has limited resources

Charts not loading properly:

  • Slow internet connection may prevent all data from loading
  • Reduce data retention in settings to keep less historical data
  • Check your router for connection issues
  • Try accessing /api/restart to restart the ESP8266 controller

πŸ› οΈ PlatformIO Setup Issues

Extension not working properly:

  • Wait for PlatformIO Core to fully download and install
  • Project configuration errors: Go to View β†’ Command Palette β†’ "PlatformIO: Rebuild IntelliSense Index"
  • Build issues: Try PlatformIO: Clean then PlatformIO: Build
  • Still having problems: Restart VS Code and wait for initialization

πŸ”Œ Hardware & Connection Problems

No sensor data showing:

  • Check wiring connections between ESP8266 and PZEM
  • Verify PZEM has AC power connected
  • Try different GPIO pins if needed
  • Check serial monitor for sensor communication errors

Can't connect to WiFi:

  • Reset ESP8266 (power cycle)
  • Try connecting to AP mode again
  • Check WiFi password spelling
  • Ensure WiFi network is 2.4GHz (ESP8266 doesn't support 5GHz)

Upload failed:

  • Check USB cable connection
  • Try different USB port
  • Check platformio.ini configuration: "board = nodemcuv2" or "[env:nodemcuv3]" must match your actual board
  • Ensure ESP8266 drivers are installed
  • Press and hold FLASH button during upload if needed

About

ESP8266 AC power monitor with real-time web interface, automatic data logging, and PZEM-014/016 sensor support for home energy tracking

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published