#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
DHT dht(DHTPIN, DHTTYPE);
int relay =7;
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
}
void loop() {
//
float humi = dht.readHumidity(); // read humidity
float tempC = dht.readTemperature(); // read temperature
//
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
if (isnan(humi) || isnan(tempC)) {
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
oled.println("Failed"); // text to display
} else {
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
oled.println("Temp: "); // text to display
oled.println(tempC); // print the temperature
oled.println((char)223); // print ° character
oled.println("C");
//
oled.setCursor(0, 20); // start to print at the second row
oled.println("Humi: ");
oled.println(humi); // print the humidity
oled.println("%");
//
oled.setCursor(0, 30); // start to print at fourth row
oled.println("Factor: ");
oled.println(((tempC*1.8)+32)+humi);
}
//
if ((((tempC*1.8)+32)+humi) > 200) {
digitalWrite(7,HIGH);
} else {
digitalWrite(7,LOW);
}
}