mikroavr

kwh_blynk_pzem

Jun 3rd, 2022 (edited)
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define BLYNK_PRINT Serial
  2. #define BLYNK_TEMPLATE_ID "TMPL-YozzxnU"
  3. #define BLYNK_DEVICE_NAME "Quickstart Template"
  4. #define BLYNK_AUTH_TOKEN "kcbRdG124Fv61Jg-r5fx8WfYXn-rR4_u"
  5.  
  6. #include <WiFi.h>
  7. #include <WiFiClient.h>
  8. #include <BlynkSimpleEsp32.h>
  9.  
  10.  
  11. // Your WiFi credentials.
  12. // Set password to "" for open networks.
  13. char ssid[] = "MIKROAVR.COM";
  14. char pass[] = "Admin@357";
  15.  
  16.  
  17.  
  18. #include <ModbusMaster.h>
  19. ModbusMaster node;
  20. static uint8_t pzemSlaveAddr = 1;
  21.  
  22.  
  23. #define MAX485_DE      2
  24. #define MAX485_RE_NEG  2
  25.  
  26. float voltage,  current, power, energy, freq, pf;
  27.  
  28. #define RXD_MASTER 17
  29. #define TXD_MASTER 16
  30. #define pzemSerial Serial1
  31.  
  32. unsigned long cur_time, old_time;
  33.  
  34. const byte pin_relay = 32;
  35.  
  36. void preTransmission()
  37. {
  38.   digitalWrite(MAX485_RE_NEG, 1);
  39.   digitalWrite(MAX485_DE, 1);
  40. }
  41.  
  42. void postTransmission()
  43. {
  44.   digitalWrite(MAX485_RE_NEG, 0);
  45.   digitalWrite(MAX485_DE, 0);
  46. }
  47.  
  48. BLYNK_WRITE(V0)
  49. {  
  50.   int value = param.asInt(); // Get value as integer
  51.   Serial.print("rel: ");
  52.   Serial.println(value);
  53.   digitalWrite(pin_relay,value);
  54. }
  55.  
  56. BLYNK_WRITE(V10)
  57. {  
  58.   int value = param.asInt(); // Get value as integer
  59.   Serial.print("v10: ");
  60.   Serial.println(value);
  61.   if(value == true){
  62.     Serial.println("reset energy");
  63.     //resetEnergy(pzemSlaveAddr);
  64.   }
  65. }
  66.  
  67. void setup() {
  68.   pinMode(pin_relay, OUTPUT);
  69.   pinMode(MAX485_RE_NEG, OUTPUT);
  70.   pinMode(MAX485_DE, OUTPUT);
  71.   // Init in receive mode
  72.   digitalWrite(MAX485_RE_NEG, 0);
  73.   digitalWrite(MAX485_DE, 0);
  74.  
  75.  
  76.   pzemSerial.begin(9600, SERIAL_8N1, RXD_MASTER, TXD_MASTER);
  77.   Serial.begin(115200);
  78.  
  79.   node.begin(pzemSlaveAddr, pzemSerial);
  80.   node.preTransmission(preTransmission);
  81.   node.postTransmission(postTransmission);
  82.   Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  83.  
  84. }
  85. void loop() {
  86.   Blynk.run();
  87.   cur_time = millis();
  88.   if (cur_time - old_time >= 3000) {
  89.     //baca_pzem();
  90.     //push_blynk();
  91.     old_time = cur_time;
  92.   }
  93.  
  94. }
  95.  
  96. void push_blynk(){
  97.   Blynk.virtualWrite(V4, voltage);
  98.   Blynk.virtualWrite(V5, current);
  99.   Blynk.virtualWrite(V6, freq);
  100.   Blynk.virtualWrite(V7, pf);
  101.   Blynk.virtualWrite(V8, power);
  102.   Blynk.virtualWrite(V9, energy);
  103. }
  104.  
  105. void baca_pzem() {
  106.   uint8_t result;
  107.   result = node.readInputRegisters(0, 9); //read the 9 registers of the PZEM-014 / 016
  108.   if (result == node.ku8MBSuccess)
  109.   {
  110.     Serial.println("sukses modbus");
  111.     voltage = node.getResponseBuffer(0) / 10.0;
  112.  
  113.     uint32_t tempdouble = 0x00000000;
  114.  
  115.     tempdouble = node.getResponseBuffer(1);       //LowByte
  116.     tempdouble |= node.getResponseBuffer(2) << 8;  //highByte
  117.     current = tempdouble / 1000.0;
  118.  
  119.     tempdouble |= node.getResponseBuffer(3);       //LowByte
  120.     tempdouble |= node.getResponseBuffer(4) << 8;  //highByte
  121.     power = tempdouble / 10.0;
  122.  
  123.     tempdouble = node.getResponseBuffer(5);       //LowByte
  124.     tempdouble |= node.getResponseBuffer(6) << 8;  //highByte
  125.     energy = tempdouble;
  126.  
  127.     tempdouble = node.getResponseBuffer(7);
  128.     freq = tempdouble / 10.0;
  129.  
  130.     tempdouble = node.getResponseBuffer(8);
  131.     pf = tempdouble / 10.0;
  132.     print_data();
  133.  
  134.   } else {
  135.     Serial.println("Failed to read modbus");
  136.   }
  137. }
  138.  
  139. void resetEnergy(uint8_t slaveAddr) {
  140.   //The command to reset the slave's energy is (total 4 bytes):
  141.   //Slave address + 0x42 + CRC check high byte + CRC check low byte.
  142.   uint16_t u16CRC = 0xFFFF;
  143.  
  144.   static uint8_t resetCommand = 0x42;
  145.   u16CRC = crc16_update(u16CRC, slaveAddr);
  146.   u16CRC = crc16_update(u16CRC, resetCommand);
  147.   Serial.println("Resetting Energy");
  148.  
  149.   delay(10);//ditambah
  150.  
  151.   pzemSerial.write(slaveAddr);
  152.   pzemSerial.write(resetCommand);
  153.   pzemSerial.write(lowByte(u16CRC));
  154.   pzemSerial.write(highByte(u16CRC));
  155.  
  156.   delay(10);//ditambah
  157.   Serial.print("respon: ");
  158.   while (pzemSerial.available() > 0) {
  159.     Serial.print(pzemSerial.read(), HEX);
  160.     Serial.print(",");
  161.   }
  162.   Serial.println();
  163.   delay(1000);
  164. }
  165.  
  166. void print_data() {
  167.   Serial.print(voltage);
  168.   Serial.print("V   ");
  169.  
  170.   Serial.print(current);
  171.   Serial.print("A   ");
  172.  
  173.   Serial.print(freq);
  174.   Serial.print("Hz   ");
  175.  
  176.   Serial.print(pf);
  177.   Serial.print("pf   ");
  178.   Serial.print("   ");
  179.  
  180.   Serial.print(power);
  181.   Serial.print("W  ");
  182.  
  183.   Serial.print(energy);
  184.   Serial.print("Wh  ");
  185.   Serial.println();
  186. }
Add Comment
Please, Sign In to add comment