/* BLYNK CONNECTION INSTRUCTION
  V0:STATE
  V1:NAME
  V2:MAP LOCATION
  EMAIL:[email protected]
  PASSWORD:zulora@2024
  data structure: state@latitude&longitude  
*/

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL21Df5SGlh"
#define BLYNK_TEMPLATE_NAME "FIRE ALERT VIA LORA"
#define BLYNK_AUTH_TOKEN "NozLO4x_WLpskXaPG50q15Gj-u8NTj5P"

#define led3       23
#define led1       19
#define led2       18
#define buzzer     4

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

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

LiquidCrystal_I2C lcd(0x27,20,4);

String name;
String latitude;
String longitude;

void setup()
{
  lcd.init();                  
  lcd.backlight();
  loadingdisplay();
  
  Serial.begin(115200);
 
  pinMode(led3,OUTPUT);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(buzzer,OUTPUT);
  digitalWrite(buzzer,LOW);

  Blynk.begin(auth, ssid, pass);
  digitalWrite(led3,HIGH);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  lcd.clear();
}

void loop()
{
 Blynk.run();
 homedisplay();
 if(getData()==HIGH)
  {
    eventDisplay();
    digitalWrite(led1,HIGH);
    digitalWrite(led2,HIGH);
    send_update();
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    lcd.clear();
  }
}

void send_update() 
{     
  Blynk.virtualWrite(V0,name);
  Blynk.virtualWrite(V1,latitude);
  Blynk.virtualWrite(V2,longitude);
  delay(2000);  
}

boolean getData()
{
  if(Serial.available())
  {
    String payload=Serial.readString(); //format: name@latitude&longitude
    name=payload.substring(0,payload.indexOf("@"));
    latitude=payload.substring((payload.indexOf("@")+1),payload.indexOf("&"));
    longitude=payload.substring((payload.indexOf("&")+1),(payload.length()-1));
   return HIGH;
  }
  else
  {
    return LOW;
  }
}


void loadingdisplay()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("*  LORA BASED FIRE *"); 
  lcd.setCursor(0,1);
  lcd.print("*  ALERT SYSTEM   *"); 
  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.setCursor(0,0);
  lcd.print("********************"); 
  lcd.setCursor(0,1);
  lcd.print("* NO FIRE ACCIDENT *");
  lcd.setCursor(0,2);
  lcd.print("*REPORTED @ GATEWAY*");
  lcd.setCursor(0,3);
  lcd.print("********************");  
  delay(500);
}

void eventDisplay()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("FIRE DETECTED AT:"); 
  lcd.setCursor(0,1);
  lcd.print(name); 
  lcd.setCursor(0,2);
  lcd.print("LATITUDE :"); 
  lcd.setCursor(10,2);
  lcd.print(latitude); 
  lcd.setCursor(0,3);
  lcd.print("LONGITUDE:");
  lcd.setCursor(10,3);
  lcd.print(longitude);  
}

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

}

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