Pzem-004tv3 กับ DHT11

ขอคำแนะนำครับ pzem-004tv3 กับ DHT11 เหมือนอ่านค่าได้บางไม่ได้บางเป็นบางช่วงเวลา

ถ้า esp 8266 ต่อใช้งาน

  1. Sensor dht 11 ขา D9
  2. Sensor pzem-004tv3 ขา D3,D4
  3. Sensor pzem-17 ขา D0,D5,D6,D7
  4. Relay 2 ch ขา D1 , D2
    อุปกรณ์ทุกอย่างทำงานได้ปกติ ยกเว้นตัว pzem-004tv3 กับ DHT11 เหมือนอ่านค่าได้บางไม่ได้บางเป็นบางช่วงเวลา
  • สาเหตุเกินจากอะไรนะครับ
  • ลอง TEST แยกอุปกรณ์ให้ทำงานแต่ละตัว สามารถทำงานได้ปกติ
  • ตอนนี้ใช้แหล่งจ่ายไฟ แปลงจาก AC 200V เป็น DC 5V 2A






ส่วนของ Code ครับ
ESP_TEST_AC_DC_TEMP_MENU-26-9-64.rar (9.4 KB)

อาจจะลองใส่ while แทน if else ดู ในส่วนนี้ โดยเงื่อนไขคือ


while (PZEMVoltage == NAN &&
          PZEMCurrent == NAN &&
          PZEMPower == NAN &&
          PZEMEnergy == NAN){

}
 if (result == node.ku8MBSuccess)                                                                /* If there is a response */
    {
      uint32_t tempdouble = 0x00000000;                                                           /* Declare variable "tempdouble" as 32 bits with initial value is 0 */
      PZEMVoltage = node.getResponseBuffer(0x0000) / 100.0;                                       /* get the 16bit value for the voltage value, divide it by 100 (as per manual) */
      // 0x0000 to 0x0008 are the register address of the measurement value
      PZEMCurrent = node.getResponseBuffer(0x0001) / 100.0;                                       /* get the 16bit value for the current value, divide it by 100 (as per manual) */

      tempdouble =  (node.getResponseBuffer(0x0003) << 16) + node.getResponseBuffer(0x0002);      /* get the power value. Power value is consists of 2 parts (2 digits of 16 bits in front and 2 digits of 16 bits at the back) and combine them to an unsigned 32bit */
      PZEMPower = tempdouble / 10.0;                                                              /* Divide the value by 10 to get actual power value (as per manual) */

      tempdouble =  (node.getResponseBuffer(0x0005) << 16) + node.getResponseBuffer(0x0004);      /* get the energy value. Energy value is consists of 2 parts (2 digits of 16 bits in front and 2 digits of 16 bits at the back) and combine them to an unsigned 32bit */
      PZEMEnergy = tempdouble;
    }
    else // ถ้าติดต่อ PZEM-017 ไม่ได้ ให้ใส่ค่า NAN(Not a Number)
    {
      PZEMVoltage = NAN;
      PZEMCurrent = NAN;
      PZEMPower = NAN;
      PZEMEnergy = NAN;
    }

จุดประสงค์คือให้วนอ่านค่าจนกว่าจะได้ก่อนค่อย ส่งค่าออกมา แต่ไม่แน่ใจว่าจะกระทบกับการทำงานหลักมากน้อยแค่ไหน ต้องลองดู

1 Likes

ขอบคุณครับพี่
ให้ครับแก้ในส่วน code เป็นของ pzem-17 วัด DC ใช่มั้ยครับ เดียวผมลองดูอีกทีครับ

แก้ตรงที่อ่านไม่ได้ง่ะครับ ผมเคยใช้วิธีนี้อยู่ อีกวิธีคือใส่ delay ให้มันสัก 1000 หรือ 2000 เหมือนจังหวะที่มันส่งค่ามากับอ่านค่า มันไม่ตรงกัน ในระบบ I2C

ได้ครับผม เดียวผมลองดูอีกทีครับ

พี่ครับ มี code หรือแนวทาง การแจ้ง ไลน์บางมั้ยครับ
เช่น ถ้า sensor pzem004 อ่านค่าไม่ได้ เป็นเวลา 5 นาที จริงๆถึงให้แจ้งส่งไลน์ ac:fail , แต่ถ้าอ่านได้ก็ ac:online
คือผมอยากกำหนดเวลาส่งไลน์ครับ