RichP

ESP8266 DHT Temperature Humidity with OLED Display

Sep 27th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 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.  * See this link for a similar sketch that included the Homie framework for MQTT functionality:
  25.  * http://www.wemustbegeeks.com/esp8266-send-dht-temperature-humidity-mqtt-oled-display/
  26.  */
  27. #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
  28. #include <DHT.h>
  29.  
  30. #define DHTPIN D4     // what pin the DHT is connected to
  31.  
  32. // Initialize the OLED display using Wire library
  33. SSD1306  display(0x3C, D3, D5); // (I2C Address, SCL Pin, SDA Pin)
  34.  
  35.  
  36. // Uncomment whatever type of DHT Sensor you are using!
  37. //#define DHTTYPE DHT11   // DHT 11
  38. #define DHTTYPE DHT22   // DHT 22  (AM2302)
  39. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
  40.  
  41.  
  42. const int TEMPERATURE_INTERVAL = 30; //In seconds
  43. unsigned long last_temperature_sent = 0;
  44.  
  45. const int HUMIDITY_INTERVAL = 30;
  46. unsigned long last_humidity_sent = 0;
  47. //float temperature = 00.00;
  48. //float humidity = 00.00;
  49. String display_temp;
  50. String display_humid;
  51.  
  52. DHT dht(DHTPIN, DHTTYPE);
  53.  
  54. void getSendTemperature() {
  55.   if (millis() - last_temperature_sent >= TEMPERATURE_INTERVAL * 1000UL || last_temperature_sent == 0) {
  56.     float temperature = dht.readTemperature(true);
  57.  
  58.     if (isnan(temperature)) {
  59.       Serial.println("Failed to read from DHT sensor!");
  60.       return;
  61.     }
  62.     display_temp = temperature;
  63.     Serial.print("Temperature: ");
  64.     Serial.print(temperature);
  65.     Serial.println(" °F");
  66.     last_temperature_sent = millis();
  67.   }
  68. }
  69.  
  70. void getSendHumid() {
  71.   if (millis() - last_humidity_sent >= HUMIDITY_INTERVAL * 1000UL || last_humidity_sent == 0) {
  72.     float humidity = dht.readHumidity();
  73.  
  74.     if (isnan(humidity)) {
  75.       Serial.println("Failed to read from DHT sensor!");
  76.       return;
  77.     }
  78.     display_humid = humidity;
  79.     Serial.print("Humidity: ");
  80.     Serial.print(humidity);
  81.     Serial.println(" %");
  82.    
  83.     last_humidity_sent = millis();
  84.   }
  85. }
  86.  
  87. void displayData() {
  88.     display.clear();
  89.     display.setTextAlignment(TEXT_ALIGN_CENTER);
  90.     display.setFont(ArialMT_Plain_10);
  91.     display.drawString(64, 0, "Office Temp/Humidty");  
  92.     display.setFont(ArialMT_Plain_24);
  93.     //String display_temp = String(temperature,2);
  94.     display.drawString(66, 13, display_temp + "F");
  95.     //String display_humid = String(humidity,2);
  96.     display.drawString(66, 40, display_humid + "%");
  97.     display.display();
  98. }
  99.  
  100.  
  101. void setup() {
  102.   Serial.begin(115200);
  103.   // Initialising the UI will init the display too.
  104.   display.init();
  105.   display.clear();
  106.   display.flipScreenVertically();
  107.   display.setTextAlignment(TEXT_ALIGN_CENTER);
  108.   display.display();
  109.   // DHT Setup
  110.   dht.begin();
  111. }
  112.  
  113. void loop() {
  114.   getSendTemperature();
  115.   getSendHumid();
  116.   displayData();  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment