//email:
//password:

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL2-y7STF43"
#define BLYNK_TEMPLATE_NAME "SMART INCUBATOR"
#define BLYNK_AUTH_TOKEN "aqDABVudQpR7pCjS8yspkNSPXX5eUag0"

//connections
#define heater   19
#define fan      18
#define humidifier 5
#define buzzer   14
#define door     13
#define servoPin 23
#define DHT_PIN  15

//libraries
#include "DHTesp.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>

DHTesp dhtSensor;
Servo myservo;
LiquidCrystal_I2C lcd(0x27,20,4);

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

float Temperature,maxT;
float Humidity,maxH;

bool doorState;
bool swingState;
String notification;


BLYNK_WRITE(V3)
{
 int data=param.asInt();
  if(data!= maxT)
  {
    maxT=data;
  }
}

BLYNK_WRITE(V4)
{
 int data=param.asInt();
  if(data!= maxH)
  {
    maxH=data;
  }
}

void setup() 
{
  Serial.begin(9600);
  lcd.init();                  
  lcd.backlight();
  welcomeDisplay();

  //gpio config
  pinMode(heater,OUTPUT);
  pinMode(fan,OUTPUT);
  pinMode(humidifier,OUTPUT);
  pinMode(buzzer,OUTPUT);
  pinMode(door, INPUT_PULLUP);
  digitalWrite(heater,LOW);
  digitalWrite(fan,LOW);
  digitalWrite(humidifier,LOW);
  digitalWrite(buzzer,LOW);

  //servo config
	ESP32PWM::allocateTimer(0);
	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);
	ESP32PWM::allocateTimer(3);
	myservo.setPeriodHertz(50);   
	myservo.attach(servoPin, 1000, 2000);
 
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  Blynk.begin(auth, ssid, pass);
}

void loop() 
{
  homeDisplay();
  Blynk.run();
  getData();
  action();
  send_update();
}

void welcomeDisplay()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("SMART EGGS INCUBATOR"); 
  lcd.setCursor(0,1);
  lcd.print(" SUMAIT UNIVERSIT  "); 
  lcd.setCursor(0,2);
  lcd.print(" SYSTEM INITIALIZE ");
  lcd.setCursor(0,3);
  lcd.print("PLEASE WAIT");
  for(int k=11;k<20;k++)
  {
    lcd.setCursor(k,3);
    lcd.print(".");
    delay(500);
  }
}

void homeDisplay()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("INCUBATOR CONDITION"); 
  lcd.setCursor(0,1);
  lcd.print("TEMPERATURE:"); 
  lcd.setCursor(12,1);
  lcd.print(Temperature,1);
  lcd.setCursor(0,2);
  lcd.print("HUDITY:");
  lcd.setCursor(8,2);
  lcd.print(Humidity,1);
  lcd.setCursor(0,3);
  lcd.print(notification);
  delay(500);
}

void getData()
{
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  Temperature = data.temperature;
  Humidity =data.humidity;
  doorState=digitalRead(door);
}

void action()
{

}

void turnEggs()
{
  for(uint8_t i=90;i>=45;i--)
  {
    myservo.write(i);
    delay(10);
  }
  for(uint8_t i=45;i<=135;i++)
  {
    myservo.write(i);
    delay(10);
  }
  for(uint8_t i=135;i>=90;i--)
  {
    myservo.write(i);
    delay(10);
  }
}

void send_update() 
{
  Blynk.virtualWrite(V0,Temperature);
  Blynk.virtualWrite(V1,Humidity);
  Blynk.virtualWrite(V2,notification);
  delay(500);  
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module