Multiple PZEM

ด้วยความบังเอิญที่จะต้องทำการวัดไฟฟ้า ด้วย PZEM จากบอร์ดเดียวกันโดยใช้ PZEM มากกว่า 1 ตัว

มีสองทางเลือก คือ (1) การใช้ port software serial เดียวกัน และจัดการด้วย library ของ PZEM โดยวิธีนี้จำเป็นจะต้องทำการ config address ด้วย เครื่องมืออื่นก่อน จึงยังไม่สะดวกทำ

และอีกวิธีคือ (2) การใช้ port แยกกัน โดยไม่ต้องเปลี่ยน address คือ ใช้ 0x01 เหมือนกัน

โดยตัวแรกต่อที่ 11, 12 และอีกตัวต่อที่ 5,6 โดยการทดลองนี้ใช้ Uno ได้ผลตาม Clip ตามรูป

#include <PZEM004Tv30.h>
/* Use software serial for the PZEM
 * Pin 11 Rx (Connects to the Tx pin on the PZEM)
 * Pin 12 Tx (Connects to the Rx pin on the PZEM)
*/
PZEM004Tv30 sensor[3] = {
  PZEM004Tv30(12,11, 0x01),
  PZEM004Tv30(5, 6, 0x01),
  PZEM004Tv30(12,11, 0x03)
};
void setup() {
  Serial.begin(74880);
}
void loop() {
  for (int i=0; i<3; i++){
    Serial.print("SENSOR No. "); Serial.println(i+1);
    float voltage = sensor[i].voltage();
    if( !isnan(voltage) ){
        Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
    } else {
        Serial.println("Error reading voltage");
    }
    float current = sensor[i].current();
    if( !isnan(current) ){
        Serial.print("Current: "); Serial.print(current); Serial.println("A");
    } else {
        Serial.println("Error reading current");
    }
    float power = sensor[i].power();
    if( !isnan(power) ){
        Serial.print("Power: "); Serial.print(power); Serial.println("W");
    } else {
        Serial.println("Error reading power");
    }
    float energy = sensor[i].energy();
    if( !isnan(energy) ){
        Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
    } else {
        Serial.println("Error reading energy");
    }
    float frequency = sensor[i].frequency();
    if( !isnan(frequency) ){
        Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
    } else {
        Serial.println("Error reading frequency");
    }
    float pf = sensor[i].pf();
    if( !isnan(pf) ){
        Serial.print("PF: "); Serial.println(pf);
    } else {
        Serial.println("Error reading power factor");
    }
    Serial.println();
    delay(2000);
  }
}

ขอบคุณที่มานำทาง : Thank you to : https://github.com/mandulaj/PZEM-004T-v30/issues/29

มาทบทวนภายหลังพบว่าน่าจะมีทางอื่นคือ hardware serial ก็ขึ้นอยู่กับว่า MCU ที่เลือกใช้นั้น จะมี ให้ใช้กี่ช่อง อย่างเช่น Mega ก็จะมี hardware serial 1-3 ให้เลือกใช้อีก