//#include <PZEM004Tv30.h>
#include <LiquidCrystal_I2C.h>
//#if defined(ESP32)
//PZEM004Tv30 pzem(Serial2, 16, 17);
//#else
//PZEM004Tv30 pzem(Serial2);
//#endif
//PZEM004Tv30 pzem(16, 17);
float voltage; //Variables of PZEM Sensor
float current;
float frequency;
float pf;
float power;
float energy;
const int sensorIn = 34; // pin where the OUT pin from sensor is connected on Arduino
int mVperAmp = 66; // this the 5A version of the ACS712 -use 100 for 20A Module and 66 for 30A Module
double Voltage_curr = 0;
double VRMS = 0;
double AmpsRMS = 0; //Varibles for Acs712 Sensor
const int sensorIn2 = 35; // pin where the OUT pin from sensor is connected on Arduino
int mVperAmp2 = 66; // this the 5A version of the ACS712 -use 100 for 20A Module and 66 for 30A Module
double Voltage_curr2 = 0;
double VRMS2 = 0;
double AmpsRMS2 = 0; //Varibles for Acs712 Sensor
// initialize the LCD library with I2C address and LCD size
LiquidCrystal_I2C lcd (0x27, 16,2);
//Relay
const int relayin = 8;
//pf warning led
const int pf_warning_led = 9;
void setup() {
Serial.begin(115200);
analogReadResolution(12);
// Initialize the LCD connected
lcd.init ();
// Turn on the backlight on LCD.
lcd. backlight ();
pinMode(relayin, OUTPUT);
digitalWrite(relayin, LOW);
pinMode(pf_warning_led,OUTPUT);
}
void loop() {
//Read_pzem();
Read_curr();
Read_curr2();
//Serial_print_pzem();
relay();
power_factor_warning();
delay(10);
}
// void Read_pzem()
// {
// voltage = pzem.voltage();
// if(voltage < 0.0){voltage = 0.0;}
// current = pzem.current();
// if(current < 0.0){current = 0.0;}
// power = pzem.power();
// if(power < 0.0){power = 0.0;}
// energy = pzem.energy();
// if(energy < 0.0){energy = 0.0;}
// frequency = pzem.frequency();
// if(frequency < 0.0){frequency = 0.0;}
// pf = pzem.pf();
// if(pf < 0.0){pf = 0.0;}
// }
// void Serial_print_pzem()
// {
// Serial.println("voltage = "); Serial.print(voltage); Serial.print("V");
// Serial.println("current = "); Serial.print(current); Serial.print("A");
// Serial.println("power = "); Serial.print(power); Serial.print("W");
// Serial.println("Frequency = "); Serial.print(frequency); Serial.print("Hz");
// Serial.println("energy = "); Serial.print(energy); Serial.print("Kwh");
// Serial.println("pf = "); Serial.print(pf);
// Serial.println("\n");
// }
float getVPP(int sensorpin)
{
float result;
int readValue; // value read from the sensor
int maxValue = 0; // store max value here
int minValue = 4096; // store min value here ESP32 ADC resolution
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorpin);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the minimum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 3.3)/4096.0; //ESP32 ADC resolution 4096
return result;
}
void Read_curr()
{
Voltage_curr = getVPP(sensorIn);
VRMS = (Voltage_curr/2.0) *0.707; //root 2 is 0.707
AmpsRMS = ((VRMS * 1000)/mVperAmp); //0.3 is the error I got for my sensor
Serial.print(AmpsRMS);
Serial.print(" Amps RMS --- ");
Serial.println(" ");
lcd.setCursor (0, 0);
lcd.print (AmpsRMS);
lcd.print (" Amps ");
}
void Read_curr2()
{
Voltage_curr2 = getVPP(sensorIn2);
VRMS2 = (Voltage_curr2/2.0) *0.707; //root 2 is 0.707
AmpsRMS2 = ((VRMS2 * 1000)/mVperAmp2); //0.3 is the error I got for my sensor
// Serial.print(AmpsRMS2);
// Serial.print(" Amps RMS2 --- ");
// lcd.setCursor (0, 0);
// lcd.print (AmpsRMS2);
// lcd.print (" Amps ");
}
void relay()
{
// if(voltage < 190.0)
// {
// digitalWrite(relayin, HIGH);
// lcd.setCursor(0, 1);
// lcd.print("Under Voltage ");
// while(1);
// }
// else{}
// if(voltage > 240.0)
// {
// digitalWrite(relayin, HIGH);
// lcd.setCursor(0, 1);
// lcd.print("Over Voltage "); // specify name to the corresponding value to be printed
// while(1);
// }
// else{}
if(AmpsRMS > 15.0)
{
digitalWrite(relayin, HIGH);
lcd.setCursor(0, 0);
lcd.print("Over Load "); // specify name to the corresponding value to be printed
while(1);
}
else{}
// if((AmpsRMS - AmpsRMS2) > 0.5)
// {
// digitalWrite(relayin, HIGH);
// lcd.setCursor(0, 0);
// lcd.print("Earth Fault "); // specify name to the corresponding value to be printed
// while(1);
// }
// else{}
}
void power_factor_warning()
{
if(pf < 0.85)
{
digitalWrite(pf_warning_led, HIGH);
}
else{
digitalWrite(pf_warning_led, LOW);
}
}