V_Tailor

Cod Esp

May 31st, 2020
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4. #include <ArduinoJson.h>
  5. SoftwareSerial s(D6,D5);
  6. const char* ssid = "cro";
  7. const char* password = "cheguevara";
  8. const char* mqtt_server = "test.mosquitto.org";
  9. WiFiClient espClient;
  10. PubSubClient client(espClient);
  11. int pornit=0;
  12. int Md =0;
  13. int lim1 =0;
  14. int lim2 =0;
  15. int lim3 =0;
  16. int lim4 =0;
  17. int lim5 =0;
  18. int lim6 =0;
  19. int pir1 =0;
  20. int pir2 =0;
  21. int apa =0;
  22. int temperatura =0;
  23.        
  24. void setup() {
  25.   // Initialize Serial port
  26.   Serial.begin(9600);
  27.   setup_wifi();
  28.   client.setServer(mqtt_server, 1883);
  29.   s.begin(9600);
  30.   while (!Serial) continue;
  31.  }
  32.  
  33. void loop() {
  34.   const size_t capacity = JSON_OBJECT_SIZE(16) + 16;
  35.   DynamicJsonDocument doc(capacity);
  36.   DeserializationError error = deserializeJson(doc, s);
  37.   if (error) {
  38.   Serial.print(F("deserializeJson() failed: "));
  39.   Serial.println(error.c_str());
  40.   return;
  41.   }
  42.   Serial.println("JSON received and parsed");
  43.   serializeJsonPretty(doc,Serial);
  44.   pornit=doc["pornit"];
  45.   Md=doc["mod"];
  46.   lim1=doc["lim1"];
  47.   lim2=doc["lim2"];
  48.   lim3=doc["lim3"];
  49.   lim4=doc["lim4"];
  50.   lim5=doc["lim5"];
  51.   lim6=doc["lim6"];
  52.   pir1=doc["pir1"];
  53.   pir2=doc["pir2"];
  54.   apa=doc["apa"];
  55.   temperatura=doc["temperatura"];
  56.   if (!client.connected()) {
  57.     reconnect();
  58.   }
  59.   if(!client.loop())
  60.     client.connect("ESP8266Client");
  61.     static char z1[10];
  62.     static char z2[10];
  63.     static char z3[10];
  64.     static char z4[10];
  65.     static char z5[10];
  66.     static char z6[10];
  67.     static char z7[10];
  68.     static char z8[10];
  69.     static char z9[10];
  70.     static char z10[10];
  71.     static char z11[10];
  72.     static char z12[10];
  73.     dtostrf(pornit, 6, 4, z1);
  74.     dtostrf(Md, 6, 4, z2);
  75.     dtostrf(lim1, 6, 4, z3);
  76.     dtostrf(lim2, 6, 4, z4);
  77.     dtostrf(lim3, 6, 4, z5);
  78.     dtostrf(lim4, 6, 5, z6);
  79.     dtostrf(lim5, 6, 4, z7);
  80.     dtostrf(lim6, 6, 4, z8);
  81.     dtostrf(pir1, 6, 4, z9);
  82.     dtostrf(pir2, 6, 5, z10);
  83.     dtostrf(apa, 6, 4, z11);
  84.     dtostrf(temperatura, 6, 4, z12);
  85.     client.publish("pornit", z1);
  86.     client.publish("Md", z2);
  87.     client.publish("lim1", z3);
  88.     client.publish("lim2", z4);
  89.     client.publish("lim3", z5);
  90.     client.publish("lim4", z6);
  91.     client.publish("lim5", z7);
  92.     client.publish("lim6", z8);
  93.     client.publish("pir1", z9);
  94.     client.publish("pir2", z10);
  95.     client.publish("apa", z11);
  96.     client.publish("temperatura", z12);
  97.  
  98. }
  99.  
  100.  
  101.  
  102. void setup_wifi() {
  103.   delay(10);
  104.   Serial.println();
  105.   Serial.print("Connecting to ");
  106.   Serial.println(ssid);
  107.   WiFi.begin(ssid, password);
  108.   while (WiFi.status() != WL_CONNECTED) {
  109.     delay(500);
  110.     Serial.print(".");
  111.   }
  112.   Serial.println("");
  113.   Serial.print("WiFi connected - ESP IP address: ");
  114.   Serial.println(WiFi.localIP());
  115. }
  116. void reconnect() {
  117.   while (!client.connected()) {
  118.     Serial.print("Attempting MQTT connection...");
  119.     if (client.connect("ESP8266Client")) {
  120.       Serial.println("connected");  
  121.       //client.subscribe("room/lamp");
  122.     } else {
  123.       Serial.print("failed, rc=");
  124.       Serial.print(client.state());
  125.       Serial.println(" try again in 5 seconds");
  126.       delay(5000);
  127.     }
  128.   }
  129. }
Add Comment
Please, Sign In to add comment