/*
  email: [email protected]
  paasword: sumait@2024
*/


#define led3       23
#define led1       19
#define led2       18
#define buzzer     4
#define SS_PIN     10
#define RST_PIN    9

#include <WiFi.h>
#include <WiFiClient.h>
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>

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

LiquidCrystal_I2C lcd(0x27,20,4);
MFRC522 rfid(SS_PIN, RST_PIN);

byte nuidPICC[4];
String alert1="HIGH TEMPERATURE",alert2="HIGH PRESSURE",alert3="OVER FLOW",alert4="LOW FUEL";
float Temperature,Pressure,Level;
float maxTemperature=30,maxPressure=85,maxLevel=95,minLevel=5;
const int tankHeight=400;

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);

  SPI.begin();           // MFRC522 Hardware uses SPI protocol
  mfrc522.PCD_Init();    // Initialize MFRC522 Hardware
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
  ShowReaderDetails(); 
  Serial.println("");
  Serial.println(F("-------------------"));
  Serial.println(F("Everything is ready"));
  Serial.println(F("Waiting PICCs to be scanned"));

}

void loop()
{
 Blynk.run();
 get_data();
 send_update();
 homedisplay();
 digitalWrite(led1,HIGH);
 digitalWrite(led2,LOW);
}

String ReadID() 
{
  byte ReadCard[4]; 
  if ( mfrc522.PICC_IsNewCardPresent()&& mfrc522.PICC_ReadCardSerial()) 
  { 
    Serial.println(F("Scanned PICC's UID:"));
    for ( uint8_t i = 0; i < 4; i++) 
    {  
      ReadCard[i] = mfrc522.uid.uidByte[i];
      Serial.print(ReadCard[i], HEX);
    }
   Serial.println("");
   mfrc522.PICC_HaltA(); // Stop reading
   return convertData(ReadCard);
  }
  else
  {
    return " ";  
  }
}

String convertData(byte Card[4])
{
  String s1=String(Card[0],HEX);
  String s2=String(Card[1],HEX);
  String s3=String(Card[2],HEX);
  String s4=String(Card[3],HEX);
  String Tag = s1+"-"+s2+"-"+s3+"-"+s4;
  Serial.println(Tag);
  return Tag;
}


void get_data() 
{
  Level =map((tankHeight-(hc.dist())),tankHeight,0,0,100);
  Temperature =map(analogRead(34),0,4095,0,50);
  Pressure =map(analogRead(35),0,4095,0,100);
}

void send_update() 
{     
  Blynk.virtualWrite(V0,Level);
  Blynk.virtualWrite(V1,Temperature);
  Blynk.virtualWrite(V2,Pressure);
  delay(150);  
}

void loadingdisplay()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("*FUEL STORAGE TANK*"); 
  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("-FUEL TANK ANALYSIS-"); 
  lcd.setCursor(0,1);
  lcd.print("F.LEVEL:"); 
  lcd.setCursor(9,1);
  lcd.print(Level,1);
  lcd.print("%");
  lcd.setCursor(0,2);
  lcd.print("F.TEMP:");
  lcd.setCursor(9,2);
  lcd.print(Temperature,1);
  lcd.print("°C");
  lcd.setCursor(0,3);
  lcd.print("PRESSURE:");
  lcd.setCursor(9,3);
  lcd.print(Pressure,1);
  lcd.print("%");
  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 Alert(uint8_t num)
{
  for(num;num>0;num--)
  {
    digitalWrite(buzzer,HIGH);
    delay(750);
    digitalWrite(buzzer,LOW);
    delay(500);
  }
}
COINS MODULEBreakout
MP3-PLAYER Breakout
NOCOMNCVCCGNDINLED1PWRRelay Module