#define BLYNK_PRINT Serial
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <ModbusMaster.h>
#define nodemcu Serial
#include <ModbusMaster.h>
ModbusMaster node1;//pzem1
ModbusMaster node2;//pzem2
ModbusMaster node3;//pzem3
ModbusMaster node4;
int vrn, vsn, vtn, vnn, vrs, vst, vtr, arus1, arus2, arus3, arus4, iavg, freq, kwh1, kwh2, kwh3, kwhtot;
uint8_t result1;
uint8_t result2;
uint8_t result3;
uint8_t result4;
uint16_t data[40];
#define BLYNK_TEMPLATE_ID "TMPLHakUdinP"
#define BLYNK_DEVICE_NAME "xxxxxx"
#define BLYNK_AUTH_TOKEN "xxxx"
char auth[] = "xxxxx";//
char ssid[] = "SIMOR#2020";
char pass[] = "simor123";
BlynkTimer timer;
WidgetRTC rtc;
void simor()
{
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
Blynk.virtualWrite(V20, currentTime);
Blynk.virtualWrite(V21, currentDate);
//pzem1
result1 = node1.readInputRegisters(0x0000, 10);
if (result1 == node1.ku8MBSuccess) {
vrn = (node1.getResponseBuffer(0x00) / 10.0f);
arus1 = (node1.getResponseBuffer(0x01) / 1000.000f) ;
freq = (node1.getResponseBuffer(0x07) / 10.0f);
kwh1 = (node1.getResponseBuffer(0x05) / 1000.0f);
}
delay(100);
result2 = node2.readInputRegisters(0x0000, 10);
if (result2 == node2.ku8MBSuccess) {
vsn = (node2.getResponseBuffer(0x00) / 10.0f);
arus2 = (node2.getResponseBuffer(0x01) / 1000.000f) ;
kwh2 = (node2.getResponseBuffer(0x05) / 1000.0f);
}
delay(100);
result3 = node3.readInputRegisters(0x0000, 10);
if (result3 == node3.ku8MBSuccess) {
vtn = (node3.getResponseBuffer(0x00) / 10.0f);
arus3 = (node3.getResponseBuffer(0x01) / 1000.000f) ;
kwh3 = (node3.getResponseBuffer(0x05) / 1000.0f);
}
delay(100);
vrs = ((vrn + vsn) / 2) * 1.73;
vst = ((vsn + vtn) / 2) * 1.73;
vtr = ((vrn + vtn) / 2) * 1.73;
iavg = (arus1 + arus2 + arus3) / 3;
kwhtot = kwh1 + kwh2 + kwh3;
Blynk.virtualWrite(V0, vrn); //
Blynk.virtualWrite(V1, vsn); //
Blynk.virtualWrite(V2, vtn); //
Blynk.virtualWrite(V3, vrs); //
Blynk.virtualWrite(V4, vst); //
Blynk.virtualWrite(V5, vtr); //
Blynk.virtualWrite(V6, arus1); //
Blynk.virtualWrite(V7, arus2); //
Blynk.virtualWrite(V8, arus3); //
Blynk.virtualWrite(V9, iavg); //
Blynk.virtualWrite(V10, freq); //
Blynk.virtualWrite(V11, kwh1); //
Blynk.virtualWrite(V12, kwh2); //
Blynk.virtualWrite(V13, kwh3); //
Blynk.virtualWrite(V14, kwhtot); //
lcd.setCursor(0, 0);
lcd.print("KWH IOT 3 PHASE");
lcd.setCursor(0, 1);
lcd.print(arus1);
lcd.setCursor(5, 1);
lcd.print(arus2);
lcd.setCursor(10, 1);
lcd.print(arus3);
lcd.setCursor(15, 1);
lcd.print(arus4);
/*
lcd.setCursor(0, 2);
lcd.print(vrn);
lcd.setCursor(5, 2);
lcd.print(vsn);
lcd.setCursor(10, 2);
lcd.print(vtn);
lcd.setCursor(15, 2);
lcd.print(vnn);
lcd.setCursor(0, 3);
lcd.print(vrs);
lcd.setCursor(5, 3);
lcd.print(vst);
lcd.setCursor(10, 3);
lcd.print(vtr);
lcd.setCursor(15, 3);
lcd.print(freq);
*/
}
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
}
void setup() {
Serial.begin(9600);
nodemcu.begin(9600);
node1.begin(1, nodemcu);
node2.begin(2, nodemcu);
node3.begin(3, nodemcu);
Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
timer.setInterval(2000L, simor);
setSyncInterval(60 * 60); // Sync interval in seconds (60 minutes)
lcd.init(); // initialize the lcd
lcd.init();
lcd.backlight();
lcd.setCursor(6, 0);
lcd.print("WELCOME");
lcd.setCursor(0, 1);
lcd.print("KWH IOT 3 PHASE");
/*
lcd.setCursor(0, 2);
lcd.print("SIMOR TECHNOLOGY)");
lcd.setCursor(0, 3);
lcd.print("By: Fuad Hasan");
*/
delay(5000);
lcd.clear();
}
void loop() {
Blynk.run();
timer.run();
}