/* BLYNK CONNECTION INSTRUCTION
  V0:Heartbeat
  V1:TEMPERATURE
  V2:Depth
  V3:flashbutton
  V5:map
  V6:LATITUDE
  V7:LONGITUDE
*/

//SAMPLE NMEA FOR SIMULATION PURPOSE
const char *gpsStream =
        "$GPGGA,111132.803,0607.800,S,03905.993,E,1,12,1.0,0.0,M,0.0,M,,*7C\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111132.803,A,0607.800,S,03905.993,E,038.9,336.7,100324,000.0,W*6F\r\n"
        "$GPGGA,111133.803,0607.790,S,03905.989,E,1,12,1.0,0.0,M,0.0,M,,*70\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111133.803,A,0607.790,S,03905.989,E,038.9,336.7,100324,000.0,W*63\r\n"
        "$GPGGA,111134.803,0607.781,S,03905.984,E,1,12,1.0,0.0,M,0.0,M,,*7A\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111134.803,A,0607.781,S,03905.984,E,038.9,336.7,100324,000.0,W*69\r\n"
        "$GPGGA,111135.803,0607.771,S,03905.980,E,1,12,1.0,0.0,M,0.0,M,,*70\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111135.803,A,0607.771,S,03905.980,E,038.9,336.7,100324,000.0,W*63\r\n"
        "$GPGGA,111136.803,0607.761,S,03905.976,E,1,12,1.0,0.0,M,0.0,M,,*7B\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111136.803,A,0607.761,S,03905.976,E,038.9,336.7,100324,000.0,W*68\r\n"
        "$GPGGA,111137.803,0607.751,S,03905.972,E,1,12,1.0,0.0,M,0.0,M,,*7D\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111137.803,A,0607.751,S,03905.972,E,038.9,336.7,100324,000.0,W*6E\r\n"
        "$GPGGA,111138.803,0607.741,S,03905.967,E,1,12,1.0,0.0,M,0.0,M,,*77\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111138.803,A,0607.741,S,03905.967,E,038.9,336.7,100324,000.0,W*64\r\n"
        "$GPGGA,111139.803,0607.731,S,03905.963,E,1,12,1.0,0.0,M,0.0,M,,*75\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111139.803,A,0607.731,S,03905.963,E,038.9,336.7,100324,000.0,W*6C\r\n"
        "$GPGGA,111140.803,0607.721,S,03905.959,E,1,12,1.0,0.0,M,0.0,M,,*76\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111140.803,A,0607.721,S,03905.959,E,038.9,336.7,100324,000.0,W*65\r\n"
        "$GPGGA,111141.803,0607.711,S,03905.955,E,1,12,1.0,0.0,M,0.0,M,,*72\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111141.803,A,0607.711,S,03905.955,E,038.9,336.7,100324,000.0,W*6B\r\n"
        "$GPGGA,111142.803,0607.701,S,03905.951,E,1,12,1.0,0.0,M,0.0,M,,*7C\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111142.803,A,0607.701,S,03905.951,E,038.9,336.7,100324,000.0,W*6D\r\n"
        "$GPGGA,111143.803,0607.691,S,03905.947,E,1,12,1.0,0.0,M,0.0,M,,*7E\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111143.803,A,0607.691,S,03905.947,E,038.9,336.7,100324,000.0,W*67\r\n"
        "$GPGGA,111144.803,0607.681,S,03905.943,E,1,12,1.0,0.0,M,0.0,M,,*75\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n"
        "$GPRMC,111144.803,A,0607.681,S,03905.943,E,038.9,336.7,100324,000.0,W*66\r\n"
        "$GPGGA,111145.803,0607.671,S,03905.939,E,1,12,1.0,0.0,M,0.0,M,,*7F\r\n"
        "$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30\r\n";

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL2jPJjbHLs"
#define BLYNK_TEMPLATE_NAME "SMART LIFE JACKET"
#define BLYNK_AUTH_TOKEN "D_vEaWClgF_t3v4z3_O5fRyWpT-upQTp"
#define GPS_BAUDRATE 9600

