daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Nov 21st, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/
  2. // Get ESP8266 going with Arduino IDE
  3. // - https://github.com/esp8266/Arduino#installing-with-boards-manager
  4. // Required libraries (sketch -> include library -> manage libraries)
  5. // - PubSubClient by Nick ‘O Leary
  6. // - DHT sensor library by Adafruit
  7.  
  8. #include <ESP8266WiFi.h>
  9. #include <PubSubClient.h>
  10. #include <DHT.h>
  11.  
  12. #define wifi_ssid "IA-Group3"
  13. #define wifi_password "industrie4.0"
  14.  
  15. #define mqtt_server "192.168.254.1"
  16. // #define mqtt_user "your_username"
  17. // #define mqtt_password "your_password"
  18.  
  19. #define humidity_topic "DHT22_1"
  20. #define temperature_topic "DHT22_1"
  21.  
  22. #define DHTTYPE DHT22
  23. #define DHTPIN  3
  24.  
  25. WiFiClient espClient;
  26. PubSubClient client(espClient);
  27. DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
  28. // NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
  29. // might need to increase the threshold for cycle counts considered a 1 or 0.
  30. // You can do this by passing a 3rd parameter for this threshold.  It's a bit
  31. // of fiddling to find the right value, but in general the faster the CPU the
  32. // higher the value.  The default for a 16mhz AVR is a value of 6.  For an
  33. // Arduino Due that runs at 84mhz a value of 30 works.
  34. // Example to initialize DHT sensor for Arduino Due:
  35. //DHT dht(DHTPIN, DHTTYPE, 30); https://github.com/esp8266/Arduino/issues/118
  36.  
  37. void setup() {
  38.   Serial.begin(115200);
  39.   dht.begin();
  40.   setup_wifi();
  41.   client.setServer(mqtt_server, 1883); // TCP/IP port 1883 is reserved with IANA for use with MQTT.
  42. }
  43.  
  44. void setup_wifi() {
  45.   delay(10);
  46.   // Start by connecting to a WiFi network
  47.   Serial.println();
  48.   Serial.print("Connecting to ");
  49.   Serial.println(wifi_ssid);
  50.  
  51.   WiFi.begin(wifi_ssid, wifi_password);
  52.  
  53.   while (WiFi.status() != WL_CONNECTED) {
  54.     delay(500);
  55.     Serial.print(".");
  56.   }
  57.  
  58.   Serial.println("");
  59.   Serial.println("WiFi connected");
  60.   Serial.println("IP address: ");
  61.   Serial.println(WiFi.localIP());
  62. }
  63.  
  64. void reconnect() {
  65.   // Loop until we're reconnected
  66.   while (!client.connected()) {
  67.     Serial.print("Attempting MQTT connection...");
  68.     // Attempt to connect
  69.     // If you do not want to use a username and password, change next line to
  70.     // if (client.connect("ESP8266Client")) {
  71.     // if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
  72.       if (client.connect("ESP8266Client")) {
  73.       Serial.println("connected");
  74.     } else {
  75.       Serial.print("failed, rc=");
  76.       Serial.print(client.state());
  77.       Serial.println(" try again in 5 seconds");
  78.       // Wait 5 seconds before retrying
  79.       delay(5000);
  80.     }
  81.   }
  82. }
  83.  
  84. bool checkBound(float newValue, float prevValue, float maxDiff) {
  85.   return !isnan(newValue) &&
  86.          (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
  87. }
  88.  
  89. long lastMsg = 0;
  90. float temp = 0.0;
  91. float hum = 0.0;
  92. float diff = 1.0;
  93.  
  94. void loop() {
  95.   if (!client.connected()) {
  96.     reconnect();
  97.   }
  98.   client.loop();
  99.  
  100.   long now = millis();
  101.   if (now - lastMsg > 360) {
  102.     lastMsg = now;
  103.  
  104.     float newTemp = dht.readTemperature();
  105.     float newHum = dht.readHumidity();
  106.  
  107.     if (checkBound(newTemp, temp, diff)) {
  108.       temp = newTemp;
  109.       Serial.print("New temperature:");
  110.       Serial.println(String(temp).c_str());
  111.       client.publish(temperature_topic, String(temp).c_str(), true);
  112.     }
  113.  
  114.     if (checkBound(newHum, hum, diff)) {
  115.       hum = newHum;
  116.       Serial.print("New humidity:");
  117.       Serial.println(String(hum).c_str());
  118.       client.publish(humidity_topic, String(hum).c_str(), true);
  119.     }
  120.   }
  121. }
RAW Paste Data
Top