// This project implements the simulation of the reading a DHT sensor using an ESP32 
// The source code is available on my git repo at : https://github.com/Bamamou/DHT11_ESP32.git
// the only difference is the sensor, Here; we use a DHT11 while in Platform io we use a DHT22
#include <DHT.h> 
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP32Servo.h>

const int servoPin = 18;

Servo servo;

#define SCREEN_WIDTH 128 // OLED width,  in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels

// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Set up the DHT sensor 
DHT dht(4, DHT22);
float temperature ;
float  humidity;
int counter;
int pos = 0;
int red = 2;
int green = 15;
int buzzer = 5;
int button = 23;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(button, INPUT);
  Serial.println("Hello, ESP32!");
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("failed to start SSD1306 OLED"));
    while (1);
  }

  delay(2000);         // wait two seconds for initializing
  oled.clearDisplay(); // clear display

  oled.setTextSize(3);         // set text size
  oled.setTextColor(WHITE);    // set text color
  oled.setCursor(0, 2);       // set position to display (x,y)
  oled.println("Nguyen Vu Minh Khoa - 21161137"); // set text
  oled.display();
}

void loop() {
  // put your main code here, to run repeatedly:
  int check_button = digitalRead(button);
  temperature  = dht.readTemperature();
  humidity     = dht.readHumidity();
  if(temperature > 70|| check_button == 1){
    digitalWrite(green, LOW);
    digitalWrite(buzzer, HIGH);
    digitalWrite(red, HIGH);
    delay(100);
    digitalWrite(red, LOW);
    delay(100);   
  }else {
    digitalWrite(buzzer, LOW);
    digitalWrite(green,HIGH);;
  }    


}