Code dành cho 1 node.
/*
NodeMCU-Esp8266-----------------------------PZEM004T
Vcc-3.3V(only test) ---------------------------5V
GND ----------------------------------------GND
RX <--------------<----------------------TX_PZEM(Tx1)
TX --------------->--------------------->RX_PZEM(Rx1)
*/
#include "Pzem004t_V3.h"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define ESP8266_BAUD 9600
//Pzem004t_V3 pzem(TX_PZEM,RX_PZEM);
Pzem004t_V3 pzem(&Serial);
BlynkTimer timer;
char auth[] = "eYGsVL2x8zAtmWzBAXDOz0rniGfaWYY5"; //Điền mã Auth Token trong Blynk App.
char ssid[] = "No internet Access"; // Tên Wifi nhà bạn (Gõ đúng tên, Chữ hoa chữ thường)
char pass[] = "12345678"; // Mật khẩu wifi nhà bạn
void setup() {
// Set ESP8266 baud rate
Serial.begin(ESP8266_BAUD);
delay(100);
pzem.begin();
pzem.setTimeout(100);
// Blynk.begin(auth, ssid, pass);
Blynk.begin(auth, ssid, pass, IPAddress(xxx,xxx,xxx,xxx), 9090);
Serial.print("Connecting ");
timer.setInterval(500L, SendToBlynk);
delay(100);
}
void SendToBlynk()
{
pzem_info pzemData = pzem.getData();
Blynk.virtualWrite(V1, pzemData.volt);
Blynk.virtualWrite(V2, pzemData.ampe);
Blynk.virtualWrite(V3, pzemData.power);
Blynk.virtualWrite(V4, pzemData.energy);
Blynk.virtualWrite(V5, pzemData.powerFactor);
Blynk.virtualWrite(V6, pzemData.freq);
Serial.println("volt: "+String(pzemData.volt));
}
BLYNK_WRITE(V0) { // Button Widget chọn V0
if (param.asInt() == 1) { //nếu nhấn nút reset thì gọi hàm
pzem.resetEnergy(); // reset PZEM
} else {
}
}
void loop() {
Blynk.run();
timer.run();
delay(500);
}