Advertisement
Masoko

working esp8622 mtqq example

Feb 23rd, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. #include "DHT.h"
  2. #include <PubSubClient.h>
  3. #include <ESP8266WiFi.h>
  4.  
  5. const char* ssid = "masoko";
  6. const char* password = "123123123";
  7.  
  8. char* topic = "masoko";
  9. char* server = "broker.hivemq.com";
  10. char* hellotopic = "hello_topic";
  11.  
  12. #define DHTPIN 1    // what pin we're connected to
  13. #define DHTTYPE DHT22   // DHT 22  (AM2302)
  14. #define REPORT_INTERVAL 130 // in sec
  15.  
  16. void callback(char* topic, byte* payload, unsigned int length) {
  17.   // handle message arrived
  18. }
  19.  
  20. String clientName;
  21. DHT dht(DHTPIN, DHTTYPE, 15);
  22. WiFiClient wifiClient;
  23. PubSubClient client(server, 1883, callback, wifiClient);
  24.  
  25. float oldH ;
  26. float oldT ;
  27.  
  28. void setup() {
  29.   Serial.begin(38400);
  30.   Serial.println("DHTxx test!");
  31.   delay(20);
  32.  
  33.   Serial.println();
  34.   Serial.println();
  35.   Serial.print("Connecting to ");
  36.   Serial.println(ssid);
  37.  
  38.   WiFi.mode(WIFI_STA);
  39.   WiFi.begin(ssid, password);
  40.  
  41.   while (WiFi.status() != WL_CONNECTED) {
  42.     delay(500);
  43.     Serial.print(".");
  44.   }
  45.   Serial.println("");
  46.   Serial.println("WiFi connected");
  47.   Serial.println("IP address: ");
  48.   Serial.println(WiFi.localIP());
  49.  
  50.   clientName += "esp8266-";
  51.   uint8_t mac[6];
  52.   WiFi.macAddress(mac);
  53.   clientName += macToStr(mac);
  54.   clientName += "-";
  55.   clientName += String(micros() & 0xff, 16);
  56.  
  57.   Serial.print("Connecting to ");
  58.   Serial.print(server);
  59.   Serial.print(" as ");
  60.   Serial.println(clientName);
  61.  
  62.   if (client.connect((char*) clientName.c_str())) {
  63.     Serial.println("Connected to MQTT broker");
  64.     Serial.print("Topic is: ");
  65.     Serial.println(topic);
  66.    
  67.     if (client.publish(hellotopic, "hello from ESP8266")) {
  68.       Serial.println("Publish ok");
  69.     }
  70.     else {
  71.       Serial.println("Publish failed");
  72.     }
  73.   }
  74.   else {
  75.     Serial.println("MQTT connect failed");
  76.     Serial.println("Will reset and try again...");
  77.     abort();
  78.   }
  79.  
  80.   dht.begin();
  81.   oldH = -1;
  82.   oldT = -1;
  83. }
  84.  
  85. void loop() {
  86.  
  87.   float h = dht.readHumidity();
  88.   float t = dht.readTemperature();
  89.   float f = dht.readTemperature(true);
  90.  
  91.   if (isnan(h) || isnan(t) || isnan(f)) {
  92.     Serial.println("Failed to read from DHT sensor!");
  93.     return;
  94.   }
  95.  
  96.   float hi = dht.computeHeatIndex(f, h);
  97.  
  98.   Serial.print("Humidity: ");
  99.   Serial.print(h);
  100.   Serial.print(" %\t");
  101.   Serial.print("Temperature: ");
  102.   Serial.print(t);
  103.   Serial.print(" *C ");
  104.   Serial.print(f);
  105.   Serial.print(" *F\t");
  106.   Serial.print("Heat index: ");
  107.   Serial.print(hi);
  108.   Serial.println(" *F");
  109.  
  110.   String payload = "{\"Humidity\":";
  111.   payload += h;
  112.   payload += ",\"Temperature\":";
  113.   payload += t;
  114.   payload += "}";
  115.  
  116.   if (t != oldT || h != oldH )
  117.   {
  118.     sendTemperature(payload);
  119.     oldT = t;
  120.     oldH = h;
  121.   }
  122.  
  123.   int cnt = REPORT_INTERVAL;
  124.  
  125.   while (cnt--)
  126.     delay(1000);
  127. }
  128.  
  129.  
  130. void sendTemperature(String payload) {
  131.   if (!client.connected()) {
  132.     if (client.connect((char*) clientName.c_str())) {
  133.       Serial.println("Connected to MQTT broker again");
  134.       Serial.print("Topic is: ");
  135.       Serial.println(topic);
  136.     }
  137.     else {
  138.       Serial.println("MQTT connect failed");
  139.       Serial.println("Will reset and try again...");
  140.       abort();
  141.     }
  142.   }
  143.  
  144.   if (client.connected()) {
  145.     Serial.print("Sending payload: ");
  146.     Serial.println(payload);
  147.  
  148.     if (client.publish(topic, (char*) payload.c_str())) {
  149.       Serial.println("Publish ok");
  150.     }
  151.     else {
  152.       Serial.println("Publish failed");
  153.     }
  154.   }
  155.  
  156. }
  157.  
  158.  
  159. String macToStr(const uint8_t* mac)
  160. {
  161.   String result;
  162.   for (int i = 0; i < 6; ++i) {
  163.     result += String(mac[i], 16);
  164.     if (i < 5)
  165.       result += ':';
  166.   }
  167.   return result;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement