IOT ENERGY METER UPDATED CODE

 #include <Wire.h>

#include <LiquidCrystal_I2C.h>

#include "EmonLib.h"             // Include Emon Library

#include "ACS712.h"              // Include ACS712 Library


// Set the LCD address (usually 0x27 or 0x3F)

#define I2C_ADDR 0x27


// Define LCD size (rows and columns)

#define LCD_ROWS 2

#define LCD_COLS 16


// Create an instance of the LCD class

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);


#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 totalEnergy = 0;              // Variable to store the total energy consumption

unsigned long lastSampleTime = 0;   // Variable to keep track of the last sample time


void setup() {

  Serial.begin(9600);

  emon1.voltage(1, VOLT_CAL, 1.7);  // Voltage sensor initialization

  sensor.calibrate();               // Current sensor calibration


  // Initialize the LCD

  lcd.init();

  // Turn on the backlight (optional)

  lcd.backlight();

}


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


    // Current sensing

    float current = sensor.getCurrentAC();      // Read current


    // Power calculation

    float power = supplyVoltage * current;      // Power in Watts (P = VI)


    // Energy calculation

    if (supplyVoltage >= 100) {

      // Store the voltage value

      voltageSamples[sampleIndex] = supplyVoltage;

      // Increment sampleIndex and loop back if necessary

      sampleIndex = (sampleIndex + 1) % numSamples;


      // Integrate power over time to calculate energy

      totalEnergy += (power * samplingInterval / 3600000.0); // Energy in Watt-hours


      // Print the total energy consumption

      Serial.print("Total energy consumption: ");

      Serial.print(totalEnergy);

      Serial.println(" Wh");


      // Update LCD with the current readings

      lcd.clear();

      lcd.setCursor(0, 0); // Set cursor to first row

      lcd.print("V: ");

      lcd.print(supplyVoltage, 2); // Print voltage with 2 decimal places

      lcd.print("V I: ");

      lcd.print(current, 2); // Print current with 2 decimal places

      lcd.setCursor(0, 1); // Set cursor to second row

      lcd.print("P: ");

      lcd.print(power, 2); // Print power with 2 decimal places

      lcd.print("W E: ");

      lcd.print(totalEnergy, 2); // Print energy with 2 decimal places

      lcd.print("Wh");

    } else {

      Serial.println("Power OFF");

      // Display "Power OFF" on the LCD

      lcd.clear();

      lcd.setCursor(0, 0);

      lcd.print("Power OFF");

    }

  }

}


Comments

Popular posts from this blog

BASICS OF ARDUINO UNO - LED BLINKING

ARDUINO UNO CODE TO DISPLAY HELLO WORLD !

ESP 32