This project presents a smart solution for real-time monitoring and forecasting of power and energy usage using an ESP32-based IoT setup integrated with cloud logging, machine learning, and a web dashboard.
This section covers the hardware components, connection schematics, firmware deployment on ESP32, and linking the system to Blynk, ThingSpeak, and Telegram Bot for real-time monitoring and alerting.
| Component | Purpose |
|---|---|
| ESP32 Dev Board | Main microcontroller (Wi-Fi enabled) |
| ACS712 Sensor | Current measurement |
| ZMPT101B Sensor | Voltage measurement |
| 16x2 LCD + I2C | Real-time display of voltage, current, power |
| Breadboard, Wires | Circuit connections |
| 5V Power Supply | Powers ESP32 and peripherals |
| Component | ESP32 Pin | Notes |
|---|---|---|
| ACS712 (OUT) | GPIO 34 (A0) | Analog input for current |
| ZMPT101B (OUT) | GPIO 35 (A1) | Analog input for voltage |
| LCD (SDA, SCL) | GPIO 21, 22 | I2C communication |
| GND | GND | Common ground |
| VCC (all) | 5V / 3.3V | Use logic-level compatible |
File: sketch_mar7a_copy_20250325014610.ino
Steps:
-
Open Arduino IDE.
-
Install board support: ESP32 by Espressif via Board Manager.
-
Select board:
Tools → Board → ESP32 Dev Module -
Install necessary libraries:
WiFi.hHTTPClient.hLiquidCrystal_I2CEmonLib
-
Edit the code to insert:
- Your Wi-Fi SSID and password
- Your Google Apps Script URL (for Sheets logging)
- Blynk Auth Token (see next section)
- ThingSpeak API Key
- Telegram Bot token and chat ID
-
Connect ESP32 via USB and click ✅ Upload
Steps:
-
Download Blynk IoT app from Play Store/App Store.
-
Create a new project.
-
Add widgets:
- 1x LCD display
- 1x Gauge for current
- 1x Gauge for voltage
- 1x Gauge for power
- 1x Notification (for alerts)
-
Copy the Auth Token from project settings.
-
Paste it into your ESP32 sketch (
char auth[] = "your_token";) -
Upload code again and monitor values on the app.
Steps:
-
Sign in at https://thingspeak.com
-
Create a new channel with fields:
- Voltage
- Current
- Power
-
Copy your Write API Key
-
In your ESP32 code, update:
String apiKey = "YOUR_API_KEY"; -
ThingSpeak will now log and plot data automatically.
Steps:
-
Open Telegram and search for BotFather
-
Run
/newbotand follow prompts to get your bot token -
Note the Bot Token and update in code:
String botToken = "YOUR_BOT_TOKEN"; -
To get your
chat_id:- Search for
userinfoboton Telegram - Send
/start, it will return your chat ID - Add that
chat_idto your ESP32 code
- Search for
-
Your ESP32 can now send alerts when:
- Power exceeds a threshold
- Sudden surges/drops are detected
Steps:
-
Open Google Sheets → Extensions → Apps Script
-
Paste code from
logSensorData.gs -
Deploy as web app:
Deploy → New deployment- Choose type: Web App
- Execute as: Me
- Who has access: Anyone
- Click
Deploy→ Copy URL
-
Update this URL in your ESP32 code:
const char* scriptUrl = "YOUR_DEPLOYED_SCRIPT_URL";
-
The ESP32 now pushes live data to your Google Sheet!
This section explains how real-time power consumption data is collected, processed, used to train a machine learning model (LSTM), and how it is then used for forecasting future power usage through an interactive web app interface.
Software-Setup/
│
├── app.py # Flask web app to run prediction & UI
├── model_training.py # LSTM model training script
│
├── monitor_data.csv # Historical training data (from Google Sheets)
├── current data.csv # Real-time uploaded input for prediction
├── power_units_lstm.keras # Trained LSTM model file
├── scaler.pkl # MinMaxScaler used to normalize input/output
│
├── static/ # Auto-generated output files and assets
│ ├── training_history.png # Loss curve of model training
│ ├── prediction_plot.png # 48-hour forecast graph (PNG)
│ ├── uploaded_data.csv # Saved original user-uploaded data
│ ├── prediction_data.csv # Saved forecast data
│
├── templates/ # Web UI HTML templates (Flask)
│ ├── index.html # Main upload interface
│ ├── results.html # Forecast display + AI Assistant chat
│
├── uploads/ # Stores incoming user CSV files
└── README.md # Documentation (you are reading it)
-
Source: The ESP32 reads voltage and current values using ACS712 and ZMPT101B.
-
Processing:
Power = Voltage × Current Units = Power × (duration / 1000) -
Logging: Data is sent to Google Sheets every few seconds using
logSensorData.gs. -
Export the Sheet manually as
monitor_data.csvfor model training.
Features Used:
- POWER
- UNITS
Preprocessing:
- Combines
DATEandTIMEinto a datetime index - Interpolates missing values
- Normalizes values with
MinMaxScaler - Uses 24-hour sliding windows for time series
Model:
- LSTM (3-layer)
- Dropout regularization
- Optimizer: Adam
- Loss: Mean Squared Error
- Early stopping enabled
Outputs:
power_units_lstm.keras: Saved modelscaler.pkl: Scaler objecttraining_history.png: Loss curve
Run with:
python model_training.pyBackend: app.py (Flask)
Workflow:
-
User uploads a
.csv(e.g.current data.csv) -
System:
-
Validates columns:
DATE, TIME, VOLTAGE, CURRENT, POWER, UNITS -
Extracts last 24 hours
-
Scales data using
scaler.pkl -
Predicts next 48 hours of
POWERandUNITS
-
-
Saves:
- Predictions to
prediction_data.csv - Graph to
prediction_plot.png - User data to
uploaded_data.csv
- Predictions to
-
Displays output on
results.htmlwith AI assistant
-
Integrated via Gemini 1.5 API
-
Handles queries like:
- "What’s the expected usage tomorrow?"
- "When is peak load?"
- "How can I reduce power?"
Install Dependencies:
pip install flask pandas numpy tensorflow scikit-learn joblib matplotlib google-generativeaiLaunch:
python app.pyVisit: http://localhost:5000
| Column | Type | Description |
|---|---|---|
| DATE | string | Format: DD/MM/YYYY |
| TIME | string | Format: HH:MM:SS |
| VOLTAGE | float | Measured voltage |
| CURRENT | float | Measured current |
| POWER | float | Calculated power (V × I) |
| UNITS | float | Energy units (kWh) |
- Minimum rows: 24 (hourly records)
- Graph: Forecast of POWER and UNITS for 48 hours
- Table: Timestamped values
- CSV: Saved prediction results
- Assistant: Interprets trends and gives suggestions