RichP

ESP8266 Temp & Humidity Display with HTU21D and Homie MQTT

Oct 24th, 2016
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. /**
  2.  * The MIT License (MIT)
  3.  *
  4.  * Copyright (c) 2016 by Daniel Eichhorn
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  * of this software and associated documentation files (the "Software"), to deal
  8.  * in the Software without restriction, including without limitation the rights
  9.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  * copies of the Software, and to permit persons to whom the Software is
  11.  * furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in all
  14.  * copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.  * SOFTWARE.
  23.  *
  24.  */
  25.  
  26. #include <Homie.h>
  27. //#include <DHT.h>
  28. #include <SparkFunHTU21D.h>
  29. #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
  30.  
  31.  
  32.  
  33. //#define DHTPIN D4     // what pin the DHT is connected to
  34.  
  35. // Initialize the OLED display using Wire library
  36. SSD1306  display(0x3C, D3, D5); // (I2C Address, SCL Pin, SDA Pin)
  37.  
  38. // Uncomment whatever type you're using!
  39. //#define DHTTYPE DHT11   // DHT 11
  40. //#define DHTTYPE DHT22   // DHT 22  (AM2302)
  41. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
  42.  
  43.  
  44. const int TEMPERATURE_INTERVAL = 30;
  45. unsigned long last_temperature_sent = 0;
  46.  
  47. const int HUMIDITY_INTERVAL = 30;
  48. unsigned long last_humidity_sent = 0;
  49. String display_temp;
  50. String display_humid;
  51.  
  52. HomieNode temperatureNode("temperature", "temperature");
  53. HomieNode humidityNode("humidity", "humidity");
  54.  
  55. //DHT dht(DHTPIN, DHTTYPE);
  56. //Create an instance of the object
  57. HTU21D myHumidity;
  58.  
  59. void setupHandler() {
  60.   // Do what you want to prepare your sensor
  61.   display.clear();
  62. }
  63.  
  64.  
  65. void getSendTemperature() {
  66.   if (millis() - last_temperature_sent >= TEMPERATURE_INTERVAL * 1000UL || last_temperature_sent == 0) {
  67.     float temperature = myHumidity.readTemperature();
  68.  
  69.     if (isnan(temperature)) {
  70.       Serial.println("Failed to read from DHT sensor!");
  71.       return;
  72.     }
  73.     display_temp = temperature;
  74.     Serial.print("Temperature: ");
  75.     Serial.print(temperature);
  76.    
  77.     Serial.println(" °F");
  78.     if (Homie.setNodeProperty(temperatureNode, "temperature").send(String(temperature))) {
  79.       last_temperature_sent = millis();
  80.     } else {
  81.       Serial.println("Sending failed");
  82.     }
  83.   }
  84. }
  85.  
  86. void getSendHumid() {
  87.   if (millis() - last_humidity_sent >= HUMIDITY_INTERVAL * 1000UL || last_humidity_sent == 0) {
  88.     float humidity = myHumidity.readHumidity();
  89.  
  90.     if (isnan(humidity)) {
  91.       Serial.println("Failed to read from DHT sensor!");
  92.       return;
  93.     }
  94.     display_humid = humidity;
  95.     Serial.print("Humidity: ");
  96.     Serial.print(humidity);
  97.     Serial.println(" %");
  98.    
  99.     if (Homie.setNodeProperty(humidityNode, "humidity").send(String(humidity))) {
  100.       last_humidity_sent = millis();
  101.     } else {
  102.       Serial.println("Sending failed");
  103.     }
  104.   }
  105. }
  106.  
  107. void displayData() {
  108.     display.clear();
  109.     display.setTextAlignment(TEXT_ALIGN_CENTER);
  110.     display.setFont(ArialMT_Plain_10);
  111.     display.drawString(64, 0, "Office Temp/Humidty");  
  112.     display.setFont(ArialMT_Plain_24);
  113.     display.drawString(66, 13, display_temp + "F");
  114.     display.drawString(66, 40, display_humid + "%");
  115.     display.display();
  116. }
  117.  
  118. void loopHandler() {
  119.   getSendTemperature();
  120.   getSendHumid();
  121.   displayData();  
  122. }
  123.  
  124. void setup() {
  125.   Homie_setFirmware("awesome-temperature", "2.0.0");
  126.   //Homie.registerNode(temperatureNode); //From Home 1.5
  127.   //Homie.registerNode(humidityNode); //From Home 1.5
  128.   Homie.setSetupFunction(setupHandler);
  129.   Homie.setLoopFunction(loopHandler);
  130.   Homie.setup();
  131.   // Initialising the display.
  132.   display.init();
  133.   display.clear();
  134.   display.flipScreenVertically();
  135.   display.setTextAlignment(TEXT_ALIGN_CENTER);
  136.   display.display();
  137.   myHumidity.begin();
  138. }
  139.  
  140. void loop() {
  141.   Homie.loop();
  142. }
Advertisement
Add Comment
Please, Sign In to add comment