#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 
#define OLED_RESET     -1 
#define SCREEN_ADDRESS 0x3C 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#include "DHT.h"
#define DHTPIN 5  
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);


void setup() {
  Serial.begin(9600);

  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.display();
  display.clearDisplay();
  dht.begin(); 
  delay(10); 
};

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t) ) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  display.setTextSize(1);            
  display.setTextColor(SSD1306_WHITE, SSD1306_BLACK); 
  display.setCursor(4, 4);
  display.print("DHT22 TEMP AND HUMID");
  display.setTextSize(1);             
  display.setTextColor(SSD1306_WHITE, SSD1306_BLACK); 
  display.setCursor(20, 30);
  display.print("TEMP: "+String(t)+"  C");  
  display.drawCircle(94, 27,2,WHITE );      
  display.setTextSize(1);         
  display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  display.setCursor(20, 45);
  display.print("HUMI: "+String(h)+"  %");
  display.display();
  delay(2000);
};