EXISTING IOT ENERGY METER FOR DISPLAYING POWER
#include "EmonLib.h" // Include Emon Library
#include "ACS712.h" // Include ACS712 Library
#define VOLT_CAL 592
#define RESISTOR_VALUE 1000 // Resistance value in Ohms
#define VOLTAGE_RMS_REF 230.0 // Reference RMS voltage in Volts (typically 230V for mains)
EnergyMonitor emon1; // Create an instance of EnergyMonitor
ACS712 sensor(ACS712_05B, A0); // Create an instance of ACS712
const int samplingInterval = 1000; // Sampling interval in milliseconds
const int numSamples = 3600; // Number of samples to store (3600 samples = 1 hour)
float voltageSamples[numSamples]; // Array to store voltage samples
int sampleIndex = 0; // Index to keep track of the current sample
float prevVoltage = 0; // Variable to store the previous voltage value
float prevPowerSum = 0; // Variable to store the sum of all previous power values
void setup() {
Serial.begin(9600);
emon1.voltage(1, VOLT_CAL, 1.7); // Voltage sensor initialization
sensor.calibrate(); // Current sensor calibration
}
void loop() {
// Voltage sensing
emon1.calcVI(25, 1000); // Calculate voltage
float supplyVoltage = emon1.Vrms; // Extract Vrms into Variable
// Current sensing
float current = sensor.getCurrentAC(); // Read current
// Power calculation
float power = (supplyVoltage * current) / 1000; // Power in Watts (P = VI / 1000)
// Energy calculation
if (supplyVoltage >= 100) {
// Store the voltage value
voltageSamples[sampleIndex] = supplyVoltage;
// Increment sampleIndex and loop back if necessary
sampleIndex = (sampleIndex + 1) % numSamples;
// Add current power to the sum of all previous power values
prevPowerSum += power;
// Print the sum of all previous power values
Serial.print("Sum of all previous power values: ");
Serial.print(prevPowerSum);
Serial.println("W");
} else {
Serial.println("Power OFF");
}
// Wait for the sampling interval
delay(samplingInterval);
}
Comments
Post a Comment