//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 GPS_BAUDRATE 9600

#define RLED       27
#define BLED       25
#define GLED       26
#define BUTTON     13


#include <WiFi.h>
#include <HTTPClient.h>
#include <FirebaseESP32.h> 
#include <LiquidCrystal_I2C.h>
#include <TinyGPS++.h>

#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""

const char* Profile = "test/";
const char* ProfileLT="VESSELS/VESSEL001/LATITUDE"; 
const char* ProfileLN="VESSELS/VESSEL001/LONGITUDE"; 
const char* ProfileNT="VESSELS/VESSEL001/NOTIFICATION"; 
const char* ProfileST="VESSELS/VESSEL001/STATUS"; 
const char* ProfileEC="/EMERGENCY CALL/NEW";
FirebaseData firebaseData;
//#define FIREBASE_HOST "https:firebaseio.com"
#define FIREBASE_HOST "https://ocean-vessel-monitoring-default-rtdb.firebaseio.com/"
#define FIREBASE_AUTH "AIzaSyBxg1uYYxnz_MLCM-JfeocY20cBq01Fwro"

String messageTemp,notification;

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

float Latitude,Longitude;

void setup()
{
  pinMode(25,OUTPUT);
  pinMode(26,OUTPUT);
  pinMode(27,OUTPUT);
  pinMode(BUTTON,INPUT_PULLUP);
  digitalWrite(RLED,HIGH);
  digitalWrite(BLED,LOW);
  digitalWrite(GLED,LOW);

  lcd.init();                  
  lcd.backlight();
  loadingdisplay();
  
  Serial.begin(9600);
  Serial2.begin(GPS_BAUDRATE);

  /*setup_wifi();
  firebase_config();
  if( Firebase.setBool(firebaseData, ProfileST, true) )
 {
   Serial.print("DEVICE ACTIVATED");
 }
 else {
    Serial.print("Error in getString, ");
    Serial.println(firebaseData.errorReason());
  }*/
}

void loop()
{
 readGps();
 homedisplay();
 //send_update();
// checkNotification();
}

String checkNotification()
{
  if (Firebase.getString(firebaseData, ProfileNT))
  {
    notification= firebaseData.stringData();
    Serial.print("NOTIFICATION IS: ");
    Serial.println(messageTemp);
  } 
  else {
    Serial.print("Error in getInt, ");
    Serial.println(firebaseData.errorReason());
  }
  return notification;
}

void send_update() 
{     
  if( Firebase.setString(firebaseData, ProfileLT, String(Latitude)) )
 {
   Serial.print("LATITUDE sent");
 }
 else {
    Serial.print("Error in getString, ");
    Serial.println(firebaseData.errorReason());
  }
  if( Firebase.setString(firebaseData, ProfileLN, String(Longitude)) )
 {
   Serial.print("LONGITUDE sent");
 }
 else {
    Serial.print("Error in getString, ");
    Serial.println(firebaseData.errorReason());
  }
}

void loadingdisplay()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("*  OCEAN VESSELS  *"); 
  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("-OCEAN VESSEL DATA-"); 
  lcd.setCursor(0,1);
  lcd.print("LAT :"); 
  lcd.setCursor(6,1);
  lcd.print(Latitude,1);
  lcd.setCursor(0,2);
  lcd.print("LONG:");
  lcd.setCursor(6,2);
  lcd.print(Longitude,1);
  lcd.setCursor(0,3);
  lcd.print("NTF:");
  lcd.setCursor(5,3);
  lcd.print(messageTemp);
  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(RLED,HIGH);
    digitalWrite(BLED,LOW);
    delay(250);
    digitalWrite(RLED,LOW);
    digitalWrite(BLED,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"));
}

void setup_wifi(void) 
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void firebase_config(void) 
{
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  //Enable auto reconnect the WiFi when connection lost
  Firebase.reconnectWiFi(true);
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
gps-neo-06Breakout
chip1:VCC
chip1:GND
chip1:RX
chip1:TX
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
rgb1:R
rgb1:COM
rgb1:G
rgb1:B