#include <PZEM004Tv30.h>
PZEM004Tv30 pzem(Serial1, 16, 17);
void setup() {
Serial.begin(115200);
}
void loop() {
float voltage = pzem.voltage();
if(voltage != NAN){
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
} else {
Serial.println("Error reading voltage");
}
float current = pzem.current();
if(current != NAN){
Serial.print("Current: "); Serial.print(current); Serial.println("A");
} else {
Serial.println("Error reading current");
}
float power = pzem.power();
if(current != NAN){
Serial.print("Power: "); Serial.print(power); Serial.println("W");
} else {
Serial.println("Error reading power");
}
float energy = pzem.energy();
if(current != NAN){
Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
} else {
Serial.println("Error reading energy");
}
float frequency = pzem.frequency();
if(current != NAN){
Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
} else {
Serial.println("Error reading frequency");
}
float pf = pzem.pf();
if(current != NAN){
Serial.print("PF: "); Serial.println(pf);
} else {
Serial.println("Error reading power factor");
}
Serial.println();
delay(1500);
}
เบื้องต้น ทดลองสลับ สายดูก่อนครับ จาก 16 เป็น 17 17 เป็น 16
ขอบคุณที่แนะนำครับ ลองแล้วก็ยังเหมือนเดิม พอมีวิธีอื่นอีกไหมครับ
เอาข้อมูลมาจาก admin นะครับ
UART2 คือ Serial2 มันต่อขา 16 กับ 17 ถูกแล้วครับ
ปรับ Code อีกนิด
//---- PZEM --------------------------------------------
#include <PZEM004Tv30.h>
//ถ้าค่าไม่ขึ้นก็ลองสลับสายดูนะครับ
#define RX2 16
#define TX2 17
#include <HardwareSerial.h>
PZEM004Tv30 pzem(&Serial2);
void setup() {
Serial.begin(115200);//เวลาดู monitor ใช้ Boudrate 115200
Serial2.begin(9600, SERIAL_8N1, RX2, TX2);
}
void loop() {
float voltage = pzem.voltage();
if (voltage != NAN) {
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
} else {
Serial.println("Error reading voltage");
}
float current = pzem.current();
if (current != NAN) {
Serial.print("Current: "); Serial.print(current); Serial.println("A");
} else {
Serial.println("Error reading current");
}
float power = pzem.power();
if (current != NAN) {
Serial.print("Power: "); Serial.print(power); Serial.println("W");
} else {
Serial.println("Error reading power");
}
float energy = pzem.energy();
if (current != NAN) {
Serial.print("Energy: "); Serial.print(energy, 3); Serial.println("kWh");
} else {
Serial.println("Error reading energy");
}
float frequency = pzem.frequency();
if (current != NAN) {
Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
} else {
Serial.println("Error reading frequency");
}
float pf = pzem.pf();
if (current != NAN) {
Serial.print("PF: "); Serial.println(pf);
} else {
Serial.println("Error reading power factor");
}
Serial.println();
delay(1500);
}
คือถ้าใช้ลักษณะนี้ จะมีปัญหากับการ Upload ดังนั้นก่อน Upload ให้ถอดสาย RX TX ก่อนนะครับ เมื่อ Upload เสร็จก็เสียบสายไปใหม่
อธิบายเพิ่มเติมหน่อยได้ไหมครับ พอดีผมไม่ค่อยเข้าใจ
ลอง Upload code ดูก่่อนครับ ถ้ามันทำงาน ก็ให้ศักษาะรายละเอียดดูอีกที
ตอนนี้ผมทำสั่วนนั้นได้แล้วครับ ผมอย่างอ่าน3ตัวพอมีวิธีแนะนำไหมครับผมอ่านค่าได้มากสุดแค่2ตัวในบอร์ดเดียว
ศึกษาดูจากที่นี่นะครับ
C:\Users\PUYIOT\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\cores\esp32/HardwareSerial.cpp
ดูค่า 3 ชุดนี้ว่า มันเปลี่ยนค่าได้หรือปล่าว และถ้าเปลี่ยนได้ Serial monitor ของคุณ ก็จะไม่สามารถดูค่าอะไรได้อีก
ได้แล้วครับ ขอบคุณมากครับ
ของผมมีปัญหาต่อPZEM004 เข้ากับบอร์ด esp32 wroom 32 ไม่อ่านค่าอะไรเลยครับ
#include <PZEM004Tv30.h>
// Define the UART2 RX and TX pins on ESP32 (Connect these to PZEM-004T)
#define PZEM_RX_PIN 16 // ESP32 RX (Connect to PZEM TX)
#define PZEM_TX_PIN 17 // ESP32 TX (Connect to PZEM RX)
// Initialize the PZEM sensor using Hardware Serial2
PZEM004Tv30 pzem(Serial2, PZEM_RX_PIN, PZEM_TX_PIN);
void setup() {
Serial.begin(115200);
Serial.println("PZEM-004T V3.0 Power Meter - ESP32");
// Uncomment to reset the energy counter
// pzem.resetEnergy();
}
void loop() {
Serial.print("Custom Address: ");
Serial.println(pzem.readAddress(), HEX);
// Read data from the PZEM sensor
float voltage = pzem.voltage();
float current = pzem.current();
float power = pzem.power();
float energy = pzem.energy();
float frequency = pzem.frequency();
float pf = pzem.pf();
// Original error handling (unchanged)
if(isnan(voltage)){
Serial.println("Error reading voltage");
} else if (isnan(current)) {
Serial.println("Error reading current");
} else if (isnan(power)) {
Serial.println("Error reading power");
} else if (isnan(energy)) {
Serial.println("Error reading energy");
} else if (isnan(frequency)) {
Serial.println("Error reading frequency");
} else if (isnan(pf)) {
Serial.println("Error reading power factor");
} else {
// Print values
Serial.print("Voltage: "); Serial.print(voltage); Serial.println(" V");
Serial.print("Current: "); Serial.print(current); Serial.println(" A");
Serial.print("Power: "); Serial.print(power); Serial.println(" W");
Serial.print("Energy: "); Serial.print(energy, 3); Serial.println(" kWh");
Serial.print("Frequency: "); Serial.print(frequency); Serial.println(" Hz");
Serial.print("Power Factor: "); Serial.println(pf);
}
Serial.println();
delay(2000); // Wait 2 seconds before next reading
}
ต่อยังไงถ่ายมาดูหน่อยครับ