// #include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <FirebaseESP32.h>
#define FIREBASE_HOST "https://mydata-179ef-default-rtdb.firebaseio.com/"
#define FIREBASE_AUTH "qtBZ4TXjhv7p8yzjC038fdyV7qZp6cTFWFjKRk9T"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
//Define FirebaseESP32 data object
FirebaseData firebaseData;
FirebaseJson json;
// LiquidCrystal_I2C lcd(0x27, 20, 4);
//onion data
int Onion_PH_E, Onion_PH_min = 6, Onion_PH_max = 7;
int Onion_N_E, Onion_N_min = 60, Onion_N_max = 100;
int Onion_P_E ,Onion_P_min = 25, Onion_P_mas = 45;
int Onion_K_E, Onion_K_min = 45, Onion_K_mas = 80;
float Onion_N_D, Onion_P_D, Onion_K_D;
//corn data
float Corn_PH_E, Corn_PH_min = 5.8, Corn_PH_max = 6.2;
int Corn_N = 120, Corn_P = 60, Corn_K = 40;
int Corn_N_E, Corn_P_E, Corn_K_E;
float Corn_N_D, Corn_P_D, Corn_K_D;
//long chili data
float Chili_PH_E, Chili_PH_min = 5.5, Chili_PH_max = 6.8;
int Chili_N_E, Chili_N_min = 15, Chili_N_max = 20;
float Chili_P_E, Chili_P_min = 2.5, Chili_P_max = 3.0;
float Chili_K_E, Chili_K_min = 15, Chili_K_max = 20;
float Chili_N_D, Chili_P_D, Chili_K_D;
//eggplant data
float Eggplant_PH_E, Eggplant_PH_min = 5.5, Eggplant_PH_max = 7.5;
int Eggplant_N = 129, Eggplant_P = 129, Eggplant_K = 129;
int Eggplant_N_E, Eggplant_P_E, Eggplant_K_E;
float Eggplant_N_D, Eggplant_P_D, Eggplant_K_D;
//test data
int NT1, PT1, KT1;
float NTC1, PTC1, KTC1, PHT1, PHTC1;
int NT2, PT2, KT2;
float NTC2, PTC2, KTC2, PHT2, PHTC2;
int NT3, PT3, KT3;
float NTC3, PTC3, KTC3, PHT3, PHTC3;
int NT4, PT4, KT4;
float NTC4, PTC4, KTC4, PHT4, PHTC4;
float NF, PF, KF;
float NFC, PFC, KFC, PHF;
//test value
int N = 95;
int P = 72;
int K = 65;
float PH = 7.5;
//cvdd
void setup() {
Serial.begin(9600);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
Firebase.setReadTimeout(firebaseData, 1000 * 60);
Firebase.setwriteSizeLimit(firebaseData, "tiny");
Serial.println("------------------------------------");
Serial.println("Connected...");
// lcd.begin(20, 4);
// lcd.backlight();
// lcd.clear();
}
void loop() {
for(int i = 0; i < 4; i++){
if(i == 0){
NT1 = N;
NTC1 = NT1*10*1.1/10;
PT1 = P;
PTC1 = PT1*10*1.1/10;
KT1 = K;
KTC1 = KT1*10*1.1/10;
PHT1 = PH;
// json.set("/NT1", NT1);
// json.set("/PT1", PT1);
// json.set("/KT1", KT1);
// json.set("/PHT1", PHT1);
// json.set("/NTC1", NTC1);
// json.set("/PTC1", PTC1);
// json.set("/KTC1", KTC1);
// Firebase.updateNode(firebaseData,"/Data",json);
}
if(i == 1){
NT2 = N;
NTC2 = NT2*10*1.1/10;
PT2 = P;
PTC2 = PT2*10*1.1/10;
KT2 = K;
KTC2 = KT2*10*1.1/10;
PHT2 = PH;
// json.set("/NT2", NT2);
// json.set("/PT2", PT2);
// json.set("/KT2", KT2);
// json.set("/PHT2", PHT2);
// json.set("/NTC2", NTC2);
// json.set("/PTC2", PTC2);
// json.set("/KTC2", KTC2);
// Firebase.updateNode(firebaseData,"/Data",json);
}
if(i == 2){
NT3 = N;
NTC3 = NT3*10*1.1/10;
PT3 = P;
PTC3 = PT3*10*1.1/10;
KT3 = K;
KTC3 = KT3*10*1.1/10;
PHT3 = PH;
// json.set("/NT3", NT3);
// json.set("/PT3", PT3);
// json.set("/KT3", KT3);
// json.set("/PHT3", PHT3);
// json.set("/NTC3", NTC3);
// json.set("/PTC3", PTC3);
// json.set("/KTC3", KTC3);
// Firebase.updateNode(firebaseData,"/Data",json);
}
if(i == 3){
NT4 = N;
NTC4 = NT4*10*1.1/10;
PT4 = P;
PTC4 = PT4*10*1.1/10;
KT4 = K;
KTC4 = KT4*10*1.1/10;
PHT4 = PH;
// json.set("/NT4", NT4);
// json.set("/PT4", PT4);
// json.set("/KT4", KT4);
// json.set("/PHT4", PHT4);
// json.set("/NTC4", NTC4);
// json.set("/PTC4", PTC4);
// json.set("/KTC4", KTC4);
// Firebase.updateNode(firebaseData,"/Data",json);
}
delay(2000);
}
//tallied test
NF = (NT1+NT2+NT3+NT4)/4;
PF = (PT1+PT2+PT3+PT4)/4;
KF = (KT1+KT2+KT3+KT4)/4;
PHF = (PHT1+PHT2+PHT3+PHT4)/4;
NFC = (NTC1+NTC2+NTC3+NTC4)/4;
PFC = (PTC1+PTC2+PTC3+PTC4)/4;
KFC = (KTC1+KTC2+KTC3+KTC4)/4;
// lcd.clear();
// lcd.setCursor(0, 1);
// lcd.print("Final Co. Converted");
// lcd.setCursor(0, 2);
// lcd.print("(mg/kg)");
// lcd.print(" ");
// lcd.print("(kg/ha)");
// delay(2000);
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("N:");
// lcd.print(NF);
// lcd.print(" ");
// lcd.print("N:");
// lcd.print(NFC);
// lcd.setCursor(0, 1);
// lcd.print("P:");
// lcd.print(PF);
// lcd.print(" ");
// lcd.print("P:");
// lcd.print(PFC);
// lcd.setCursor(0, 2);
// lcd.print("K:");
// lcd.print(KF);
// lcd.print(" ");
// lcd.print("K:");
// lcd.print(KFC);
// lcd.setCursor(0, 3);
// lcd.print("PH:");
// lcd.print(PHF);
// lcd.print(" ");
// lcd.print("PH:");
// lcd.print(PHF);
// json.set("/NF", NF);
// json.set("/PF", PF);
// json.set("/KF", KF);
// json.set("/PHF", PHF);
// json.set("/NFC", NFC);
// json.set("/PFC", PFC);
// json.set("/KFC", KFC);
// Firebase.updateNode(firebaseData,"/Data",json);
delay(3000);
// //Onion
// lcd.clear();
// lcd.setCursor(4, 0);
// lcd.print("N ");
// lcd.print("P ");
// lcd.print("K ");
// lcd.print("PH ");
// lcd.setCursor(0, 1);
// lcd.print("On:");
if(Onion_N_min > NFC){
Onion_N_D = NFC-Onion_N_min;
int onionN = Onion_N_D;
// lcd.print(onionN);
Onion_N_E = 0;
json.set("/Onion_N_D", Onion_N_D);
Firebase.updateNode(firebaseData,"/Data",json);
}
else {
Onion_N_E = 1;//abundaant
json.set("/Onion_N_D", "/");
Firebase.updateNode(firebaseData,"/Data",json);
// lcd.print(" /");
}
// if(Onion_P_min > PFC){
// Onion_P_D = PFC-Onion_P_min;
// int onionP = Onion_P_D;
// // lcd.print(" ");
// // lcd.print(onionP);
// Onion_P_E = 0;
// json.set("/Onion_P_E", Onion_P_E);
// Firebase.updateNode(firebaseData,"/Data",json);
// }
// else {
// Onion_P_E = 1;//abundant
// //lcd.print(" /");
// json.set("/Onion_P_E", Onion_P_E);
// Firebase.updateNode(firebaseData,"/Data",json);
// }
// if(Onion_K_min > KFC){
// Onion_K_D = KFC-Onion_K_min;
// int onionK = Onion_K_D;
// // lcd.print(" ");
// // lcd.print(onionK);
// Onion_K_E = 0;
// json.set("/Onion_K_E", Onion_K_E);
// Firebase.updateNode(firebaseData,"/Data",json);
// }
// else {
// Onion_K_E = 1;//abundant
// // lcd.print(" /");
// json.set("/Onion_K_E", Onion_K_E);
// Firebase.updateNode(firebaseData,"/Data",json);
// }
// if(Onion_PH_min >= PHF && Onion_PH_max <= PHF){
// Onion_PH_E = 1; //abundant
// // lcd.print(" ");
// // lcd.print("/");
// json.set("/Onion_PH_E", Onion_PH_E);
// Firebase.updateNode(firebaseData,"/Data",json);
// }
// else {
// Onion_PH_E = 0; //deficient
// // lcd.print(" ");
// // lcd.print("X");
// json.set("/Onion_PH_E", Onion_PH_E);
// Firebase.updateNode(firebaseData,"/Data",json);
// }
// //Corn
// lcd.setCursor(0, 2);
// lcd.print("Co:");
// if(Corn_N > NFC){
// Corn_N_D = NFC-Corn_N;
// int cornN = Corn_N_D;
// lcd.print(cornN);
// Corn_N_E = 0;
// }
// else {
// lcd.print(" /");
// Corn_N_E = 1;//abundant
// }
// if(Corn_P > PFC){
// Corn_P_D = PFC-Corn_P;
// int cornP = Corn_P_D;
// lcd.print(" ");
// lcd.print(cornP);
// Corn_P_E = 0;
// }
// else {
// lcd.print(" /");
// Corn_P_E = 1;//abundant
// }
// if(Corn_K > KFC){
// Corn_K_D = KFC-Corn_K;
// int cornK = Corn_K_D;
// lcd.print(" ");
// lcd.print(cornK);
// Corn_K_E = 0;
// }
// else {
// lcd.print(" /");
// Corn_K_E = 1;//abundant
// }
// if(Corn_PH_min >= PHF && Corn_PH_max <= PHF){
// Corn_PH_E = 1; //deficient
// lcd.print(" ");
// lcd.print("/");
// }
// else {
// Corn_PH_E = 0; //abundant
// lcd.print(" ");
// lcd.print("X");
// }
// //Chili
// lcd.setCursor(0, 3);
// lcd.print("Ch:");
// if(Chili_N_min > NFC){
// Chili_N_D = NFC-Chili_N_min;
// int chiliN = Chili_N_D;
// lcd.print(chiliN);
// Chili_N_E = 0;
// }
// else {
// Chili_N_E = 1;//abundaant
// lcd.print(" /");
// }
// if(Chili_P_min > PFC){
// Chili_P_D = PFC-Chili_P_min;
// int chiliP = Chili_P_D;
// lcd.print(" ");
// lcd.print(chiliP);
// Chili_P_E = 0;
// }
// else {
// Chili_P_E = 1;//abundant
// lcd.print(" /");
// }
// if(Chili_K_min > KFC){
// Chili_K_D = KFC-Chili_K_min;
// int chiliK = Chili_K_D;
// lcd.print(" ");
// lcd.print(chiliK);
// Chili_K_E = 0;
// }
// else {
// Chili_K_E = 1;//abundant
// lcd.print(" /");
// }
// if(Chili_PH_min >= PHF && Chili_PH_max <= PHF){
// Chili_PH_E = 1; //abundant
// lcd.print(" ");
// lcd.print("/");
// }
// else {
// Chili_PH_E = 0; //deficient
// lcd.print(" ");
// lcd.print("X");
// }
// //eggplant
// delay(3000);
// lcd.clear();
// lcd.setCursor(4, 0);
// lcd.print("N ");
// lcd.print("P ");
// lcd.print("K ");
// lcd.print("PH ");
// lcd.setCursor(0, 1);
// lcd.print("Eg:");
// if(Eggplant_N > NFC){
// Eggplant_N_D = NFC-Eggplant_N;
// int eggplantN = Eggplant_N_D;
// lcd.print(eggplantN);
// Eggplant_N_E = 0;
// }
// else {
// lcd.print(" /");
// Eggplant_N_E = 1;//abundant
// }
// if(Eggplant_P > PFC){
// Eggplant_P_D = PFC-Eggplant_P;
// int eggplantP = Eggplant_P_D;
// lcd.print(" ");
// lcd.print(eggplantP);
// Eggplant_P_E = 0;
// }
// else {
// lcd.print(" /");
// Eggplant_P_E = 1;//abundant
// }
// if(Eggplant_K > KFC){
// Eggplant_K_D = KFC-Eggplant_K;
// int eggplantK = Eggplant_K_D;
// lcd.print(" ");
// lcd.print(eggplantK);
// Eggplant_K_E = 0;
// }
// else {
// lcd.print(" /");
// Eggplant_K_E = 1;//abundant
// }
// if(Eggplant_PH_min >= PHF && Eggplant_PH_max <= PHF){
// Eggplant_PH_E = 1; //deficient
// lcd.print(" ");
// lcd.print("/");
// }
// else {
// Eggplant_PH_E = 0; //abundant
// lcd.print(" ");
// lcd.print("X");
// }
}