bangnaga

Node-Red ESP8266 MQTT Senosr PZEM

Aug 4th, 2021
823
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4. #include <Wire.h>
  5.  
  6. #include <PZEM004Tv30.h>
  7. #include <SoftwareSerial.h>
  8.  
  9. const char* ssid = "Almer";
  10. const char* password = "tidakada";
  11. const char* mqtt_server = "broker.hivemq.com";
  12. const char* mqtt_user = "";
  13. const char* mqtt_pass= "";
  14.  
  15. #define tegangan "0"
  16. #define arus "0"
  17. #define daya "0"
  18. #define energi "0"
  19. #define keterangan ""
  20.  
  21.  
  22. WiFiClient espClient;
  23. PubSubClient client(espClient);
  24.  
  25. /* Use software serial for the PZEM
  26.  * Pin 12 Rx (Connects to the Tx pin on the PZEM)
  27.  * Pin 13 Tx (Connects to the Rx pin on the PZEM)
  28. */
  29. //#if !defined(PZEM_RX_PIN) && !defined(PZEM_TX_PIN)
  30. //#define PZEM_RX_PIN 12
  31. //#define PZEM_TX_PIN 13
  32. //#endif
  33.  
  34.  
  35. //SoftwareSerial pzemSWSerial(PZEM_RX_PIN, PZEM_TX_PIN);
  36. PZEM004Tv30 pzem(13,12);
  37.  
  38.  
  39. void setup_wifi() {
  40.   // Connecting to a WiFi network
  41.   WiFi.begin(ssid, password);
  42.   while (WiFi.status() != WL_CONNECTED) {
  43.     delay(500);
  44.     Serial.print(".");
  45.   }
  46.   Serial.println("WiFi Terhubung");
  47.   Serial.println("IP address: ");
  48.   Serial.println(WiFi.localIP());
  49. }
  50.  
  51.  
  52. void reconnect() {
  53.   // Ulangi koneksi sampai terhubung
  54.   Serial.println("Menghubungkan Ulang...");
  55.   while (!client.connected()) {
  56.     Serial.print("Melakukan koneksi ke MQTT Broker...");
  57.     // Attempt to connect
  58.     if (client.connect("Arduino_ikhsan", mqtt_user, mqtt_pass)) {
  59.       Serial.println("connected");
  60.     } else {
  61.       Serial.print("failed, rc=");
  62.       Serial.print(client.state());
  63.       Serial.println(" Reconect dalam 5 detik");
  64.       delay(5000);
  65.     }
  66.   }
  67. }
  68.  
  69.  
  70. void setup() {
  71.     /* Debugging serial */
  72.     Serial.begin(115200);
  73.  
  74.     setup_wifi();
  75.     client.setServer(mqtt_server, 1883);
  76. }
  77.  
  78. void loop() {
  79.          
  80.  //   Serial.print("Custom Address:");
  81.  //   Serial.println(pzem.readAddress(), HEX);
  82.  
  83.     // Read the data from the sensor
  84.     float voltage = pzem.voltage();
  85.     float current = pzem.current();
  86.     float power = pzem.power();
  87.     float energy = pzem.energy();
  88.     float frequency = pzem.frequency();
  89.     float pf = pzem.pf();
  90.  
  91.     // Check if the data is valid
  92.     if(isnan(voltage)){
  93.         Serial.println("Error reading voltage");
  94.     } else if (isnan(current)) {
  95.         Serial.println("Error reading current");
  96.     } else if (isnan(power)) {
  97.         Serial.println("Error reading power");
  98.     } else if (isnan(energy)) {
  99.         Serial.println("Error reading energy");
  100.     } else if (isnan(frequency)) {
  101.         Serial.println("Error reading frequency");
  102.     } else if (isnan(pf)) {
  103.         Serial.println("Error reading power factor");
  104.     } else {
  105.  
  106.         // Print the values to the Serial console
  107.         Serial.print("Voltage: ");      Serial.print(voltage);      Serial.println("V");
  108.         Serial.print("Current: ");      Serial.print(current);      Serial.println("A");
  109.         Serial.print("Power: ");        Serial.print(power);        Serial.println("W");
  110.         Serial.print("Energy: ");       Serial.print(energy,3);     Serial.println("kWh");
  111.         Serial.print("Frequency: ");    Serial.print(frequency, 1); Serial.println("Hz");
  112.         Serial.print("PF: ");           Serial.println(pf);
  113.     }
  114.  
  115.     Serial.println();
  116.     if (!client.connected()) {
  117.     reconnect();
  118.     }
  119.     client.loop();
  120.     client.publish("tegangan", String(voltage).c_str(), true);
  121.     client.publish("arus", String(current).c_str(), true);
  122.     client.publish("daya", String(power).c_str(), true);
  123.     client.publish("energi", String(energy,3).c_str(), true);
  124.     client.publish("keterangan", String(keterangan).c_str(), true);
  125.    
  126.   client.publish("keterangan", "Light Off");
  127.  
  128.     delay(2000);
  129. }
RAW Paste Data