#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLc1ftqugS"
#define BLYNK_DEVICE_NAME "Energy Monitor"
#define BLYNK_AUTH_TOKEN "r4Aue2xjegpJO7cIJdVIm7aLfxeJ2Ozl"
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
BlynkTimer timer;
unsigned long previousMillis = 0;
unsigned long interval = 30000;
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
#define wifiLed 2 //LED1
int wifiFlag = 0;
//VOLTAGE INPUTS
#define Rvin 36
#define Yvin 39
#define Bvin 34
//CT INPUTS
const int CTR = 32; //set ESP signal read pin
const int CTY = 33;
const int CTB = 35;
float PF = 0.95; // Aprox Power factor value
#define CTRange 20; //set Non-invasive AC Current Sensor range (5A,10A,20A)
// VREF: Analog reference
// For Arduino UNO, Leonardo and mega2560, etc. change VREF to 5
// For Arduino Zero, Due, MKR Family, ESP32, etc. 3V3 controllers, change VREF to 3.3
#define VREF 3.3
BLYNK_CONNECTED()
{
Blynk.syncAll();
}
//R Phase CT
float ReadCTRValue()
{
float CTRVal = 0;
float RpeakVoltage = 0;
float RvoltageVirtualValue = 0; //Vrms
for (int i = 0; i < 5; i++)
{
RpeakVoltage += analogRead(CTR); //read peak voltage
delay(1);
}
RpeakVoltage = RpeakVoltage / 5;
RvoltageVirtualValue = RpeakVoltage * 0.707; //change the peak voltage to the Virtual Value of voltage
/*The circuit is amplified by 2 times, so it is divided by 2.*/
RvoltageVirtualValue = (RvoltageVirtualValue / 4096 * VREF ) / 2;
CTRVal = RvoltageVirtualValue * CTRange;
return CTRVal;
}
//Y Phase CT
float ReadCTYValue()
{
float CTYVal = 0;
float YpeakVoltage = 0;
float YvoltageVirtualValue = 0; //Vrms
for (int i = 0; i < 5; i++)
{
YpeakVoltage += analogRead(CTY); //read peak voltage
delay(1);
}
YpeakVoltage = YpeakVoltage / 5;
YvoltageVirtualValue = YpeakVoltage * 0.707; //change the peak voltage to the Virtual Value of voltage
/*The circuit is amplified by 2 times, so it is divided by 2.*/
YvoltageVirtualValue = (YvoltageVirtualValue / 4096 * VREF ) / 2;
CTYVal = YvoltageVirtualValue * CTRange;
return CTYVal;
}
//B Phase CT
float ReadCTBValue()
{
float CTBVal = 0;
float BpeakVoltage = 0;
float BvoltageVirtualValue = 0; //Vrms
for (int i = 0; i < 5; i++)
{
BpeakVoltage += analogRead(CTB); //read peak voltage
delay(1);
}
BpeakVoltage = BpeakVoltage / 5;
BvoltageVirtualValue = BpeakVoltage * 0.707; //change the peak voltage to the Virtual Value of voltage
/*The circuit is amplified by 2 times, so it is divided by 2.*/
BvoltageVirtualValue = (BvoltageVirtualValue / 4096 * VREF ) / 2;
CTBVal = BvoltageVirtualValue * CTRange;
return CTBVal;
}
void setup()
{
Serial.begin(115200);
initWiFi();
pinMode(CTR, INPUT);
pinMode(CTY, INPUT);
pinMode(CTB, INPUT);
pinMode(Rvin, INPUT);
pinMode(Yvin, INPUT);
pinMode(Bvin, INPUT);
timer.setInterval(500L, VandI);
Blynk.begin(auth, ssid, pass);
}
// get maximum reading value R Phase
int Rmax() {
int maxR = 0;
for (int R = 0; R < 100; R++) {
int Rphase = analogRead(Rvin); // read from analog channel 0 (A0)
if (maxR < Rphase) maxR = Rphase;
delayMicroseconds(100);
}
return maxR;
}
// get maximum reading value Y Phase
int Ymax() {
int maxY = 0;
for (int Y = 0; Y < 100; Y++) {
int Yphase = analogRead(Yvin); // read from analog channel 1 (A1)
if (maxY < Yphase) maxY = Yphase;
delayMicroseconds(100);
}
return maxY;
}
// get maximum reading value B Phase
int Bmax() {
int maxB = 0;
for (int B = 0; B < 100; B++) {
int Bphase = analogRead(Bvin); // read from analog channel 2 (A2)
if (maxB < Bphase) maxB = Bphase;
delayMicroseconds(100);
}
return maxB;
}
void loop()
{
unsigned long currentMillis = millis();
// if WiFi is down, try reconnecting every CHECK_WIFI_TIME seconds
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >= interval)) {
Serial.print(millis());
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
previousMillis = currentMillis;
}
Blynk.run();
timer.run();
bool isconnected = Blynk.connected();
if (isconnected == true) {
wifiFlag = 1;
digitalWrite(wifiLed, HIGH);
}
if (isconnected == false) {
wifiFlag = 0;
digitalWrite(wifiLed, HIGH);
delay(300);
digitalWrite(wifiLed, LOW);
delay(300);
}
}
void VandI()
{
//R Phase Checks
float CTRVal = ReadCTRValue(); //read AC Current Value of R
Serial.print(CTRVal);
Serial.println(" A/Rphase");
Blynk.virtualWrite(V4, CTRVal);
float CTYVal = ReadCTYValue(); //read AC Current Value of Y
Serial.print(CTYVal);
Serial.println(" A/Yphase");
Blynk.virtualWrite(V5, CTYVal);
float CTBVal = ReadCTBValue(); //read AC Current Value of B
Serial.print(CTBVal);
Serial.println(" A/Bphase");
Blynk.virtualWrite(V6, CTBVal);
//Voltage Measurment
//R phase output
char RVolt[10];
// get amplitude (maximum - or peak value)
uint32_t vrout = Rmax();
// get actual voltage (ADC voltage reference = 1.1V)
vrout = vrout * 1100 / 4095;
// calculate the RMS value ( = peak/√2 )
vrout /= sqrt(2);
Serial.print(vrout);
Serial.println(" V/Rphase");
Blynk.virtualWrite(V1, vrout);
//Y phase output
char YVolt[10];
uint32_t vyout = Ymax();
vyout = vyout * 1100 / 4095;
vyout /= sqrt(2);
Serial.print(vyout );
Serial.println(" V/Yphase");
Blynk.virtualWrite(V2, vyout);
//B phase output
char BVolt[10];
uint32_t vbout = Bmax();
vbout = vbout * 1100 / 4095;
vbout /= sqrt(2);
Serial.print(vbout );
Serial.println(" V/Bphase");
Blynk.virtualWrite(V3, vbout);
//Power Calculation approx
int RPower = vrout * CTRVal * PF;
int YPower = vyout * CTYVal * PF;
int BPower = vbout * CTBVal * PF;
Serial.print(RPower);
Serial.println(" Watts/Rphase");
Blynk.virtualWrite(V10, RPower);
Blynk.virtualWrite(V11, YPower);
Blynk.virtualWrite(V12, BPower);
}