IOT METER UPDATED CODE CODE CODE CODE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "EmonLib.h" // Include Emon Library
#include "ACS712.h" // Include ACS712 Library
// Define LCD parameters
#define I2C_ADDR 0x27 // I2C address for the LCD
#define LCD_ROWS 2
#define LCD_COLS 16
// Create instances of the libraries
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);
EnergyMonitor emon1;
ACS712 sensor(ACS712_05B, A0);
#define VOLT_CAL 592
#define RESISTOR_VALUE 1000 // Resistance value in Ohms
#define VOLTAGE_RMS_REF 230.0 // Reference RMS voltage in Volts
#define POWER_THRESHOLD 100 // Power threshold for determining "power off" state
const int samplingInterval = 1000; // Sampling interval in milliseconds
const int displayInterval = 20000; // Display update interval in milliseconds (20 seconds)
float currentPowerSum = 0; // Sum of power values over the 20-second interval
int powerSampleCount = 0; // Number of power samples in the 20-second interval
float totalEnergy = 0; // Total energy consumption
unsigned long lastSampleTime = 0; // Last sample time
unsigned long lastDisplayTime = 0; // Last display update time
float lastMeasuredPower = 0; // Last measured power value
void setup() {
Serial.begin(9600);
// Initialize the LCD
lcd.init();
// lcd.backlight(); // Uncomment if backlight control is needed
// Initialize sensors
emon1.voltage(1, VOLT_CAL, 1.7); // Voltage sensor initialization
sensor.calibrate(); // Current sensor calibration
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - lastSampleTime >= samplingInterval) {
lastSampleTime = currentTime;
// Voltage sensing
emon1.calcVI(25, 1000); // Calculate voltage and current
float supplyVoltage = emon1.Vrms; // Extract Vrms into variable
// Check if voltage is less than 100V
if (supplyVoltage < 100.0) {
// Set power to zero
lastMeasuredPower = 0;
// Send power off state to ESP32 module
Serial.println("POWER_OFF");
} else {
// Current sensing
float current = sensor.getCurrentAC(); // Read current
// Power calculation
lastMeasuredPower = supplyVoltage * current; // Power in Watts (P = VI)
// Send power value to ESP32 module
Serial.print("POWER_VALUE:");
Serial.println(lastMeasuredPower);
}
// Add power to the current power sum
currentPowerSum += lastMeasuredPower;
powerSampleCount++;
// Check if 20 seconds have passed
if (currentTime - lastDisplayTime >= displayInterval) {
// Calculate average power over the last 20 seconds
float averagePower = currentPowerSum / powerSampleCount;
// Print the average power
Serial.print("Average Power: ");
Serial.print(averagePower);
Serial.println(" W");
// Update the LCD display
lcd.clear();
lcd.setCursor(0, 0);
if (lastMeasuredPower == 0) {
lcd.print("Power: OFF");
} else {
lcd.print("Power: ");
lcd.print(averagePower, 2);
lcd.print(" W");
}
// Update total energy consumption (in Watt-hours)
totalEnergy += (averagePower * (displayInterval / 1000.0)) / 3600.0;
// Display total energy consumption on the second line
lcd.setCursor(0, 1);
lcd.print("Energy: ");
lcd.print(totalEnergy, 2);
lcd.print(" Wh");
// Reset power sum and count for the next interval
currentPowerSum = 0;
powerSampleCount = 0;
lastDisplayTime = currentTime;
}
}
}
Comments
Post a Comment