Advertisement
Thorik_tk

Example code PZEM-017

Aug 17th, 2023 (edited)
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | Source Code | 0 0
  1. /* Example code Read data modul PZEM-017 */
  2.  
  3. #include <SoftwareSerial.h>
  4. #include <ModbusMaster.h>
  5.  
  6. SoftwareSerial PZEMSerial;
  7.  
  8. #define MAX485_RO 13  //D7
  9. #define MAX485_RE 12  //D6
  10. #define MAX485_DE 5   //D1
  11. #define MAX485_DI 4   //D2
  12.  
  13. static uint8_t pzemSlaveAddr = 0x01;
  14. static uint16_t NewshuntAddr = 0x0001;
  15.  
  16. ModbusMaster node;
  17.  
  18. float PZEMVoltage, PZEMCurrent, PZEMPower, PZEMEnergy;
  19.  
  20. unsigned long startMillisPZEM;
  21. unsigned long currentMillisPZEM;
  22. const unsigned long periodPZEM = 1000;
  23.  
  24. unsigned long startMillisReadData;
  25. unsigned long startMillis1;
  26.  
  27. void setup() {
  28.   startMillis1 = millis();
  29.   Serial.begin(115200);
  30.   PZEMSerial.begin(9600, SWSERIAL_8N2, MAX485_RO, MAX485_DI);
  31.  
  32.   startMillisPZEM = millis();
  33.   pinMode(MAX485_RE, OUTPUT);
  34.   pinMode(MAX485_DE, OUTPUT);
  35.   digitalWrite(MAX485_RE, 0);
  36.   digitalWrite(MAX485_DE, 0);
  37.  
  38.   node.preTransmission(preTransmission);
  39.   node.postTransmission(postTransmission);
  40.   node.begin(pzemSlaveAddr, PZEMSerial);
  41.   delay(1000);
  42.  
  43.   while (millis() - startMillis1 < 5000) {
  44.     delay(500);
  45.     Serial.print(".");
  46.   }
  47.  
  48.   setShunt(pzemSlaveAddr);
  49.   changeAddress(0xF8, pzemSlaveAddr);
  50.   // resetEnergy();
  51. }
  52.  
  53. void loop() {
  54.   currentMillisPZEM = millis();
  55.   if (currentMillisPZEM - startMillisPZEM >= periodPZEM) {
  56.     uint8_t result;
  57.     result = node.readInputRegisters(0x0000, 6);
  58.     if (result == node.ku8MBSuccess) {
  59.       uint32_t tempdouble = 0x00000000;
  60.       PZEMVoltage = node.getResponseBuffer(0x0000) / 100.0;
  61.       PZEMCurrent = node.getResponseBuffer(0x0001) / 100.0;
  62.  
  63.       tempdouble = (node.getResponseBuffer(0x0003) << 16) + node.getResponseBuffer(0x0002);
  64.       PZEMPower = tempdouble / 10.0;
  65.  
  66.       tempdouble = (node.getResponseBuffer(0x0005) << 16) + node.getResponseBuffer(0x0004);
  67.       PZEMEnergy = tempdouble;
  68.     } else {
  69.       //PZEMVoltage = NAN;
  70.       //PZEMCurrent = NAN;
  71.       //PZEMPower = NAN;
  72.       //PZEMEnergy = NAN;
  73.     }
  74.  
  75.     Serial.print("Vdc : ");
  76.     Serial.print(PZEMVoltage);
  77.     Serial.println(" V ");
  78.     Serial.print("Idc : ");
  79.     Serial.print(PZEMCurrent);
  80.     Serial.println(" A ");
  81.     Serial.print("Power : ");
  82.     Serial.print(PZEMPower);
  83.     Serial.println(" W ");
  84.     Serial.print("Energy : ");
  85.     Serial.print(PZEMEnergy);
  86.     Serial.println(" Wh ");
  87.  
  88.     startMillisPZEM = currentMillisPZEM;
  89.   }
  90. }
  91.  
  92. void preTransmission() {
  93.   if (millis() - startMillis1 > 5000) {
  94.     digitalWrite(MAX485_RE, 1);
  95.     digitalWrite(MAX485_DE, 1);
  96.     delay(1);
  97.   }
  98. }
  99.  
  100. void postTransmission() {
  101.   if (millis() - startMillis1 > 5000) {
  102.     delay(3);
  103.     digitalWrite(MAX485_RE, 0);
  104.     digitalWrite(MAX485_DE, 0);
  105.   }
  106. }
  107.  
  108. void setShunt(uint8_t slaveAddr) {
  109.  
  110.   static uint8_t SlaveParameter = 0x06;
  111.   static uint16_t registerAddress = 0x0003;
  112.  
  113.   uint16_t u16CRC = 0xFFFF;
  114.   u16CRC = crc16_update(u16CRC, slaveAddr);
  115.   u16CRC = crc16_update(u16CRC, SlaveParameter);
  116.   u16CRC = crc16_update(u16CRC, highByte(registerAddress));
  117.   u16CRC = crc16_update(u16CRC, lowByte(registerAddress));
  118.   u16CRC = crc16_update(u16CRC, highByte(NewshuntAddr));
  119.   u16CRC = crc16_update(u16CRC, lowByte(NewshuntAddr));
  120.  
  121.   preTransmission();
  122.  
  123.   PZEMSerial.write(slaveAddr);
  124.   PZEMSerial.write(SlaveParameter);
  125.   PZEMSerial.write(highByte(registerAddress));
  126.   PZEMSerial.write(lowByte(registerAddress));
  127.   PZEMSerial.write(highByte(NewshuntAddr));
  128.   PZEMSerial.write(lowByte(NewshuntAddr));
  129.   PZEMSerial.write(lowByte(u16CRC));
  130.   PZEMSerial.write(highByte(u16CRC));
  131.   delay(10);
  132.   postTransmission();
  133.   delay(100);
  134. }
  135.  
  136. void resetEnergy() {
  137.   uint16_t u16CRC = 0xFFFF;
  138.   static uint8_t resetCommand = 0x42;
  139.   uint8_t slaveAddr = pzemSlaveAddr;
  140.   u16CRC = crc16_update(u16CRC, slaveAddr);
  141.   u16CRC = crc16_update(u16CRC, resetCommand);
  142.   preTransmission();
  143.   PZEMSerial.write(slaveAddr);
  144.   PZEMSerial.write(resetCommand);
  145.   PZEMSerial.write(lowByte(u16CRC));
  146.   PZEMSerial.write(highByte(u16CRC));
  147.   delay(10);
  148.   postTransmission();
  149.   delay(100);
  150. }
  151.  
  152. void changeAddress(uint8_t OldslaveAddr, uint8_t NewslaveAddr) {
  153.  
  154.   static uint8_t SlaveParameter = 0x06;
  155.   static uint16_t registerAddress = 0x0002;
  156.   uint16_t u16CRC = 0xFFFF;
  157.   u16CRC = crc16_update(u16CRC, OldslaveAddr);
  158.   u16CRC = crc16_update(u16CRC, SlaveParameter);
  159.   u16CRC = crc16_update(u16CRC, highByte(registerAddress));
  160.   u16CRC = crc16_update(u16CRC, lowByte(registerAddress));
  161.   u16CRC = crc16_update(u16CRC, highByte(NewslaveAddr));
  162.   u16CRC = crc16_update(u16CRC, lowByte(NewslaveAddr));
  163.   preTransmission();
  164.  
  165.   PZEMSerial.write(OldslaveAddr);
  166.   PZEMSerial.write(SlaveParameter);
  167.   PZEMSerial.write(highByte(registerAddress));
  168.   PZEMSerial.write(lowByte(registerAddress));
  169.   PZEMSerial.write(highByte(NewslaveAddr));
  170.   PZEMSerial.write(lowByte(NewslaveAddr));
  171.   PZEMSerial.write(lowByte(u16CRC));
  172.   PZEMSerial.write(highByte(u16CRC));
  173.   delay(10);
  174.   postTransmission();
  175.   delay(100);
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement