Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. //MQTT
  2. #include <PubSubClient.h>
  3. //ESP
  4. #include <ESP8266WiFi.h>
  5.  
  6. #define MQTT_AUTH false
  7. #define MQTT_USERNAME ""
  8. #define MQTT_PASSWORD ""
  9.  
  10. #include <SoftwareSerial.h>
  11. //https://github.com/olehs/PZEM004T
  12. #include <PZEM004T.h>
  13.  
  14. PZEM004T pzem(D2, D1);
  15. IPAddress ip(192, 168, 1, 1);
  16.  
  17. //Constantes
  18. const String HOSTNAME  = "pzem";
  19.  
  20. const char* ssid = "TopSecretNetwork";
  21.  
  22. const char* password = "secretpsw";
  23.  
  24. const char* host = "emoncms.org";
  25.  
  26. //MQTT BROKERS GRATUITOS PARA TESTES https://github.com/mqtt/mqtt.github.io/wiki/public_brokers
  27. const char* MQTT_SERVER = "192.168.1.150";
  28.  
  29. WiFiClient wclient;
  30. PubSubClient client(MQTT_SERVER, 1883, wclient);
  31.  
  32.  
  33. void setup() {
  34.   Serial.begin(115200);
  35.   WiFi.begin(ssid, password);
  36.   WiFi.mode(WIFI_STA);
  37.   Serial.println(".");
  38.   // Aguarda até estar ligado ao Wi-Fi
  39.   while (WiFi.status() != WL_CONNECTED) {
  40.     delay(500);
  41.     Serial.print(".");
  42.   }
  43.   Serial.println("");
  44.   Serial.print("Ligado a ");
  45.   Serial.println(ssid);
  46.   Serial.print("Endereço IP: ");
  47.   Serial.println(WiFi.localIP());
  48.   pzem.setAddress(ip);
  49. }
  50.  
  51. bool checkMqttConnection() {
  52.   if (!client.connected()) {
  53.     if (MQTT_AUTH ? client.connect(HOSTNAME.c_str(), MQTT_USERNAME, MQTT_PASSWORD) : client.connect(HOSTNAME.c_str())) {
  54.       //SUBSCRIÇÃO DE TOPICOS
  55.       Serial.println("CONNECTED");
  56.     }
  57.   }
  58.   return client.connected();
  59. }
  60.  
  61.  
  62.  
  63. void loop() {
  64.   if (WiFi.status() == WL_CONNECTED) {
  65.     if (checkMqttConnection()) {
  66.       client.loop();
  67.  
  68.      
  69.       float v = pzem.voltage(ip);
  70.       String voltagem;
  71.       voltagem = String(v);
  72.       if (v < 0.0) v = 0.0;
  73.       Serial.print(v); Serial.print("V; ");
  74.       client.publish("/Casa/PZEM/V", voltagem.c_str());
  75.  
  76.       float i = pzem.current(ip);
  77.       String amperagem;
  78.       amperagem = String(i);
  79.       if (i >= 0.0) {
  80.         Serial.print(i);
  81.         Serial.print("A; ");
  82.       }
  83.       client.publish("/Casa/PZEM/A", amperagem.c_str());
  84.  
  85.       float p = pzem.power(ip);
  86.       String potencia;
  87.       potencia = String(p);
  88.       if (p >= 0.0) {
  89.         Serial.print(p);
  90.         Serial.print("W; ");
  91.       }
  92.       client.publish("/Casa/PZEM/P", potencia.c_str());
  93.  
  94.  
  95.       float e = pzem.energy(ip);
  96.       String energia;
  97.       energia = String(e);
  98.       if (e >= 0.0) {
  99.         Serial.print(e);
  100.         Serial.print("Wh; ");
  101.       }
  102.       client.publish("/Casa/PZEM/E", energia.c_str());
  103.  
  104.  
  105.  
  106.       WiFiClient client;
  107.       const int httpPort = 80;
  108.       if (!client.connect(host, httpPort)) {
  109.         Serial.println("connection failed");
  110.         return;
  111.       }
  112.  
  113.  
  114.       // We now create a URI for the request
  115.       String url = "/api/post?node=pzem&apikey=71128d82c7c98335d2010468336f0f73&json={voltagem:" + String(v) + ",amperagem:" + String(i) + ",potencia:" + String(p) + ",energia:" + String(e)+ "}";
  116.       //String url = "/api/post?node=pzem&apikey=71128d82c7c98335d2010468336f0f73&json={voltagem:" + String(voltagem.c_str()) + ",amperagem:" + String(amperagem.c_str()) + ",potencia:" + String(potencia.c_str()) + ",energia:" + String(energia.c_str())+ "}";
  117.       //String url = "/input/post.json?csv=" + String(voltagem) + "," + String(amperagem) + "&apikey=71128d82c7c98335d2010468336f0f73"; //Enter api key here
  118.  
  119.       Serial.print("Requesting URL: ");
  120.       Serial.println(url);
  121.  
  122.       // This will send the request to the server
  123.       client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  124.                    "Host: " + host + "\r\n" +
  125.                    "Connection: close\r\n\r\n");
  126.       unsigned long timeout = millis();
  127.       while (client.available() == 0) {
  128.         if (millis() - timeout > 5000) {
  129.           Serial.println(">>> Client Timeout !");
  130.           client.stop();
  131.           return;
  132.         }
  133.       }
  134.  
  135.     }
  136.   }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement