//https://www.pcbway.com/blog/technology/High_Precision_Digital_AC_Energy_Meter_Circuit_Voltage_Current_Power_KWh_3a6bf090.html
#include "HLW8032.h"
#include <LiquidCrystal.h>
#define SYNC_TIME 500
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
static unsigned long start_time, end_time;
HLW8032 HL;
void setup()
{
lcd.begin(20, 4);
HL.begin(Serial, 4);
start_time = millis();
}
void loop()
{
end_time = millis();
HL.SerialReadLoop();
if (end_time - start_time > SYNC_TIME)
{
if (HL.SerialRead == 1)
{
lcd.setCursor(0, 0);
lcd.print("Voltage[V]: ");
lcd.setCursor(12, 0);
lcd.print(HL.GetVol() * 0.001, 2);
lcd.setCursor(0, 1);
lcd.print("Current[A]: ");
lcd.setCursor(12, 1);
lcd.print(HL.GetCurrent(), 2);
lcd.setCursor(0, 2);
lcd.print("Power[W]: ");
lcd.setCursor(10, 2);
lcd.print(HL.GetActivePower() * 0.001, 2);
lcd.setCursor(0, 3);
lcd.print("Energy[KWh]: ");
lcd.setCursor(13, 3);
lcd.print(HL.GetKWh(), 2);
}
start_time = end_time;
}
}