Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The MIT License (MIT)
- *
- * Copyright (c) 2016 by Daniel Eichhorn
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- *
- * See this link for a similar sketch that included the Homie framework for MQTT functionality:
- * http://www.wemustbegeeks.com/esp8266-send-dht-temperature-humidity-mqtt-oled-display/
- */
- #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
- #include <DHT.h>
- #define DHTPIN D4 // what pin the DHT is connected to
- // Initialize the OLED display using Wire library
- SSD1306 display(0x3C, D3, D5); // (I2C Address, SCL Pin, SDA Pin)
- // Uncomment whatever type of DHT Sensor you are using!
- //#define DHTTYPE DHT11 // DHT 11
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
- const int TEMPERATURE_INTERVAL = 30; //In seconds
- unsigned long last_temperature_sent = 0;
- const int HUMIDITY_INTERVAL = 30;
- unsigned long last_humidity_sent = 0;
- //float temperature = 00.00;
- //float humidity = 00.00;
- String display_temp;
- String display_humid;
- DHT dht(DHTPIN, DHTTYPE);
- void getSendTemperature() {
- if (millis() - last_temperature_sent >= TEMPERATURE_INTERVAL * 1000UL || last_temperature_sent == 0) {
- float temperature = dht.readTemperature(true);
- if (isnan(temperature)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- display_temp = temperature;
- Serial.print("Temperature: ");
- Serial.print(temperature);
- Serial.println(" °F");
- last_temperature_sent = millis();
- }
- }
- void getSendHumid() {
- if (millis() - last_humidity_sent >= HUMIDITY_INTERVAL * 1000UL || last_humidity_sent == 0) {
- float humidity = dht.readHumidity();
- if (isnan(humidity)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- display_humid = humidity;
- Serial.print("Humidity: ");
- Serial.print(humidity);
- Serial.println(" %");
- last_humidity_sent = millis();
- }
- }
- void displayData() {
- display.clear();
- display.setTextAlignment(TEXT_ALIGN_CENTER);
- display.setFont(ArialMT_Plain_10);
- display.drawString(64, 0, "Office Temp/Humidty");
- display.setFont(ArialMT_Plain_24);
- //String display_temp = String(temperature,2);
- display.drawString(66, 13, display_temp + "F");
- //String display_humid = String(humidity,2);
- display.drawString(66, 40, display_humid + "%");
- display.display();
- }
- void setup() {
- Serial.begin(115200);
- // Initialising the UI will init the display too.
- display.init();
- display.clear();
- display.flipScreenVertically();
- display.setTextAlignment(TEXT_ALIGN_CENTER);
- display.display();
- // DHT Setup
- dht.begin();
- }
- void loop() {
- getSendTemperature();
- getSendHumid();
- displayData();
- }
Advertisement
Add Comment
Please, Sign In to add comment