#define led3       23
#define led1       19
#define led2       18
#define Heater     26
#define barometer  33
#define tempSensor 34
#define bpmSensor  35

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

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

LiquidCrystal_I2C lcd(0x27,20,4);
TinyGPSPlus gps;

String alert1="",alert2="",alert3="",alert4="";
float Latitude,Longitude,Depth,Temperature,Heartbeat,Altitude;
float minTemperature=20;
bool flash,heaterState;

void setup()
{
  lcd.init();                  
  lcd.backlight();
  loadingdisplay();
  
  Serial.begin(9600);
  Serial2.begin(GPS_BAUDRATE);
 
  pinMode(led3,OUTPUT);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(Heater,OUTPUT);
  digitalWrite(Heater,LOW);

  Blynk.begin(auth, ssid, pass);
  Blynk.virtualWrite(V4,minTemperature);
  Blynk.virtualWrite(V3,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
}

void loop()
{
 Blynk.run();
 get_data();
 readGps();
 send_update();
 homedisplay();
 if(Temperature <= minTemperature)
 {
    digitalWrite(Heater,HIGH);
    heaterState=HIGH;
 }
 else
 {
    digitalWrite(Heater,LOW);
    heaterState=LOW;
 }

 if(flash==HIGH)
 {
    flashing(5);
 }

}

BLYNK_WRITE(V3)
{
  int data=param.asInt();
  if(data==1)
  {
    flash=HIGH;
  }
  else
  {
    flash=LOW;
  }
}

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

void get_data() 
{
  Temperature =map(analogRead(tempSensor),0,4095,0,50);
  Heartbeat =map(analogRead(bpmSensor),0,4095,0,100);
  Depth= map(analogRead(barometer),0,4095,0,150);
}

void send_update() 
{     
  Blynk.virtualWrite(V0,Heartbeat);
  Blynk.virtualWrite(V1,Temperature);
  Blynk.virtualWrite(V8,heaterState);
  Blynk.virtualWrite(V2,Depth);
  Blynk.virtualWrite(V6,Latitude);
  Blynk.virtualWrite(V7,Longitude);
  delay(150);  
}

void loadingdisplay()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("*SMART LIFE JACKET*"); 
  lcd.setCursor(0,1);
  lcd.print("*   MONITORING    *"); 
  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 LIFE JACKET-"); 
  lcd.setCursor(0,1);
  lcd.print("H.RATE:"); 
  lcd.setCursor(9,1);
  lcd.print(Heartbeat,1);
  lcd.print("%");
  lcd.setCursor(0,2);
  lcd.print("B.TEMP:");
  lcd.setCursor(9,2);
  lcd.print(Temperature,1);
  lcd.print("°C");
  lcd.setCursor(0,3);
  lcd.print("DEPTH:");
  lcd.setCursor(9,3);
  lcd.print(Depth,1);
  lcd.print("M");
  delay(500);
}

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 flashing(uint8_t num)
{
  for(num;num>0;num--)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    delay(250);
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    delay(250);
  }
}

void readGps()
{
 // if (Serial2.available() > 0) 
 // {
 //   if (gps.encode(Serial2.read())) 
 while (*gpsStream)
    if (gps.encode(*gpsStream++))
    {
      if (gps.location.isValid()) 
      {
        Latitude = gps.location.lat();
        Serial.print(F("Latitude: "));
        Serial.println(Latitude,6);
        Longitude = gps.location.lng();
        Serial.print(F("Longitude: "));
        Serial.println(Longitude,6);
      } 
      else 
      {
        Serial.println(F("Location: INVALID"));
      }
      Serial.println();
    }
 // }
  if (millis() > 5000 && gps.charsProcessed() < 10)
   Serial.println(F("No GPS data received: check wiring"));
}
gps-neo-06Breakout