#include "DHT.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void setup() {
// put your setup code here, to run once:
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Clear the display
display.clearDisplay();
//Set the color - always use white despite actual display color
display.setTextColor(WHITE);
//Set the font size
display.setTextSize(1);
//Set the cursor coordinates
display.setCursor(0,0);
display.print("DHT22");
display.setCursor(0,10);
if (isnan(h) || isnan(t) || isnan(f)) {
// Serial.println(F("Failed to read from DHT sensor!"));
display.print("Failed to read from DHT sensor!");
}
else{
display.print("Humidity: ");
display.print(h);
display.print(" %");
display.setCursor(0,20);
display.print("Temp: ");
display.print(t);
display.print("C ");
display.print(f);
display.print("F");
}
display.display();
digitalWrite(13, HIGH);
delay(50);
digitalWrite(13, LOW);
}