/*
// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
#include <EEPROM.h>
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
EnergyMonitor emon2; // Create an instance
EnergyMonitor emon3; // Create an instance
EnergyMonitor emon4; // Create an instance
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);
#define Wat_Address1 100
#define Wat_Address2 200
#define Wat_Address3 300
#define kwh_Address1 400
#define kwh_Address2 500
#define kwh_Address3 600
#define Cost_Address1 700
#define Cost_Address2 800
#define Cost_Address3 900
#define Relay1_Address1 1100
#define Relay2_Address2 1200
#define Relay3_Address3 1300
#define Relay4_Address4 1400
int vCalibration = 162.01;
int currCalibration1 = 01.0001;
int currCalibration2 = 01.0001;
int currCalibration3 = 01.0001;
unsigned long unt1=3600000000.0;//unt sate
unsigned long unt2=3600000000.0;//unt sate
unsigned long unt3=3600000000.0;//unt sate
unsigned long unt4=3600000000.0;//unt sate
unsigned long lastMillis1 = millis();
unsigned long lastMillis2 = millis();
unsigned long lastMillis3 = millis();
unsigned long lastMillis4 = millis();
float supplyVoltage;
float apparentPower1;
float apparentPower2;
float apparentPower3;
float apparentPower4;
float powerFActor=0.0;
float realPower;
float Irms;
float kWh1 = 0.0;
float kWh2 = 0.0;
float kWh3 = 0.0;
float kWh4 = 0.0;
float estimatedCost1 = 0.0;
float estimatedCost2 = 0.0;
float estimatedCost3 = 0.0;
float estimatedCost4 = 0.0;
int estimatedCost_int1;
int estimatedCost_int2;
int estimatedCost_int3;
const float costPerKWh1 = 10; // Assuming 10 USD per kWh
const float costPerKWh2 = 10; // Assuming 10 USD per kWh
const float costPerKWh3 = 10; // Assuming 10 USD per kWh
const float costPerKWh4 = 10; // Assuming 10 USD per kWh
double secondsRaw;
double hours,kWh;
int hoursToShow;
double minutes;
int minutesToShow;
double seconds;
int secondsToShow;
float c_sate = 0.0;
float Power_of1 = 0.0;
float Power_of2 = 0.0;
float Power_of3 = 0.0;
int Power1_Mode;
int Power2_Mode;
int Power3_Mode;
int Power4_Mode;
int Mode;
int timer;
int value;char key;
void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Electronic India");
lcd.setCursor(3, 1);
lcd.print("9720262382");
Serial.print("9720262382");
emon1.voltage(A0,vCalibration,1.7); // Voltage: input pin, calibration, phase_shift
emon2.current(A1,currCalibration1); // Current: input pin, calibration.
emon3.current(A2,currCalibration2); // Current: input pin, calibration.
emon4.current(A3,currCalibration3); // Current: input pin, calibration.
readEnergyDataFromEEPROM();
}
void loop(){
if(Serial.available() == 0) {}
value = Serial.parseInt();
key = Serial.parseInt();
if(value > 0)
{
if(key=='A'){key=' ';Mode++;}
if(Mode==6){Mode=5;}
Serial.println(Mode);
}
voltegSetup();Sategs1();
Power_on_of();
timer++;
Sategs1();
}
void Sategs1(){
if(Serial.available() == 0) {}
value = Serial.parseInt();
key = Serial.parseInt();
if(value > 0)
{
if(key=='A'){key=' ';Mode++;}
if(Mode==6){Mode=5;}
Serial.println(Mode);
if(Mode==1){
if(value==1){Power_of1++;Serial.println(Power_of1);}
if(value==2){Power_of1--;Serial.println(Power_of1);}
}
if(Mode==2){
if(value==1){Power1_Mode++;Serial.println(Power1_Mode);}
if(value==2){Power1_Mode--;Serial.println(Power1_Mode);}
}
if(Mode==3){
if(value==1){Power2_Mode++;Serial.println(Power2_Mode);}
if(value==2){Power2_Mode--;Serial.println(Power2_Mode);}
}
if(Mode==4){
if(value==1){Power3_Mode++;Serial.println(Power3_Mode);}
if(value==2){Power3_Mode--;Serial.println(Power3_Mode);}
}
if(Mode==5){
if(value==1){Power4_Mode++;Serial.println(Power4_Mode);}
if(value==2){Power4_Mode--;Serial.println(Power4_Mode);}
}
}
}
void Sategs2(){
if(Serial.available() == 0) {}
value = Serial.parseInt();
key = Serial.parseInt();
if(value > 0)
{
if(key=='A'){key=' ';Mode++;}
if(Mode==6){Mode=5;}
Serial.println(Mode);
if(Mode==1){
if(value==1){Power_of2++;Serial.println(Power_of2);}
if(value==2){Power_of2--;Serial.println(Power_of2);}
}
if(Mode==2){
if(value==1){Power1_Mode++;Serial.println(Power1_Mode);}
if(value==2){Power1_Mode--;Serial.println(Power1_Mode);}
}
if(Mode==3){
if(value==1){Power2_Mode++;Serial.println(Power2_Mode);}
if(value==2){Power2_Mode--;Serial.println(Power2_Mode);}
}
if(Mode==4){
if(value==1){Power3_Mode++;Serial.println(Power3_Mode);}
if(value==2){Power3_Mode--;Serial.println(Power3_Mode);}
}
if(Mode==5){
if(value==1){Power4_Mode++;Serial.println(Power4_Mode);}
if(value==2){Power4_Mode--;Serial.println(Power4_Mode);}
}
}
}
void Sategs3(){
if(Serial.available() > 0) {}
value = Serial.read();
key = value;
if(value > 0)
{
if(key=='A'){key=' ';Mode++;Serial.print(Mode);}
if(Mode==6){Mode=5;}
if(key=='B'){key=' ';Mode--;}
if(Mode==0){Mode=1;}
if(Mode==1){
Serial.print("WateSate :");
Serial.println(key);
if(key=='1'){Power_of3++;}
if(key=='2'){Power_of3--;}
}
if(Mode==2){
Serial.print("PawerSate1 :");
Serial.println(Power1_Mode);
if(key=='1'){Power1_Mode++;}
if(key=='2'){Power1_Mode--;}
}
if(Mode==3){
Serial.print("PawerSate2 :");
Serial.println(Power2_Mode);
if(key=='1'){Power2_Mode++;}
if(key=='2'){Power2_Mode--;}
}
if(Mode==4){
Serial.print("PawerSate3 :");
Serial.println(Power3_Mode);
if(key=='1'){Power3_Mode++;}
if(key=='2'){Power3_Mode--;}
}
if(Mode==5){
Serial.print("PawerSate4 :");
Serial.println(Power4_Mode);
if(key=='1'){Power4_Mode++;}
if(key=='2'){Power4_Mode--;}
}
}
}
void Power_on_of(){
if(Power_of1 < apparentPower1){
Serial.println("Power:Of ");
}
if(Power_of1 > apparentPower1){
Serial.println("Power:On ");
}
if(Power_of2 < apparentPower2){
Serial.println("Power:Of ");
}
if(Power_of2 > apparentPower2){
Serial.println("Power:On ");
}
if(Power_of3 < apparentPower3){
Serial.println("Power:Of ");
}
if(Power_of3 < apparentPower3){
Serial.println("Power:On ");
}
if(Power1_Mode < supplyVoltage){
//Serial.println("Power:Of ");
}
if(Power2_Mode < supplyVoltage){
//Serial.println("Power:Of ");
}
if(Power3_Mode < supplyVoltage){
//Serial.println("Power:Of ");
}
if(Power4_Mode < supplyVoltage){
//Serial.println("Power:Of ");
}
}
void voltegSetup(){
emon2=emon1;emon3=emon1;emon4=emon1;
emon1.calcVI(20,150); // Calculate all. No.of half wavelengths (crossings), time-out
//emon1.serialprint(); // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)
if(timer==2){timer=0;
apparentPower1 = emon1.apparentPower *1.00/1.000; //extract Apparent Power into variable
apparentPower2 = emon2.apparentPower *1.00/1.000; //extract Apparent Power into variable
apparentPower3 = emon3.apparentPower *1.00/1.000; //extract Apparent Power into variable
apparentPower3 = emon4.apparentPower *1.00/1.000; //extract Apparent Power into variable
Irms = emon1.Irms; //extract Irms into Variable
supplyVoltage = emon1.Vrms *1.0/1.0; //extract Vrms into Variable
}
estimatedCost_int1 = static_cast<int>(estimatedCost1 * 100); // Estimated Cost multiplied by 100
realPower = emon1.realPower; //extract Real Power into variable
powerFActor = emon1.powerFactor; //extract Power Factor into Variable
unsigned long currentMillis1 = millis();
unsigned long currentMillis2 = millis();
unsigned long currentMillis3 = millis();
unsigned long currentMillis4 = millis();
kWh1 += apparentPower1 * (currentMillis1 - lastMillis1) / unt1;
kWh2 += apparentPower2 * (currentMillis2 - lastMillis2) / unt2;
kWh3 += apparentPower3 * (currentMillis3 - lastMillis3) / unt3;
kWh4 += apparentPower4 * (currentMillis4 - lastMillis4) / unt4;
estimatedCost1 = kWh1 * costPerKWh1;
estimatedCost2 = kWh2 * costPerKWh2;
estimatedCost3 = kWh3 * costPerKWh3;
estimatedCost4 = kWh4 * costPerKWh4;
lastMillis1 = currentMillis1;
lastMillis2 = currentMillis2;
lastMillis3 = currentMillis3;
lastMillis4 = currentMillis4;
saveEnergyDataToEEPROM();
}
void estimated_lcd1(){
lcd.setCursor(0, 0);
lcd.print("V");//realPower
if(supplyVoltage<100){lcd.print("0");}
if(supplyVoltage<10){lcd.print("0");}
lcd.print(supplyVoltage,0 *1.000/1.000);lcd.print(" ");
//TimePrent();
lcd.print("A");
lcd.print(Irms,3 *1.000/1.000);
lcd.print("P");
lcd.print(estimatedCost1);
lcd.setCursor(0, 1);
lcd.print("W");//realPower
if(apparentPower1<199){lcd.print("0");}
if(apparentPower1<99){lcd.print("0");}
if(apparentPower1<9){lcd.print("0");}
lcd.print(apparentPower1,2);
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print("KW");
if(kWh1<100){lcd.print("0");}
if(kWh1<10){lcd.print("0");}
lcd.print(kWh1);
}
void estimated_lcd2(){
lcd.setCursor(0, 0);
lcd.print("V");//realPower
if(supplyVoltage<100){lcd.print("0");}
if(supplyVoltage<10){lcd.print("0");}
lcd.print(supplyVoltage,0 *1.000/1.000);lcd.print(" ");
//TimePrent();
lcd.print("A");
lcd.print(Irms,3 *1.000/1.000);
lcd.print("P");
lcd.print(estimatedCost2);
lcd.setCursor(0, 1);
lcd.print("W");//realPower
if(apparentPower2<199){lcd.print("0");}
if(apparentPower2<99){lcd.print("0");}
if(apparentPower2<9){lcd.print("0");}
lcd.print(apparentPower2,2);
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print("KW");
if(kWh2<100){lcd.print("0");}
if(kWh2<10){lcd.print("0");}
lcd.print(kWh2);
}
void estimated_lcd3(){
lcd.setCursor(0, 0);
lcd.print("V");//realPower
if(supplyVoltage<100){lcd.print("0");}
if(supplyVoltage<10){lcd.print("0");}
lcd.print(supplyVoltage,0 *1.000/1.000);lcd.print(" ");
//TimePrent();
lcd.print("A");
lcd.print(Irms,3 *1.000/1.000);
lcd.print("P");
lcd.print(estimatedCost3);
lcd.setCursor(0, 1);
lcd.print("W");//realPower
if(apparentPower3<199){lcd.print("0");}
if(apparentPower3<99){lcd.print("0");}
if(apparentPower3<9){lcd.print("0");}
lcd.print(apparentPower3,2);
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print("KW");
if(kWh3<100){lcd.print("0");}
if(kWh3<10){lcd.print("0");}
lcd.print(kWh3);
}
void TimeSetup(){
secondsRaw = millis() / 1000;
hours = fmod(secondsRaw / 3600.0, 1);
hoursToShow = secondsRaw / 3600;
minutes = hours * 3600.0 / 60;
minutesToShow = minutes;
seconds = fmod(minutes, 1);
secondsToShow = seconds * 60;
/*
if(hoursToShow<9){Serial.print("0");}
Serial.print(hoursToShow);
if(minutesToShow<9){Serial.print("0");}
Serial.print(minutesToShow);
if(secondsToShow<9){Serial.print(":0");}
Serial.println(secondsToShow);
if(hoursToShow<9){lcd.print("0");}
lcd.print(hoursToShow);
lcd.print(":");
if(minutesToShow<9){lcd.print("0");}
lcd.print(minutesToShow);lcd.print(":");
if(secondsToShow<9){lcd.print("0");}
lcd.print(secondsToShow);
}
void readEnergyDataFromEEPROM(){
EEPROM.get(Wat_Address1,Power_of1);
EEPROM.get(Wat_Address2,Power_of2);
EEPROM.get(Wat_Address3,Power_of3);
EEPROM.get(Cost_Address1,estimatedCost_int1);
EEPROM.get(Cost_Address2,estimatedCost_int2);
EEPROM.get(Cost_Address3,estimatedCost_int3);
EEPROM.get(kwh_Address1,kWh1);
EEPROM.get(kwh_Address2,kWh2);
EEPROM.get(kwh_Address3,kWh3);
EEPROM.get(Relay1_Address1,Power1_Mode);
EEPROM.get(Relay2_Address2,Power2_Mode);
EEPROM.get(Relay3_Address3,Power3_Mode);
EEPROM.get(Relay4_Address4,Power4_Mode);
}
void saveEnergyDataToEEPROM(){
EEPROM.put(Wat_Address1,Power_of1);
EEPROM.put(Wat_Address2,Power_of2);
EEPROM.put(Wat_Address3,Power_of3);
EEPROM.put(Cost_Address1,estimatedCost_int1);
EEPROM.put(Cost_Address2,estimatedCost_int2);
EEPROM.put(Cost_Address3,estimatedCost_int2);
EEPROM.put(kwh_Address1,kWh1);
EEPROM.put(kwh_Address2,kWh2);
EEPROM.put(kwh_Address3,kWh3);
EEPROM.put(Relay1_Address1,Power1_Mode);
EEPROM.put(Relay2_Address2,Power2_Mode);
EEPROM.put(Relay3_Address3,Power3_Mode);
EEPROM.put(Relay4_Address4,Power4_Mode);
}
*/