// [email protected]
//kist@2024

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL2w0FNBAk6"
#define BLYNK_TEMPLATE_NAME "CHICKEN FEEDING VIA IOT"
#define BLYNK_AUTH_TOKEN "MhWx_CUMkVOObLev7K5q1ruT04Xb8AGa"

#define buzzer      23
#define en1         16
#define en2         17
#define ir1         13
#define ir2         14
#define triggerPin 19
#define echoPin    18

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <HCSR04.h>
#include <LiquidCrystal_I2C.h>

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

HCSR04 hc(triggerPin,echoPin);
LiquidCrystal_I2C lcd(0x27,20,4);

float reserve;
bool rstate=LOW;
String notification;

BLYNK_WRITE(V2)
{
  int data=param.asInt();
  if(data!= rstate)
  {
    rstate=data;
  }
}

void setup()
{
  lcd.init();                  
  lcd.backlight();
  loadingdisplay();
  
  Serial.begin(115200);
  delay(500);

  pinMode(en1,OUTPUT);
  pinMode(en2,OUTPUT);
  pinMode(ir1,INPUT_PULLUP);
  pinMode(ir2,INPUT_PULLUP);
  pinMode(buzzer,OUTPUT);
  digitalWrite(buzzer,LOW);
  digitalWrite(en1,LOW);
  digitalWrite(en2,LOW);

  Blynk.begin(auth, ssid, pass);
}

void loop()
{
 Blynk.run();
 get_data();
 send_update();
 homedisplay();

 if(rstate) 
 {
  alert_display("FEEDING IN PROGRESS");
  Blynk.virtualWrite(V1,"FEEDING IN PROGRESS");
  Alert(5);
  feedChicken();
 }

if(reserve<10)
 {
  alert_display("INSUFFICIENT FOOD ");
  Blynk.virtualWrite(V1,"INSUFFICIENT FOOD ");
  Alert(3); 
  }
else
{
  Blynk.virtualWrite(V1,"ENOUGH FOOD PRESENT");
}
delay(1000);
}

void feedChicken()
{
  while(digitalRead(ir2)==LOW)
  {
    digitalWrite(en1,HIGH);
    digitalWrite(en2,LOW);
  }
  while(digitalRead(ir1)==LOW)
  {
    digitalWrite(en1,LOW);
    digitalWrite(en2,HIGH);
  }
}

void get_data() 
{
  reserve=map(hc.dist(),0,400,100,0);
}

void send_update() 
{     
  Blynk.virtualWrite(V0,reserve);
  delay(150);  
}

void loadingdisplay()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("SMART FEEDING SYSTEM"); 
  lcd.setCursor(0,1);
  lcd.print("FOR CHICKEN VIA IoT"); 
  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("SMART FEEDING SYSTEM"); 
  lcd.setCursor(0,1);
  lcd.print("FOR CHICKEN VIA IoT"); 
  lcd.setCursor(0,2);
  lcd.print("RESERVE:"); 
  lcd.print(reserve); 
  lcd.setCursor(0,3);
  if(rstate){lcd.print("  FEEDING ACTIVATED "); }
  else{lcd.print(" FEEDING DEACTIVATED");}
}

void alert_display(String textAlert)
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("----SYSTEM ALERT----");
  lcd.setCursor(0,1);
  lcd.print("********************");
  lcd.setCursor(0,2);
  lcd.print(textAlert);
  lcd.setCursor(0,3);
  lcd.print("********************");
}

void Alert(uint8_t num)
{
  for(num;num>0;num--)
  {
    digitalWrite(buzzer,HIGH);
    delay(500);
    digitalWrite(buzzer,LOW);
    delay(500);
  }
}