สวัสดีครับ ตอนนี้ผมกำลังทำโปรเจ็ค วัดไฟฟ้า 3 เฟส ด้วย PZEM004T +ESP8266 ส่งเข้า BLYNK
ลองดูตัวอย่างและ ดัดแปลงเรื่อยๆ แต่สรุปแล้ว Energy ที่บวกออกมาค่าไม่ตรงกับทั้ง 3 เฟส เลยคิดค่าไฟฟ้าไม่ตรงครับ อยากรบกวนทุกท่านที่ให้คำปรึกษาได้ ว่าจะต้องแก้ไขตรงไหน และ เพิ่มอะไรอีกได้ครับ
ขอบคุณล่วงหน้าครับ
โค๊ด
/*
-PZEM004T-
5V - 5V
GND - GND
D3 - TX(PZEM1)
D4 - RX(PZEM1)
D5 - TX(PZEM2)
D6 - RX(PZEM2)
D7 - TX(PZEM3)
D0 - RX(PZEM3)
*/
#include <ESP8266WiFi.h>
#include <PZEM004Tv30.h>
#include <BlynkSimpleEsp8266.h>
#define PIN_RESET -1
#define DC_JUMPER 0
#define BLYNK_PRINT Serial
PZEM004Tv30 pzem1(D3, D4); //rx,tx pin for phase1
PZEM004Tv30 pzem2(D5, D6); //rx,tx pin for phase2
PZEM004Tv30 pzem3(D7, D0); //rx,tx pin for phase3
unsigned long previousMillis = 0;
float voltage1, current1, power1, energy1, frequency1, pf1;
float voltage2, current2, power2, energy2, frequency2, pf2;
float voltage3, current3, power3, energy3, frequency3, pf3;
float rate_1 = 0;
float rate_2 = 0;
float rate_3 = 0;
float cost_total;
char ssid[] = "HUAWEI nova 3i";
char pass[] = "00000000";
char auth[] = "av05uggUksb6XHOp-hFvLTHiPRBoMKim";
uint8_t logo_bmp[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xC0, 0xF0, 0xE0, 0x78, 0x38, 0x78, 0x3C, 0x1C, 0x3C, 0x1C, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1C, 0x3C, 0x1C, 0x3C, 0x78, 0x38, 0xF0, 0xE0, 0xF0, 0xC0, 0xC0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x00, 0xF0, 0xF8, 0x70, 0x3C, 0x3C, 0x1C, 0x1E, 0x1E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0E, 0x0E, 0x1E, 0x1E, 0x1E, 0x3C, 0x1C, 0x7C, 0x70, 0xF0, 0x70, 0x20, 0x01, 0x01, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x1C, 0x3E, 0x1E, 0x0F, 0x0F, 0x07, 0x87, 0x87, 0x07, 0x0F, 0x0F, 0x1E, 0x3E, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x1F, 0x1F, 0x3F, 0x3F, 0x1F, 0x1F, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
//for reset energy
BLYNK_WRITE(V7)
{
int pinValue = param.asInt();
if (pinValue == HIGH) {
Serial.println("Reset energy");
pzem1.resetEnergy();
}
}
BLYNK_WRITE(V14)
{
int pinValue = param.asInt();
if (pinValue == HIGH) {
Serial.println("Reset energy");
pzem2.resetEnergy();
}
}
BLYNK_WRITE(V21)
{
int pinValue = param.asInt();
if (pinValue == HIGH) {
Serial.println("Reset energy");
pzem3.resetEnergy();
}
}
void setup() {
Serial.begin(115200);
// pzem.resetEnergy(); //reset energy
Blynk.begin(auth, ssid, pass, "blynk.honey.co.th", 8080);
}
void loop() {
Blynk.run();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) { //run every 1 second
previousMillis = currentMillis;
displayValue(); //update OLED
//------sent data to blynk------
//phase1 (V7 for reset energy)
if (!isnan(voltage1)) {
Blynk.virtualWrite(V1, voltage1);
Blynk.virtualWrite(V2, current1);
Blynk.virtualWrite(V3, power1);
Blynk.virtualWrite(V4, energy1);
Blynk.virtualWrite(V5, frequency1);
Blynk.virtualWrite(V6, pf1);
}
//phase2 (V14 for reset energy)
if (!isnan(voltage2)) {
Blynk.virtualWrite(V8, voltage2);
Blynk.virtualWrite(V9, current2);
Blynk.virtualWrite(V10, power2);
Blynk.virtualWrite(V11, energy2);
Blynk.virtualWrite(V12, frequency2);
Blynk.virtualWrite(V13, pf2);
}
//phase3 (V21 for reset energy)
if (!isnan(voltage3)) {
Blynk.virtualWrite(V15, voltage3);
Blynk.virtualWrite(V16, current3);
Blynk.virtualWrite(V17, power3);
Blynk.virtualWrite(V18, energy3);
Blynk.virtualWrite(V19, frequency3);
Blynk.virtualWrite(V20, pf3);
}
}
}
void displayValue() {
//------read data------
//phase1
voltage1 = pzem1.voltage();
current1 = pzem1.current();
power1 = pzem1.power();
energy1 = pzem1.energy();
frequency1 = pzem1.frequency();
pf1 = pzem1.pf();
//phase2
voltage2 = pzem2.voltage();
current2 = pzem2.current();
power2 = pzem2.power();
energy2 = pzem2.energy();
frequency2 = pzem2.frequency();
pf2 = pzem2.pf();
//phase3
voltage3 = pzem3.voltage();
current3 = pzem3.current();
power3 = pzem3.power();
energy3 = pzem3.energy();
frequency3 = pzem3.frequency();
pf3 = pzem3.pf();
//คิดค่าไฟฟ้า
float AMenergy = pzem1.energy()+pzem2.energy()+pzem3.power();
if( !isnan(AMenergy) ){
Serial.print("AMenergy: "); Serial.print(AMenergy,3); Serial.println("kWh");
Blynk.virtualWrite(V23,AMenergy,4);
if (AMenergy > 0 && AMenergy <= 150) { //หน่วยที่ 0-150
rate_1 = AMenergy * 3.2484;
}
if (AMenergy > 150) {
rate_1 = 150 * 3.2484;
}
if (AMenergy >= 151 && AMenergy <= 400) { //หน่วยที่ 151-400
rate_2 = (AMenergy - 150) * 4.2218;
}
if (AMenergy > 400) { //เกิน 400
rate_2 = 250 * 4.2218;
rate_3 = (AMenergy - 400) * 4.4217;
}
float ft = AMenergy*(-0.116);
float total_vat = (rate_1 + rate_2 + rate_3 + ft + 38.22)*0.07;
float total = (rate_1 + rate_2 + rate_3 + ft + 38.22) + total_vat;
cost_total = total;
Blynk.virtualWrite(V24,total,1);
//------Serial display------
//phase1
Serial.print("Voltage: "); Serial.print(voltage1); Serial.println("V");
Serial.print("Current: "); Serial.print(current1); Serial.println("A");
Serial.print("Power: "); Serial.print(power1); Serial.println("W");
Serial.print("Energy: "); Serial.print(energy1, 3); Serial.println("kWh");
Serial.print("Frequency: "); Serial.print(frequency1, 1); Serial.println("Hz");
Serial.print("PF: "); Serial.println(pf1); Serial.println("");
//phase2
Serial.print("Voltage: "); Serial.print(voltage2); Serial.println("V");
Serial.print("Current: "); Serial.print(current2); Serial.println("A");
Serial.print("Power: "); Serial.print(power2); Serial.println("W");
Serial.print("Energy: "); Serial.print(energy2, 3); Serial.println("kWh");
Serial.print("Frequency: "); Serial.print(frequency2, 1); Serial.println("Hz");
Serial.print("PF: "); Serial.println(pf2); Serial.println("");
//phase3
Serial.print("Voltage: "); Serial.print(voltage3); Serial.println("V");
Serial.print("Current: "); Serial.print(current3); Serial.println("A");
Serial.print("Power: "); Serial.print(power3); Serial.println("W");
Serial.print("Energy: "); Serial.print(energy3, 3); Serial.println("kWh");
Serial.print("Frequency: "); Serial.print(frequency3, 1); Serial.println("Hz");
Serial.print("PF: "); Serial.println(pf3); Serial.println("");
}
}