/*#define BLYNK_TEMPLATE_ID "TMPL6YpuF0jRP"
#define BLYNK_TEMPLATE_NAME "Water Level"
#define BLYNK_AUTH_TOKEN "NdCWs-1S9wno3MOjf2GoKp5DFMjTWfKd"*/
/*Index
V0 //Right Tank Gauge
V1 //for Drain/Fill RightTank
V2 //Time
V3 //Left Tank Gauge
V4 //for Drain/Fill LeftTank
The device can send data to the App using Blynk.virtualWrite(pin, value)
and receive data from the App using BLYNK_WRITE(vPIN).
*/
/*
Used Pins
2 Output onBoardLED
4 Output buzz
12 Output trig
18 Output trigLeft
25 Output ExternalLed
13 Input echo
19 Input echoLeft
Ok to use Output & Input 13 16 17 21 22 23 25 26 27 32 33 ( {21 22} for LCD)
Ok to use Input Only 34 35 36 39
*/
#define BLYNK_TEMPLATE_ID "TMPL6Uxs9p6G9"
#define BLYNK_TEMPLATE_NAME "Garage Tanks"
#define BLYNK_AUTH_TOKEN "oCILRQsAs6TquPXa5T2lflGrHVd88lxs"
#define BLYNK_PRINT Serial
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
//Include the library files
#include <Wire.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include "time.h"
#include <Arduino.h>
#include <OTAWebUpdater.h> //local file
#include <WiFiMulti.h>
#include <Wire.h> //LCD
#include <LiquidCrystal_I2C.h> //LCD
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2); //LCD
LiquidCrystal_I2C LCD2 = LiquidCrystal_I2C(0x26, 16, 2); //LCD
int MinsForStairsTime;
int SecForStairsTime;
int MinsForGarageTime;
int SecForGarageTime;
char TimerStairsForFirebase[8];
char TimerGarageForFirebase[8];
int CountDownDuration =600; //600 = 10 mins for countdown
//set the LED pins to output
#include <CountDown.h> //local file
WiFiMulti wifiMulti;
// WiFi connect timeout per AP. Increase when connecting takes longer.
const uint32_t connectTimeoutMs = 10000;
const int RSSI_MAX =-50;// define maximum strength of signal in dBm
const int RSSI_MIN =-100;// define minimum strength of signal in dBm
#define DEEP_SLEEP_TIME 5
//#include <LiquidCrystal_I2C.h>
#define onBoardLED 2
#define ExternalLed 25
/*#define LED2 4
#define LED3 5
#define LED4 18*/
#define trig 12
#define echo 13
#define trigLeft 18
#define echoLeft 19
//#define relay 14
//Relay
#define ACTIVATED HIGH
#define DEACTIVATED LOW
#define ON HIGH
#define OFF LOW
int StairMotorRelay = 16;
int GarageMotorRelay = 17;
char StairMotorRelayStateLCD[5];
char GarageMotorRelayStateLCD[5];
bool WriteCountDownStairs;
bool WriteCountDownGarage;
int StairAcSensor =34;
int GarageAcSensor =35;
bool StairMotorState;
bool GarageMotorState;
//Relay
int buzz=4; //FOR BUZZER
int count; //for power saving
// FOR TIME @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec =10800; // 60*60* (+3 GMT) =10800 Jordan TimeZone
const int daylightOffset_sec = 1; //no Daylight saving in Jordan Currently
struct tm timeinfo;
// FOR TIME @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
char Time[30];
char TimeNoSpace[30]; //to send notification to BLynk email without space in the beginning (the space was created by me (GG) to make time center allign in the APP (last Update))
char BootTime[30]; //to save boot time
bool FirstBoot;
char Hour[3]; //Firebase
char Minute[3];//Firebase
#define MaxSize 128
char EmptyNotifRight[MaxSize];
char HalfNotifRight[MaxSize];
char FullNotifRight[MaxSize];
char EmptyNotifLeft[MaxSize];
char EmergencyAlert[MaxSize];
char FullNotifLeft[MaxSize];
BlynkTimer timer;
// Enter your Auth token
//
char auth[] = "oCILRQsAs6TquPXa5T2lflGrHVd88lxs";
//Enter your WIFI SSID and password
char ssid[] = "SCHRUTE_FARMS";
char pass[] = "*thatswhatshesaid*";
int RSSI_Percentage ;// for int dBmtoPercentage(int dBm) function
String localIPADString; // for int dBmtoPercentage(int dBm) function
//byte ap_mac[18] = { 0xCC, 0xD4, 0x2E, 0x88, 0xD0, 0x6E };
IPAddress staticIP(192, 168, 1, 220); // Your Desired Static IP Address
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns (8, 8, 8, 8);
int RightMode;
int LeftMode;
char RightModeLCD[4]; //for lcd Drain/Fill for right Tank
char LeftModeLCD[4]; //for lcd Drain/Fill for Left Tank
char StairMotorLCDState[4]; //for lcd Stair motor on or off
char GarageMotorLCDState[4]; //for lcd Garage motor on or off
int powerstate=1;
int blynkDistance; //for Right Gauge V0
int blynkDistanceLeft; //for Left Gauge V3
int blynkDistancePer; //for void PercForLCD()
int blynkDistanceLeftPer; //for void PercForLCD()
int RoofFireBaseDistancePer; //for void PercForLCD()
char blynkDistancePercentage[6];// convert blynkDistance to %
char blynkDistanceLeftPercentage[6];// convert blynkDistanceLeft to %
int RoofFireBaseDistance; //measure distance on left ROOF sensor (Get by FIREBASE) // it was delcared here because we need <FireBaseHeadRead.h> to see it //to recv it from Firebase
char RoofFireBaseDistancePercentage[6];// convert RoofFireBaseDistance to %
int RoofFireBaseHourInteger; // it was delcared here because we need <FireBaseHeadRead.h> to see it //to recv time from Firebase to compare with HourInteger (this board time)
int RoofFireBaseMinuteInteger; // it was delcared here because we need <FireBaseHeadRead.h> to see it //to recv time from Firebase to compare with MinuteInteger (this board time)
int HourInteger; // it was delcared here because we need <FireBaseHeadRead.h> to see it //to recv it from Firebase
int MinuteInteger; // it was delcared here because we need <FireBaseHeadRead.h> to see it //to recv it from Firebase
int distance ;//measure distance on Right sensor
int distanceLeft; //measure distance on left sensor
const int RightTankEmpty=166; //Right Tank empty height in CM
const int RightTankPlacementOffset=29; // Sensor palcemnt offset (from the top)
//const int BlynkFullRange=144.0 //for PercForLCD()
const int LeftTankEmpty=173; //Left Tank empty height in CM
const int LeftTankPlacementOffset=36; //Sensor palcemnt offset (from the top)
int IsRoofIncreasing; //values should be as : -1 decreasing 1 increasing 0 no changes at all
BLYNK_WRITE(V1) //for Drain/Fill RightTankBLYNK_WRITE
{
int therightmode=param.asInt();
RightMode=therightmode;
}
BLYNK_WRITE(V4) //for Drain/Fill LeftTank
{
int theLeftmode=param.asInt();
LeftMode=theLeftmode;
}
/*BLYNK_WRITE(V4){ //for sleep mode
int thepowerstate=param.asInt();
powerstate=thepowerstate;
}*/
//For auto check if increasing or not%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 300000; // interval at which to blink (milliseconds) 1k=seconds 60000(60k)=mints >>>300000(300k) =5mnts
//const long intervalForCheck_Right_Left_DRN_FLN_Change=300000; //5 mins //DRN COMMENTED
const long intervalForcheckFlowRate=300000;
const long intervalForBurnAvoidance=420000; // 7 mins
unsigned long previousMillisForRoofOfflineCheck = 0; // will store last time LED was updated
//unsigned long previousMillisForCheck_Right_Left_DRN_FLN_Change=0; //DRN COMMENTED
unsigned long previousMillisForcheckFlowRate=0;
unsigned long previousMillisForRoofOfflineCheckDeletedTree = 0; // will store last time LED was updated
// constants won't change:
const long intervalForRoofOfflineCheck = 180000; // interval 3Mnts
unsigned long previousMillisStairMotorRelay = 0; // will store last time LED was updated
unsigned long previousMillisGarageMotorRelay = 0; // will store last time LED was updated
unsigned long currentMillisAutoChangeRightTankMode = 0; // will store last time LED was updated
unsigned long previousMillisForWasFilledWithin2HoursGarage = 0; // will store last time Roof Tank WasFilledWithin2HoursGarage
unsigned long previousMillisForStairMotorBurnAvoidance=0;
unsigned long previousMillisForGarageMotorBurnAvoidance=0;
//const long intervalForRelay = 600000; // interval at which to blink (milliseconds) 1k=seconds 60000(60k)=mints >>>300000(300k) =5mnts >>>> 600K=10mnts
const long intervalForRelay = 600000; // interval at which to blink (milliseconds) 1k=seconds 60000(60k)=mints >>>300000(300k) =5mnts >>>> 600K=10mnts //BETA
const long intervalForWasFilledWithin2HoursGarage = 7200000; // interval at which to blink (milliseconds) 1k=seconds 60000(60k)=mints >>>300000(300k) =5mnts >>>> 600K=10mnts //BETA
/* int ArrayRight[3]; //DRN COMMENTED
int ArrayLeft[3];
int I_CheckBoth; //i
int arrFlowRate[3];
int I_FlowRate; */
double FlowRateGarageRight=404.00f;//calucalte Flow rate for Garage Right Tank
//double FlowRateGarageRight=0; //calucalte Flow rate for Garage Right Tank
//char FlowRateGarageRightChar [6];
char ETforGarage2BDoneFirebase [32];
int ETforGarage2BDoneMins;
bool WasFilledWithin2HoursGarage;
bool JustBootedInitValueForFlowRate; //true upon booting, false when runs at least once
#include <FireBaseHeadRead.h> //local file
//For auto check if increasing or not%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void goToSleep(){
Serial.printf("Going to Sleep for %d Minutes...\n",DEEP_SLEEP_TIME);
esp_sleep_enable_timer_wakeup(DEEP_SLEEP_TIME*60*1000000);
esp_deep_sleep_start();
}
/*void goToModemSleep(){
Serial.printf("Going to modem Sleep ");
delay (500);
esp_sleep_enable_timer_wakeup(3*60*1000000);
esp_light_sleep_start();
//esp_deep_sleep_start();
}*/
TaskHandle_t Task0;
TaskHandle_t Task1;
void setup() {
// Debug console
Serial.begin(9600);
pinMode(buzz, OUTPUT); //FOR BUZZER
pinMode(onBoardLED, OUTPUT);
pinMode(ExternalLed, OUTPUT);
/*pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);*/
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(trigLeft, OUTPUT);
pinMode(echoLeft, INPUT);
//pinMode(relay, OUTPUT);
// digitalWrite(relay, HIGH);
/* lcd.setCursor(0, 0);
lcd.print("System");
lcd.setCursor(4, 1);
lcd.print("Loading..");*/
pinMode(StairMotorRelay, OUTPUT);
pinMode(GarageMotorRelay, OUTPUT);
pinMode(StairAcSensor, INPUT );
pinMode(GarageAcSensor, INPUT);
digitalWrite(StairMotorRelay,DEACTIVATED);// initalize relay to be OFF upon booting
digitalWrite(GarageMotorRelay,DEACTIVATED); // initalize relay to be OFF upon booting
strncpy(StairMotorRelayStateLCD, "DCTV", 5);//for lcd //LCD
strncpy(GarageMotorRelayStateLCD, "DCTV", 5);//for lcd //LCD
WriteCountDownStairs=false; //initalize with NO PRINTING COUNTDOWN
WriteCountDownGarage=false; //initalize with NO PRINTING COUNTDOWN
WasFilledWithin2HoursGarage=false;
//FlowRateGarageRight= 0.00; //calucalte Flow rate for Garage Right Tank
// strncpy(FlowRateGarageRightChar, "NuLL!", 6);//for lcd //LCD
strncpy(ETforGarage2BDoneFirebase, "N/A", 32);//for lcd //LCD
FirstBoot=1;
//ETforGarage2BDoneMins=0;
//set the LED pins to output
LCD.init(); //LCD
LCD.backlight();//LCD
LCD.clear();//LCD
LCD2.init(); //LCD
LCD2.backlight();//LCD
LCD2.clear();//LCD
LCD.setCursor(0, 0);//LCD
LCD.print("booting...");//LCD
if (WiFi.config(staticIP, gateway, subnet,dns) == true) {
WiFi.hostname( "ESP32-Garage" );
LCD.setCursor(0, 1);
LCD.print("WiFi Conf Succed");
Serial.println("Wi-Fi Static Configuration Succeeded.");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
Serial.print("Local ESP32 Gateway: ");
Serial.println(WiFi.gatewayIP());
Serial.print("Local ESP32 Subnet: ");
Serial.println(WiFi.subnetMask());
}
else{
Serial.println("Configuration failed.");
LCD.clear(); //LCD
LCD.setCursor(0, 0);//LCD
LCD.print("WiFi Conf Failed");//LCD
}
// Set ESP32 as a Wi-Fi Station
WiFi.mode(WIFI_STA);
WiFi.setTxPower(WIFI_POWER_19_5dBm); // Set WiFi RF power output to highest level
// Add list of wifi networks
wifiMulti.addAP("SCHRUTE_FARMS", "*thatswhatshesaid*");
wifiMulti.addAP("IoT", "*thatswhatshesaid*");
// WiFi.scanNetworks will return the number of networks found
LCD.clear(); //LCD
LCD.setCursor(0, 0);//LCD
LCD.print("WiFi Scanning...");//LCD
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
}
else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
tone(buzz, 800, 100); // Plays 262Hz tone for seconds //FOR BUZZER
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(" ) ");
Serial.println(WiFi.BSSIDstr(i));
delay(10);
tone(buzz, 0, 50); // Plays 262Hz tone for 0.250 seconds //FOR BUZZER
}
}
// Connect to Wi-Fi using wifiMulti (connects to the SSID with strongest connection)
LCD.clear(); //LCD
LCD.setCursor(0, 0);//LCD
LCD.print("Connecting to...");//LCD
// char WiFiSSIDforLCD[24] =WiFi.SSID();
LCD.setCursor(6, 1);//LCD
LCD.print("Wi-Fi");//LCD
//LCD.print(WiFi.SSID());//LCD
// LCD.print(WiFi.SSID());//LCD
Serial.println("Connecting Wifi...");
if(wifiMulti.run() == WL_CONNECTED) {
LCD.clear(); //LCD
LCD.setCursor(0, 0);//LCD
LCD.print(WiFi.SSID());//LCD
LCD.setCursor(0,1);//LCD
LCD.print(WiFi.BSSIDstr());//LCD
Serial.println("");
Serial.print("WiFi connected ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
Serial.print("Router MAC Address: ");
Serial.println(WiFi.BSSIDstr());
}
//######WIFI MULTI
// delay(1000 );// to see wifi info on screen
LCD.clear(); //LCD
LCD.setCursor(0,0);//LCD
LCD.print("Connecting Blynk");//LCD
Blynk.begin(auth, ssid, pass,"blynk.cloud", 80);
// lcd.init();
//lcd.backlight();
LCD.setCursor(0,1);//LCD
LCD.print("Config Time...");//LCD
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
LCD.setCursor(0,1);//LCD
LCD.print("Config WebServer");//LCD
WebserverSetup();
FirebaseInSetup();
// LCD.clear(); //LCD
//LCD.print("Config FireBase");//LCD
tone(buzz, 1200, 1500); // Plays 262Hz tone for 0.250 seconds //FOR BUZZER
// delay(4000);
// lcd.clear();
tone(buzz, 0, 250); // Plays 262Hz tone for 0.250 seconds //FOR BUZZER
printLocalTime();//save boot time
ultrasonic(); // save first value measure fo FlowRate in arrFlowRate[0]
JustBootedInitValueForFlowRate=false; //true upon booting, false when runs at least once
checkFlowRate(); //run first thing to save init value for FlowRatePrev
/*arrFlowRate[0]=distance; //DRN COMMENTED
ArrayRight[0]=distance;
ArrayLeft[0]=distanceLeft;
I_FlowRate=1; // change index of arrFlowRate array to index 1
I_CheckBoth=1;
*/
xTaskCreatePinnedToCore(
Task0code, /* Task function. */
"Task0", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task0, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(10);
//create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
delay(10);
}
//Get the ultrasonic sensor values
void ultrasonic() {
// if (powerstate==1) {
digitalWrite(onBoardLED, HIGH); //Led on
digitalWrite(ExternalLed, HIGH); //Led on
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(20);
digitalWrite(trig, LOW);
long t = pulseIn(echo, HIGH);
distance = (t / 29) / 2;
//delayMicroseconds(20);
delayMicroseconds(100);
digitalWrite(trigLeft, LOW);
delayMicroseconds(2);
//delayMicroseconds(2);
digitalWrite(trigLeft, HIGH);
delayMicroseconds(20);
digitalWrite(trigLeft, LOW);
long tLeft = pulseIn(echoLeft, HIGH);
distanceLeft = (tLeft / 29) / 2;
//Blynk.virtualWrite(V1, distance);
printLocalTime();
//snprintf(EmptyNotifRight,MaxSize,"( In Garage ), RIGHT Tank is Empty, Disconnect Motor...\nOn: %s",TimeNoSpace);
snprintf( HalfNotifRight,MaxSize,"( In Garage ), RIGHT Tank is Half\nOn: %s",TimeNoSpace);
//snprintf(FullNotifRight,MaxSize,"( In Garage ), RIGHT Tank is about to Flood, Disconnect Motor & Close Valves\nOn: %s",TimeNoSpace);
//snprintf(EmptyNotifLeft,MaxSize,"( In Garage ), LEFT Tank is Empty, Disconnect Motor\nOn: %s",TimeNoSpace);
snprintf( EmergencyAlert,MaxSize,"Roof Is Offline\nOn: %s",TimeNoSpace);
//snprintf( FullNotifLeft,MaxSize,"( In Garage ), LEFT Tank is about to Flood, Disconnect Motor & Close Valves\nOn: %s",TimeNoSpace);
snprintf(EmptyNotifRight,MaxSize,"\n\n( In Garage ➡️ ), [ RIGHT ] Tank is EMPTY\nOn: %s",TimeNoSpace);
snprintf(FullNotifRight,MaxSize,"\n\n( In Garage ➡️ ), [ RIGHT ] Tank is FULL\nOn: %s",TimeNoSpace);
snprintf(EmptyNotifLeft,MaxSize,"\n\n( In Garage ⬅️ ), [ LEFT ] Tank is EMPTY\nOn: %s",TimeNoSpace);
snprintf( FullNotifLeft,MaxSize,"\n\n( In Garage ⬅️ ), [ LEFT ] Tank is FULL\nOn: %s",TimeNoSpace);
Blynk.virtualWrite(V2, Time); //send time to App
if (RightMode==0) {
strncpy(RightModeLCD, "FLN", 4);//for lcd //LCD
Serial.println("Right Tank is in Filling Mode 0");
}
else if (RightMode==1) {
strncpy(RightModeLCD, "DRN", 4);//for lcd //LCD
Serial.println("Right Tank is in Draining Mode 1");
}
if (LeftMode==0) {
strncpy(LeftModeLCD, "FLN", 4);//for lcd //LCD
Serial.println("Left Tank is in Filling Mode 0");
}
else if (LeftMode==1) {
strncpy(LeftModeLCD, "DRN", 4);//for lcd //LCD
Serial.println("Left Tank is in Draining Mode 1");
}
//int blynkDistance = (distance - MaxLevel) * -1;
blynkDistance = (-(distance-RightTankPlacementOffset)); //for Right Gauge V0
blynkDistanceLeft= (-(distanceLeft-LeftTankPlacementOffset)); //for Left Gauge V3 //36 instead of 29 because 7 cm difference between Thanks LeftTankPlacementOffset=36
if (distance>500){ // when sensor disconnects it shows 608-609cm
Blynk.virtualWrite(V0, 1); //to know if the sensor is disconnected
FireBaseEvent("Garage/Events/Right/","Right Tank Sensor Disconnected");
}
else if (distance <= RightTankEmpty) { //real reading for RIGHT sensor
Blynk.virtualWrite(V0, blynkDistance);
} else //distance is >160 so let it -145
{
Blynk.virtualWrite(V0, -139); //hardcoded
}
// lcd.setCursor(0, 0);
// lcd.print("WLevel:");
if (distanceLeft>500){ // when sensor disconnects it shows 608-609cm
Blynk.virtualWrite(V3, 1); //to know if the sensor is disconnected
FireBaseEvent("Garage/Events/Left/","Left Tank Sensor Disconnected");
}
else if (distanceLeft <= LeftTankEmpty) { //real reading for LEFT sensor
Blynk.virtualWrite(V3, blynkDistanceLeft);
} else { //distance is >160 so let it -145
Blynk.virtualWrite(V3, -139);
}
//BLYNK_WRITE();
//////FILLING STARTS
// RIGHT TANK STARTS
if ( distance >= RightTankEmpty-2) { //RightTankEmpty=168 cm
Serial.printf("Right Tank= %d cm\n", distance);
Serial.println("Right Tank is EMPTY");
if (RightMode==1) { //Draining
//Blynk.logEvent("empty_tank","( In Garage ), RIGHT Tank is Empty, Disconnect Motor");
tone(buzz, 1200, 1500); // Plays 262Hz tone for seconds //FOR BUZZER
Blynk.logEvent("empty_tank",EmptyNotifRight);
Serial.println("empty_tank (RIGHT) notification sent");
/*unsigned long currentMillisAutoChangeRightTankMode= millis(); //check to Turn Relay OFF again //BETA
if (currentMillisAutoChangeRightTankMode - currentMillisAutoChangeRightTankMode >= 120000) { //after //2 mnts change the mode
currentMillisAutoChangeRightTankMode=currentMillisAutoChangeRightTankMode;
RightMode=0; //change the mode to filling
Blynk.virtualWrite(V1, RightMode); //update blynk app button status to the new one
LeftMode=0; //change the mode to filling
Blynk.virtualWrite(V4, LeftMode); //update blynk app button status to the new one
}
*/
tone(buzz, 0, 250); // Plays 262Hz tone for 0.250 seconds //FOR BUZZER
}
}
/*else if ( distance >= 120 && distance < 160) {
Serial.printf("Right Tank= %d cm\n", distance);
Serial.println("Right Tank is Very Low");
}
else if ( distance > 80 && distance < 120) {
Serial.printf("Right Tank= %d cm\n", distance);
if ( distance == 80){
//Blynk.logEvent("half_tank","( In Garage ), RIGHT Tank is Half");
Blynk.logEvent("half_tank",HalfNotifRight);
Serial.println("half_tank RIGHT notification sent");
}
Serial.println("Right Tank is Qurater");
}
else if ( distance >= 75 && distance <= 80) {
Serial.printf("Right Tank= %d cm\n", distance);
Serial.println("Right Tank is Half");
}
else if ( distance >= 40 && distance < 75 ) {
Serial.printf("Right Tank= %d cm\n", distance);
Serial.println("Right Tank is 3 Quarters");
}
else if ( distance > 18 && distance < 40) {
Serial.printf("Right Tank= %d cm\n", distance);
Serial.println("Right Tank is Almost Full");
Tank is Flooding"\);
}
else if ( distance > 16 && distance <= 18) {
Serial.printf("Right Tank= %d cm\n", distance);
Serial.println("Right Tankis Full");
} */
else if ( distance >= 21 && distance <= RightTankPlacementOffset+1) { //30 distance == -1 blynkDistance RightTankPlacementOffset+1)=> 29+1=30
Serial.printf("Right Tank= %d cm\n", distance);
Serial.println("Right Tank is Flooding");
if (RightMode==0) { //Filling
tone(buzz, 1200, 1500); // Plays 262Hz tone for seconds //FOR BUZZER
//Blynk.logEvent("full_tank","( In Garage ), RIGHT Tank is about to Flood, Disconnect Motor & Close Valves");
Blynk.logEvent("full_tank",FullNotifRight);
Serial.println("full_tank (RIGHT) notification sent");
/*unsigned long currentMillisAutoChangeRightTankMode= millis(); //check to Turn Relay OFF again //BETA
if (currentMillisAutoChangeRightTankMode - currentMillisAutoChangeRightTankMode >= 120000) {
currentMillisAutoChangeRightTankMode=currentMillisAutoChangeRightTankMode;
RightMode=1;
LeftMode=1;
} */
tone(buzz, 0, 250); // Plays 262Hz tone for 0.250 seconds //FOR BUZZER
}
}
else {
Serial.printf("%d cm\n", distance);
}
//RIGHT TANK ENDS
//###################################################################
//***LEFT TANK STARTS
if ( distanceLeft >= LeftTankEmpty-2) { //LeftTankEmpty==174 cm
Serial.printf("Left Tank= %d cm\n", distanceLeft);
Serial.println("Left Tank is EMPTY");
if (LeftMode==1) { //Draining
tone(buzz, 1200, 1500); // Plays 262Hz tone for seconds //FOR BUZZER
//Blynk.logEvent("empty_tank","( In Garage ), LEFT Tank is Empty, Disconnect Motor");
Blynk.logEvent("empty_tank",EmptyNotifLeft);
Serial.println("empty_tank (LEFT) notification sent");
tone(buzz, 0, 250); // Plays 262Hz tone for 0.250 seconds //FOR BUZZER
}
}
/*
else if ( distanceLeft >= 120 && distanceLeft < 160) {
Serial.printf("Left Tank= %d cm\n", distanceLeft);
Serial.println("Left Tank is Very Low");
}
else if ( distanceLeft > 80 && distanceLeft < 120) {
Serial.printf("Left Tank= %d cm\n", distanceLeft);
if ( distanceLeft == 80){
// Blynk.logEvent("half_tank","( In Garage ), LEFT Tank is Half");
Blynk.logEvent("half_tank",HalfNotifLeft);
Serial.println("half_tank LEFT notification sent");
}
Serial.println("Left Tank is Qurater");
}
else if ( distanceLeft >= 75 && distanceLeft <= 80) {
Serial.printf("Left Tank= %d cm\n", distanceLeft);
Serial.println("Left Tank is Half");
}
else if ( distanceLeft >= 40 && distanceLeft < 75 ) {
Serial.printf("Left Tank= %d cm\n", distanceLeft);
Serial.println("Left Tank is 3 Quarters");
}
else if ( distanceLeft > 18 && distanceLeft < 40) {
Serial.printf("Left Tank= %d cm\n", distanceLeft);
Serial.println("Left Tank is Almost Full");
}
else if ( distanceLeft > 16 && distanceLeft <= 18) {
Serial.printf("Left Tank= %d cm\n", distanceLeft);
Serial.println("Left Tank is Full");
}*/
else if ( distanceLeft >= 21 && distanceLeft <= LeftTankPlacementOffset+1) { //37 distanceLeft == -1 blynkDistanceLeft LeftTankPlacementOffset+1)=> 36+1=37
Serial.printf("Left Tank= %d cm\n", distanceLeft);
Serial.println("Left Tank is Flooding");
if (LeftMode==0) { //Filling
tone(buzz, 1200, 1500); // Plays 262Hz tone for seconds //FOR BUZZER
// Blynk.logEvent("full_tank","( In Garage ), LEFT Tank is about to Flood, Disconnect Motor & Close Valves");
Blynk.logEvent("full_tank",FullNotifLeft);
Serial.println("full_tank (LEFT) notification sent");
tone(buzz, 0, 250); // Plays 262Hz tone for 0.250 seconds //FOR BUZZER
}
}
else {
Serial.printf("Left Tank= %d cm\n", distanceLeft);
}
PercForLCD();//convert Distance to percentage for LCD
//***LEFT TANK ENDS
digitalWrite(onBoardLED, LOW);
digitalWrite(ExternalLed, LOW);
}
/*else if (powerstate==0){ //let go to sleep
goToSleep();
}*/
//}
// FOR TIME @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void printLocalTime(){
while(getLocalTime(&timeinfo)==0){
Serial.println("Failed to obtain time");
Serial.println("Getting time from NTP Server...");
getLocalTime(&timeinfo);
delay(100);
}
strftime(Time, sizeof(Time), " %a, %r", &timeinfo);
strftime(TimeNoSpace, sizeof(TimeNoSpace), "%a, %r", &timeinfo);
if (FirstBoot==1){
strftime(BootTime, sizeof(BootTime), "%a, %b %d || %r", &timeinfo);
FirstBoot=0;
}
//For DataBase
strftime(Hour, sizeof(Hour), "%I", &timeinfo);
strftime(Minute, sizeof(Minute),"%M", &timeinfo);
HourInteger=atoi(Hour);
MinuteInteger=atoi(Minute);
//For DataBase
Serial.println(&timeinfo, "%A, %B %d %Y, %r");
Serial.println();
}
/*
void CheckRightFunc(int arr[])
{
if (arr[0] > arr[1] && distance!=0 && RightMode!=0) //Right Tank is Filling and Sensor is connected (!=0) and the mode is not Filling (!=0)
{
RightMode=0; //change the mode to filling
Blynk.virtualWrite(V1, RightMode); //update blynk app button status to the new one
Serial.println( "Right Tank Level is Increasing"); //latest distance arr[1] < less than Previous distance arr[0]
}
else if (arr[0] < arr[1] && distance!=0 && RightMode!=1) //Right Tank is Draining and Sensor is connected (!=0) and the mode is not Draining (!=1)
{
RightMode=1; //change the mode to Draining
Blynk.virtualWrite(V1, RightMode); //update blynk app button status to the new one
Serial.println( "Right Tank Level is Decreasing"); //latest distance arr[1] > Larger than Previous distance arr[0]
}
}
void CheckLeftFunc(int arrLeft[])
{
if (arrLeft[0] > arrLeft[1] && distanceLeft!=0 && LeftMode!=0) //Left Tank is Filling and Sensor is connected (!=0) and the mode is not Filling (!=0)
{
LeftMode=0; //change the mode to filling
Serial.println( "Left Tank Level is Increasing"); //latest distance arr[1] < less than Previous distance arr[0]
Blynk.virtualWrite(V4, LeftMode); //change blynk app buttin status
}
else if (arrLeft[0] < arrLeft[1]&& distanceLeft!=0 && LeftMode!=1) //Left Tank is Draining and Sensor is connected (!=0) and the mode is not Draining (!=1)
{
LeftMode=1; //change the mode to Draining
Serial.println( "Left Tank Level is Decreasing"); //latest distance arr[1] > Larger than Previous distance arr[0]
Blynk.virtualWrite(V4, LeftMode); //change blynk app buttin status
}
} //end of CheckLeftFunc
*/
/* void checkFlowRate(){
unsigned long currentMillisForcheckFlowRate= millis(); //check if the water increasing or decrasing
if (currentMillisForcheckFlowRate - previousMillisForcheckFlowRate>= intervalForcheckFlowRate) {
// save the last time you blinked the LED
previousMillisForcheckFlowRate = currentMillisForcheckFlowRate;
arrFlowRate[I_FlowRate]=distance;
//FlowRateGarageRight= arrFlowRate[I_FlowRate]- arrFlowRate[I_FlowRate -1];
Serial.printf( "I_FlowRate=%d\n",I_FlowRate);
if ( (arrFlowRate[I_FlowRate-1] > arrFlowRate[I_FlowRate] ) || (arrFlowRate[I_FlowRate-1] < arrFlowRate[I_FlowRate] ) )
{
FlowRateGarageRight= (arrFlowRate[I_FlowRate-1]-arrFlowRate[I_FlowRate]) / 5.0 ; //calculate flow rate new reaading - last reading devided by 5 mins the whole ionterbal this calc the rate per min
Serial.println("Flowrate is + ir - ");
Firebase.RTDB.setString(&fbdo, "Garage/Arr/", String("a[0]= ") + String(arrFlowRate[0]) + String(" / a[1]= ") + String(arrFlowRate[1]) + String(" /Substratc= ")+ String(arrFlowRate[0]-arrFlowRate[1]) + String(" /Frate= ") +String(FlowRateGarageRight) ); //write Roof Distance Recv from Firebase
}
else if (arrFlowRate[I_FlowRate-1] == arrFlowRate[I_FlowRate] ){
FlowRateGarageRight= 0; //calculate flow rate new reaading - last reading devided by 5 mins the whole ionterbal this calc the rate per min
Firebase.RTDB.setString(&fbdo, "Garage/Arr/", String("a[0]= ") + String(arrFlowRate[0]) + String(" / a[1]= ") + String(arrFlowRate[1]) + String(" /Substratc= ")+ String(arrFlowRate[0]-arrFlowRate[1]) + String(" /Frate= ") +String(FlowRateGarageRight) );
Serial.println("Flowrate is 0 ");
}
//if negative it increrasing if positive it is increasing
// Firebase.RTDB.setString(&fbdo, "Garage/Debugging/FlowRate Array/",String("a[") +String (I_FlowRate-1) + String ("]= ")+ String(arrFlowRate[I_FlowRate-1]) + String(" / a[")+String (I_FlowRate) + String ("]= ")+ String(arrFlowRate[I_FlowRate]) + String(" /Substract= ")+ String(arrFlowRate[I_FlowRate-1]-arrFlowRate[I_FlowRate]) + String(" /Frate= ") +String(FlowRateGarageRight) +String (" @ ")+TimeNoSpace );
// sprintf(FlowRateGarageRightChar, "%.2f", FlowRateGarageRight);
//FlowRateGarageRight = strtof(FlowRateGarageRightChar)
if (I_FlowRate==2){
arrFlowRate[0]=arrFlowRate[2];
I_FlowRate=0;
Serial.println("I_FlowRate=2 String");
}
I_FlowRate++;
}
Serial.println("end of Flowrate Func");
}*/
void checkFlowRate(){
static int FlowRatePrev;
static int FlowRatePrevLeft;
if (JustBootedInitValueForFlowRate==false){
FlowRatePrev=distance;
FlowRatePrevLeft=distanceLeft;
JustBootedInitValueForFlowRate=true;
}
unsigned long currentMillisForcheckFlowRate= millis(); //check if the water increasing or decrasing
if (currentMillisForcheckFlowRate - previousMillisForcheckFlowRate>= intervalForcheckFlowRate) {
int FlowRateCurrent=distance;
int FlowRateCurrentLeft=distanceLeft;
// save the last time you blinked the LED
previousMillisForcheckFlowRate = currentMillisForcheckFlowRate;
// ********************* Integrated Check_Roof_DRN_FLN_Change() *************************** Put her before the original FUnc cuz, in the FlowRate Func we change the value of the FlowRatePrev
if ( FlowRatePrev > FlowRateCurrent && distance!=0 && RightMode!=0) //Right Tank is Filling and Sensor is connected (!=0) and the mode is not Filling (!=0) //I_CheckBoth-1 ==> the first reading I_CheckBoth is the latest reading
{
RightMode=0; //change the mode to filling
Blynk.virtualWrite(V1, RightMode); //update blynk app button status to the new one
Serial.println( "Right Tank Level is Increasing"); //latest distance arr[1] < less than Previous distance arr[0]
//Firebase.RTDB.setString(&fbdo, "Garage/Debugging/ArrRight/",String("a[") +String (I_CheckBoth-1) + String ("]= ")+ String(ArrayRight[I_CheckBoth-1]) + String(" / a[")+String (I_CheckBoth) + String ("]= ")+ String(ArrayRight[I_CheckBoth]) +String("Increasing") +String("Rightmode= ")+String (RightMode)+String (" @ ")+TimeNoSpace );
}
else if (FlowRatePrev < FlowRateCurrent && distance!=0 && RightMode!=1) //Right Tank is Draining and Sensor is connected (!=0) and the mode is not Draining (!=1)
{
RightMode=1; //change the mode to Draining
Blynk.virtualWrite(V1, RightMode); //update blynk app button status to the new one
Serial.println( "Right Tank Level is Decreasing"); //latest distance arr[1] > Larger than Previous distance arr[0]
//Firebase.RTDB.setString(&fbdo, "Garage/Debugging/ArrRight/",String("a[") +String (I_CheckBoth-1) + String ("]= ")+ String(ArrayRight[I_CheckBoth-1]) + String(" / a[")+String (I_CheckBoth) + String ("]= ")+ String(ArrayRight[I_CheckBoth]) +String("Decreasing") +String("Rightmode= ")+String (RightMode)+String (" @ ")+TimeNoSpace );
}
////LEFT TANK CHECK @@@@@@@@@@@@@@####################$$$$$$$$$$$$$$$$$$$$$$$$$$$$$%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&&&&&&&&&&&&&&&&&&&&
if (FlowRatePrevLeft > FlowRateCurrentLeft && distanceLeft!=0 && LeftMode!=0) //Left Tank is Filling and Sensor is connected (!=0) and the mode is not Filling (!=0)
{
LeftMode=0; //change the mode to filling
Blynk.virtualWrite(V4, LeftMode); //change blynk app buttin status
Serial.println( "Left Tank Level is Increasing"); //latest distance arr[1] < less than Previous distance arr[0]
//Firebase.RTDB.setString(&fbdo, "Garage/Debugging/ArrLeft/",String("a[") +String (I_CheckBoth-1) + String ("]= ")+ String(ArrayLeft[I_CheckBoth-1]) + String(" / a[")+String (I_CheckBoth) + String ("]= ")+ String(ArrayLeft[I_CheckBoth]) +String("Increasing") +String("Leftmode= ")+String (LeftMode)+String (" @ ")+TimeNoSpace );
}
else if (FlowRatePrevLeft < FlowRateCurrentLeft && distanceLeft!=0 && LeftMode!=1) //Left Tank is Draining and Sensor is connected (!=0) and the mode is not Draining (!=1)
{
LeftMode=1; //change the mode to Draining
Serial.println( "Left Tank Level is Decreasing"); //latest distance arr[1] > Larger than Previous distance arr[0]
Blynk.virtualWrite(V4, LeftMode); //change blynk app buttin status
// Firebase.RTDB.setString(&fbdo, "Garage/Debugging/ArrLeft/",String("a[") +String (I_CheckBoth-1) + String ("]= ")+ String(ArrayLeft[I_CheckBoth-1]) + String(" / a[")+String (I_CheckBoth) + String ("]= ")+ String(ArrayLeft[I_CheckBoth]) +String("Decreasing") +String("Leftmode= ")+String (LeftMode)+String (" @ ")+TimeNoSpace );
}
//**** Original CheckFlowRate Func*****************
if (FlowRatePrev != FlowRateCurrent )
{
FlowRateGarageRight= (FlowRatePrev-FlowRateCurrent) / 5.00 ; //calculate flow rate new reaading - last reading devided by 5 mins the whole ionterbal this calc the rate per min
Serial.println("Flowrate is + ir - ");
Firebase.RTDB.setString(&fbdo, "Garage/Arr/", String("FlowRatePrev= ") + String(FlowRatePrev) +String(" / FlowRateCurrent= ") +String(FlowRateCurrent) + String(" /Subtract= ")+ String(FlowRatePrev-FlowRateCurrent) + String(" /Frate= ") +String(FlowRateGarageRight) +String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
}
else if (FlowRatePrev == FlowRateCurrent ){
FlowRateGarageRight= 0.00; //calculate flow rate new reaading - last reading devided by 5 mins the whole ionterbal this calc the rate per min
Firebase.RTDB.setString(&fbdo, "Garage/Arr/", String("FlowRatePrev= ") + String(FlowRatePrev) +String(" / FlowRateCurrent= ") +String(FlowRateCurrent) + String(" /Subtract= ")+ String(FlowRatePrev-FlowRateCurrent) + String(" /Frate= ") +String(FlowRateGarageRight) +String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
Serial.println("Flowrate is 0 ");
}
FlowRatePrev=FlowRateCurrent;
FlowRatePrevLeft=FlowRateCurrentLeft;
}
Serial.println("end of Flowrate Func");
}
//****************************************************************************
/*void Check_Right_Left_DRN_FLN_Change(){
unsigned long currentMillisForCheck_Right_Left_DRN_FLN_Change = millis(); //check if the water increasing or decrasing
if (currentMillisForCheck_Right_Left_DRN_FLN_Change - previousMillisForCheck_Right_Left_DRN_FLN_Change >= intervalForCheck_Right_Left_DRN_FLN_Change) {
// save the last time you blinked the LED
previousMillisForCheck_Right_Left_DRN_FLN_Change = currentMillisForCheck_Right_Left_DRN_FLN_Change;
Serial.printf( "I_CheckBoth=%d\n",I_CheckBoth);
ArrayRight[I_CheckBoth]=distance;
ArrayLeft[I_CheckBoth]=distanceLeft;
if (ArrayRight[I_CheckBoth-1] > ArrayRight[I_CheckBoth] && distance!=0 && RightMode!=0) //Right Tank is Filling and Sensor is connected (!=0) and the mode is not Filling (!=0) //I_CheckBoth-1 ==> the first reading I_CheckBoth is the latest reading
{
RightMode=0; //change the mode to filling
Blynk.virtualWrite(V1, RightMode); //update blynk app button status to the new one
Serial.println( "Right Tank Level is Increasing"); //latest distance arr[1] < less than Previous distance arr[0]
Firebase.RTDB.setString(&fbdo, "Garage/Debugging/ArrRight/",String("a[") +String (I_CheckBoth-1) + String ("]= ")+ String(ArrayRight[I_CheckBoth-1]) + String(" / a[")+String (I_CheckBoth) + String ("]= ")+ String(ArrayRight[I_CheckBoth]) +String("Increasing") +String("Rightmode= ")+String (RightMode)+String (" @ ")+TimeNoSpace );
}
else if (ArrayRight[I_CheckBoth-1] < ArrayRight[I_CheckBoth] && distance!=0 && RightMode!=1) //Right Tank is Draining and Sensor is connected (!=0) and the mode is not Draining (!=1)
{
RightMode=1; //change the mode to Draining
Blynk.virtualWrite(V1, RightMode); //update blynk app button status to the new one
Serial.println( "Right Tank Level is Decreasing"); //latest distance arr[1] > Larger than Previous distance arr[0]
Firebase.RTDB.setString(&fbdo, "Garage/Debugging/ArrRight/",String("a[") +String (I_CheckBoth-1) + String ("]= ")+ String(ArrayRight[I_CheckBoth-1]) + String(" / a[")+String (I_CheckBoth) + String ("]= ")+ String(ArrayRight[I_CheckBoth]) +String("Decreasing") +String("Rightmode= ")+String (RightMode)+String (" @ ")+TimeNoSpace );
}
////LEFT TANK CHECK @@@@@@@@@@@@@@####################$$$$$$$$$$$$$$$$$$$$$$$$$$$$$%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&&&&&&&&&&&&&&&&&&&&
if (ArrayLeft[I_CheckBoth-1] > ArrayLeft[I_CheckBoth] && distanceLeft!=0 && LeftMode!=0) //Left Tank is Filling and Sensor is connected (!=0) and the mode is not Filling (!=0)
{
LeftMode=0; //change the mode to filling
Blynk.virtualWrite(V4, LeftMode); //change blynk app buttin status
Serial.println( "Left Tank Level is Increasing"); //latest distance arr[1] < less than Previous distance arr[0]
Firebase.RTDB.setString(&fbdo, "Garage/Debugging/ArrLeft/",String("a[") +String (I_CheckBoth-1) + String ("]= ")+ String(ArrayLeft[I_CheckBoth-1]) + String(" / a[")+String (I_CheckBoth) + String ("]= ")+ String(ArrayLeft[I_CheckBoth]) +String("Increasing") +String("Leftmode= ")+String (LeftMode)+String (" @ ")+TimeNoSpace );
}
else if (ArrayLeft[I_CheckBoth-1] < ArrayLeft[I_CheckBoth]&& distanceLeft!=0 && LeftMode!=1) //Left Tank is Draining and Sensor is connected (!=0) and the mode is not Draining (!=1)
{
LeftMode=1; //change the mode to Draining
Serial.println( "Left Tank Level is Decreasing"); //latest distance arr[1] > Larger than Previous distance arr[0]
Blynk.virtualWrite(V4, LeftMode); //change blynk app buttin status
Firebase.RTDB.setString(&fbdo, "Garage/Debugging/ArrLeft/",String("a[") +String (I_CheckBoth-1) + String ("]= ")+ String(ArrayLeft[I_CheckBoth-1]) + String(" / a[")+String (I_CheckBoth) + String ("]= ")+ String(ArrayLeft[I_CheckBoth]) +String("Decreasing") +String("Leftmode= ")+String (LeftMode)+String (" @ ")+TimeNoSpace );
}
if (I_CheckBoth==2){
Firebase.RTDB.setString(&fbdo, "Garage/Debugging/CheckBoth/",String("I_CheckBoth=2 String") +String(" @ ")+TimeNoSpace );
ArrayRight[0]=ArrayRight[2];
ArrayLeft[0]=ArrayLeft[2];
I_CheckBoth=0;
Serial.println("I_CheckBoth=2 String");
}
I_CheckBoth++;
Serial.println("I_CheckBoth++");
Firebase.RTDB.setString(&fbdo, "Garage/Debugging/CheckBoth++/",String("I_CheckBoth++ || value= ") +String (I_CheckBoth)+String(" @ ")+TimeNoSpace );
}
Firebase.RTDB.setString(&fbdo, "Garage/Debug/",String("end of check Func ") +String(" @ ")+TimeNoSpace );
Serial.println("end of check Func");
} */
void PercForLCD(){ ////LCD
blynkDistancePer= (100- (-blynkDistance) /139.0 *100);
blynkDistanceLeftPer= (100-(-blynkDistanceLeft )/139.0 * 100);
RoofFireBaseDistancePer= (100-(-RoofFireBaseDistance )/139.0 * 100);
/* if(blynkDistancePer>100){
snprintf(blynkDistancePercentage, 6,"ERR");
}
else if(blynkDistanceLeftPer>100){
snprintf(blynkDistanceLeftPercentage, 6,"ERR");
}
else{*/
snprintf(blynkDistancePercentage, 6, "%d%s", blynkDistancePer,"%");
snprintf(blynkDistanceLeftPercentage, 6, "%d%s", blynkDistanceLeftPer,"%");
snprintf(RoofFireBaseDistancePercentage, 6, "%d%s", RoofFireBaseDistancePer,"%");
} //End of PercForLCD
void LcdPrintInLoop(){
LCD.clear(); //LCD
LCD.setCursor(0, 0);//LCD
LCD.print(blynkDistanceLeftPercentage);//LCD
LCD.setCursor(0, 1);//LCD
LCD.print(LeftModeLCD);//LCD
//Right
LCD.setCursor(4, 0);//LCD
LCD.print(blynkDistancePercentage);//LCD
LCD.setCursor(4, 1);//LCD
LCD.print(RightModeLCD);//LCD
//Time
LCD.setCursor(8 ,0);//LCD
LCD.print(&timeinfo, "%r");//LCD
LCD.setCursor(8, 1);
LCD.print(&timeinfo, "%p");//LCD
LCD.setCursor(11, 1);
LCD.print(&timeinfo, "%a");//LCD
LCD2.clear();//LCD
LCD2.setCursor(0, 0);//LCD
LCD2.print(StairMotorLCDState);//LCD Stair motor statee
LCD2.setCursor(4,0);//LCD
LCD2.print(StairMotorRelayStateLCD);//LCD Garage motor state
LCD2.setCursor(0, 1);//LCD
LCD2.print( GarageMotorLCDState);//LCD Stair motor statee
LCD2.setCursor(4, 1);//LCD
LCD2.print(GarageMotorRelayStateLCD);//LCD Garage motor state
// CountDowninLoopStairs();
// CountDowninLoopGarage();
if(WriteCountDownStairs==true){
CountDowninLoopStairs();
LCD2.setCursor(13, 0);
LCD2.print(RoofFireBaseDistancePercentage);//LCD Stair motor statee
}
else {
strncpy(TimerStairsForFirebase, "--:--", 8);//for Firebase
// previousMillisStairsCountDown=0; //to reset the timer
// remainingTimeStairs = CountDownDuration;
LCD2.setCursor(9, 0);
LCD2.print(RoofFireBaseDistancePercentage);//LCD Stair motor statee
}
if (WriteCountDownGarage==true){
CountDowninLoopGarage();
}
else
{
strncpy(TimerGarageForFirebase, "--:--", 8);//for Firebase
// previousMillisGarageCountDown=0; //to reset the timer
//remainingTimeGarage = CountDownDuration;
}
//start at 9
//LCD2.print("ACTV");
}
void ReadACsensor(){
if(digitalRead(StairAcSensor)==ON){
StairMotorState=ON;
strncpy(StairMotorLCDState, "ON", 4);//for lcd //LCD
}
else {
StairMotorState=OFF;
strncpy(StairMotorLCDState, "OFF", 4);//for lcd //LCD
}
if(digitalRead(GarageAcSensor)==ON){
GarageMotorState=ON;
strncpy(GarageMotorLCDState, "ON", 4);//for lcd //LCD
}
else {
GarageMotorState=OFF;
strncpy(GarageMotorLCDState, "OFF", 4);//for lcd //LCD
}
/* Firebase.RTDB.setString(&fbdo, "Garage/Motors/Stair Motor State/", String(StairMotorLCDState) + String(" / ") + String(StairMotorRelayStateLCD) + " / "+ String(TimerStairsForFirebase)+ " / WriteCountDownStairs= " +String (WriteCountDownStairs) ); //write StairMotorLCDState om Firebase
Firebase.RTDB.setString(&fbdo, "Garage/Motors/Garge Motor State/", String(GarageMotorLCDState)+ String(" / ") + String(GarageMotorRelayStateLCD)+ " / "+ String(TimerGarageForFirebase) + " / WriteCountDownGarage= "+String (WriteCountDownGarage) + String(" / ")+ " WasFilledWithin2HoursGarage= "+String(WasFilledWithin2HoursGarage)); //write GarageMotorLCDState om Firebase
*/
}
void checkForStairRelayChange(){ //BETA
//if(strcmp(StairMotorLCDState,"ON") == 0){ //TEST
//if(StairMotorState==ON){ // check if the motor is ON then turn off the motor
if (blynkDistance >= 0 || blynkDistanceLeft >= 0){ //Remeber that blynkdistances are negative
if(StairMotorState==ON){ // check if the motor is ON then turn off the motor
// if(strcmp(StairMotorLCDState,"ON") == 0){ //TEST
digitalWrite(StairMotorRelay,ACTIVATED);// Activate Relay -- Turn off (Stair Motor)
strncpy(StairMotorRelayStateLCD, "ACTV", 5);//for lcd //LCD
FireBaseEvent("Garage/Events/Stair Motor/","Stair Motor Automatically Turned OFF {Temporally as a Backup for 10 Minutes} (Garage Tanks Are FULL)");
Blynk.logEvent("emergency_alert","Stair Motor Automatically Turned OFF {Temporally as a Backup for 10 Minutes} (Garage Tanks Are FULL)");
}
}
//if (StairMotorRelay==ON){
if (strcmp(StairMotorRelayStateLCD,"ACTV") == 0){ //TEST
WriteCountDownStairs=true;
//CountDowninLoopStairs(); //print countdown
unsigned long currentMillisStairRelay = millis(); //check to Turn Relay OFF again
if (currentMillisStairRelay - previousMillisStairMotorRelay >= intervalForRelay) {
digitalWrite(StairMotorRelay,DEACTIVATED); //Turn ON (Stair Motor) again, for Future
strncpy(StairMotorRelayStateLCD, "DCTV", 5);//for lcd //LCD
FireBaseEvent("Garage/Events/Stair Motor/","Stair Motor Automatically Back ON Again {10 Minutes passed}");
Blynk.logEvent("emergency_alert","Stair Motor Automatically Back ON Again {10 Minutes passed}");
//delay(2000);
previousMillisStairMotorRelay=currentMillisStairRelay;
//previousMillisStairsCountDown=0; //to reset the timer
remainingTimeStairs = CountDownDuration;
strncpy(TimerStairsForFirebase, "--:--", 8);//for Firebase
}
}
else{
WriteCountDownStairs=false;
remainingTimeStairs = CountDownDuration;
strncpy(TimerStairsForFirebase, "--:--", 8);//for Firebase
}
//Firebase.RTDB.setString(&fbdo, "Garage/Motors/WriteCountDownStairs/", String (WriteCountDownStairs)); //write StairMotorLCDState om Firebase
}
void checkForGarageRelayChange(){ //BETA
//if (strcmp(GarageMotorLCDState,"ON") == 0){
// if (GarageMotorState==ON){
if ( (RoofFireBaseDistance >= 0 && (RoofFireBaseDistance!=403 ||RoofFireBaseDistance!=404)) || distance >= RightTankEmpty || distanceLeft >= LeftTankEmpty || WasFilledWithin2HoursGarage==true ){ //Remeber that blynkDistances are negative
if (GarageMotorState==ON ){
// if (strcmp(GarageMotorLCDState,"ON") == 0){ //TEST
if (RoofFireBaseDistance >= 0 ){ //if roof is FULL OR OFFLINE
if (RoofFireBaseDistance==403 || RoofFireBaseDistance==404) { //IF roof is offline //403 offline (out of date) // 404 not written yet on firebase
FireBaseEvent("Garage/Events/Garage Motor/" ," WARNING: G̶a̶r̶a̶g̶e̶ M̶o̶t̶o̶r̶ A̶u̶t̶o̶m̶a̶t̶i̶c̶a̶l̶l̶y̶ T̶u̶r̶n̶e̶d̶ O̶F̶F̶ {̶T̶e̶m̶p̶o̶r̶a̶l̶l̶y̶ a̶s̶ a̶ B̶a̶c̶k̶u̶p̶ f̶o̶r̶ 1̶0̶ M̶i̶n̶u̶t̶e̶s̶}̶ (Roof Tanks OFFLINE)");
//Blynk.logEvent("emergency_alert"," WARNING: G̶a̶r̶a̶g̶e̶ M̶o̶t̶o̶r̶ A̶u̶t̶o̶m̶a̶t̶i̶c̶a̶l̶l̶y̶ T̶u̶r̶n̶e̶d̶ O̶F̶F̶ {̶T̶e̶m̶p̶o̶r̶a̶l̶l̶y̶ a̶s̶ a̶ B̶a̶c̶k̶u̶p̶ f̶o̶r̶ 1̶0̶ M̶i̶n̶u̶t̶e̶s̶}̶ (Roof Tanks OFFLINE)");
Firebase.RTDB.setString(&fbdo, "Garage/Events/Garage Motor/Roof", String(RoofFireBaseDistance));
}
else //if roof is actually FULL
{
digitalWrite(GarageMotorRelay,ACTIVATED);// Activate Relay -- Turn off (Garge Motor)
strncpy(GarageMotorRelayStateLCD, "ACTV", 5);//for lcd //LCD
FireBaseEvent("Garage/Events/Garage Motor/" ,"Garage Motor Automatically Turned OFF {Temporally as a Backup for 10 Minutes} (Roof Tanks Are FULL)");
Firebase.RTDB.setString(&fbdo, "Garage/Events/Garage Motor/Roof", String(RoofFireBaseDistance));
Blynk.logEvent("emergency_alert","Garage Motor Automatically Turned OFF {Temporally as a Backup for 10 Minutes} (Roof Tanks Are FULL)");
WasFilledWithin2HoursGarage=true; //remeber if Roof was full in the past 2 hours
}
}
else if ( distance >= RightTankEmpty || distanceLeft >= LeftTankEmpty){
digitalWrite(GarageMotorRelay,ACTIVATED);// Activate Relay -- Turn off (Garge Motor)
strncpy(GarageMotorRelayStateLCD, "ACTV", 5);//for lcd //LCD
FireBaseEvent("Garage/Events/Garage Motor/" ,"Garage Motor Automatically Turned OFF {Temporally as a Backup for 10 Minutes} (Garage Tanks Are EMPTY)");
Blynk.logEvent("emergency_alert","Garage Motor Automatically Turned OFF {Temporally as a Backup for 10 Minutes} (Garage Tanks Are EMPTY)");
}
//}//
}
}
//if (GarageMotorRelay==ACTIVATED){
if (strcmp(GarageMotorRelayStateLCD,"ACTV") == 0){ //TEST
WriteCountDownGarage=true;
// CountDowninLoopGarage(); //print countdown
unsigned long currentMillisGarageRelay = millis(); //check to Turn Relay OFF again
if (currentMillisGarageRelay - previousMillisGarageMotorRelay >= intervalForRelay) {
digitalWrite(GarageMotorRelay,DEACTIVATED); //Turn ON (Garge Motor) again, for Future
strncpy(GarageMotorRelayStateLCD, "DCTV", 5);//for lcd //LCD
FireBaseEvent("Garage/Events/Garage Motor/","Garage Motor Automatically Back ON Again {10 Minutes Passed}");
Blynk.logEvent("emergency_alert","Garage Motor Automatically Back ON Again {10 Minutes Passed}");
//delay(2000);
previousMillisGarageMotorRelay=currentMillisGarageRelay;
remainingTimeGarage = CountDownDuration;
strncpy(TimerGarageForFirebase, "--:--", 8);//for Firebase
}
}
else {
WriteCountDownGarage=false;
remainingTimeGarage = CountDownDuration;
strncpy(TimerGarageForFirebase, "--:--", 8);//for Firebase
}
// Firebase.RTDB.setString(&fbdo, "Garage/Motors/WriteCountDownGarage/", String (WriteCountDownGarage)); //write StairMotorLCDState om Firebase
if(WasFilledWithin2HoursGarage==true){
unsigned long currentMillisForWasFilledWithin2HoursGarage = millis(); //check to Turn Relay OFF again
if (currentMillisForWasFilledWithin2HoursGarage - previousMillisForWasFilledWithin2HoursGarage >= intervalForWasFilledWithin2HoursGarage) {
WasFilledWithin2HoursGarage=false;
previousMillisForWasFilledWithin2HoursGarage=currentMillisForWasFilledWithin2HoursGarage;
}
}
}
int dBmtoPercentage(int dBm)
{
if(dBm <= RSSI_MIN)
{
RSSI_Percentage = 0;
}
else if(dBm >= RSSI_MAX)
{
RSSI_Percentage = 100;
}
else
{
RSSI_Percentage = 2 * (dBm + 100);
}
return RSSI_Percentage ;
}//dBmtoPercentage
void ETforGarage2BDone(){
int ETforGarage2BDoneMinsOnly;
int ETforGarage2BDoneSecOnly;
//float FlowRateGarageRightLocal;
// sprintf(FlowRateGarageRightChar, "%.2f", FlowRateGarageRight);
// FlowRateGarageRightLocal=atof(FlowRateGarageRightChar);
if (FlowRateGarageRight>0 && FlowRateGarageRight != 404) {
//if (FlowRateGarageRight>0 ) {
ETforGarage2BDoneMins= (distance-RightTankPlacementOffset) / abs(FlowRateGarageRight);
ETforGarage2BDoneMinsOnly=ETforGarage2BDoneMins/60;
ETforGarage2BDoneSecOnly=ETforGarage2BDoneMins%60;
sprintf(ETforGarage2BDoneFirebase,"%d Hour, %d Minute To Be Full",ETforGarage2BDoneMinsOnly,ETforGarage2BDoneSecOnly);
// sprintf(ETforGarage2BDoneFirebase,"%d Hour, %d Minute To Be Full",ETforGarage2BDoneMins/60,ETforGarage2BDoneMins%60);
//empty 168 and Full 29cm
//Firebase.RTDB.setString(&fbdo, "Garage/Debugging/Flow Rate > 0/", String(TimeNoSpace) + String(FlowRateGarageRight) +String(" cm / minute") + String(" ~~ Estimated Time: ")+String(ETforGarage2BDoneFirebase)); //write Roof Distance Recv from Firebase
}
else if(FlowRateGarageRight<0){
ETforGarage2BDoneMins=(RightTankEmpty-distance) /abs( FlowRateGarageRight);
ETforGarage2BDoneMinsOnly=ETforGarage2BDoneMins/60;
ETforGarage2BDoneSecOnly=ETforGarage2BDoneMins%60;
sprintf(ETforGarage2BDoneFirebase,"%d Hour, %d Minute To Be Empty",ETforGarage2BDoneMinsOnly,ETforGarage2BDoneSecOnly);
// sprintf(ETforGarage2BDoneFirebase,"%d Hour, %d Minute To Be Empty",ETforGarage2BDoneMins/60,ETforGarage2BDoneMins%60);
//Firebase.RTDB.setString(&fbdo, "Garage/Debugging/Flow Rate < 0/", String(TimeNoSpace) + String(FlowRateGarageRight) +String(" cm / minute") + String(" ~~ Estimated Time: ")+String(ETforGarage2BDoneFirebase)); //write Roof Distance Recv from Firebase
}
else if (FlowRateGarageRight==0) {
ETforGarage2BDoneMins=0;
strncpy(ETforGarage2BDoneFirebase, "N/A!", 32);//for Firebase
//Firebase.RTDB.setString(&fbdo, "Garage/Debugging/Flow Rate = 0/", String(TimeNoSpace) + String(FlowRateGarageRight) +String(" cm / minute") + String(" ~~ Estimated Time: ")+String(ETforGarage2BDoneFirebase)); //write Roof Distance Recv from Firebase
}
// Firebase.RTDB.setString(&fbdo, "Garage/Flow Rate/", String(TimeNoSpace) +String(" || ")+ String(FlowRateGarageRight) +String(" cm/minute") + String(" || Estimated Time: ")+String(ETforGarage2BDoneFirebase)); //write Roof Distance Recv from Firebase
}
void StairMotorBurnAvoidance(){ // to check if the motor is on and no Water Flow Avaible from City (Motor Burn Avoidance)
unsigned long currentMillisForStairMotorBurnAvoidance = millis();
if(StairMotorState==ON && FlowRateGarageRight==0){ // check if the motor is ON and water is not increasing
if (currentMillisForStairMotorBurnAvoidance - previousMillisForStairMotorBurnAvoidance >= intervalForBurnAvoidance) { //420000== 7 Mins in millisconds
previousMillisForStairMotorBurnAvoidance=currentMillisForStairMotorBurnAvoidance;
FireBaseEvent("Garage/Events/Stair Motor/","Warning: Stair Motor Burn Avoidance (Check Water Flow or Valve)");
Blynk.logEvent("emergency_alert","Warning: Stair Motor Burn Avoidance (Check Water Flow or Valve)");
}
}
else {
previousMillisForStairMotorBurnAvoidance=currentMillisForStairMotorBurnAvoidance;
}
}
void GarageMotorBurnAvoidance(){ // to check if the motor is on and no Water Flow Avaible from Garage Tanks (Motor Burn Avoidance)
unsigned long currentMillisForGarageMotorBurnAvoidance = millis();
if(GarageMotorState==ON && IsRoofIncreasing != 1 ){ // check if the motor is ON and water is not increasing in rooof
if (currentMillisForGarageMotorBurnAvoidance - previousMillisForGarageMotorBurnAvoidance >= intervalForBurnAvoidance) { //420000== 7 Mins in millisconds
previousMillisForGarageMotorBurnAvoidance=currentMillisForGarageMotorBurnAvoidance;
if (IsRoofIncreasing==0){
FireBaseEvent("Garage/Events/Garage Motor/","Warning: Stair Motor Burn Avoidance (No Water Flow)");
Blynk.logEvent("emergency_alert","Warning: Garage Motor Burn Avoidance (No Water Flow)");
}
else if (IsRoofIncreasing==-1){
FireBaseEvent("Garage/Events/Garage Motor/","Warning: Stair Motor Burn Avoidance (Water Flow is Decreasing)");
Blynk.logEvent("emergency_alert","Warning: Garage Motor Burn Avoidance (Water Flow is Decreasing)");
}
}
}
else {
previousMillisForGarageMotorBurnAvoidance=currentMillisForGarageMotorBurnAvoidance;
}
}
void loop() {}
void Task0code( void * pvParameters ){
for(;;){
Serial.print("Task0 running on core ");
Serial.println(xPortGetCoreID());
FirebaseinLoop();
delay(10);
}
}
//Task2code: blinks an LED every 700 ms
void Task1code( void * pvParameters ){
for(;;){
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
Blynk.syncVirtual(V1); //Sync value from app to ESP32 Drain/fill mode for RIGHT Tank
Blynk.syncVirtual(V4); //Sync value from app to ESP32 Drain/fill mode for LEFT Tank
//if the connection to the stongest hotstop is lost, it will connect to the next network on the list
if (wifiMulti.run(connectTimeoutMs) == WL_CONNECTED) {
Serial.println(" ");
Serial.println(" ");
Serial.println("*****************************************");
Serial.print("WiFi connected to: ");
Serial.println(WiFi.SSID());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
dBmtoPercentage(WiFi.RSSI());
Serial.printf("%d %\n",RSSI_Percentage);
Serial.print("Router MAC Address: ");
Serial.println(WiFi.BSSIDstr());
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
localIPADString=WiFi.localIP().toString().c_str(); //convert IP to string
Serial.println("");
Serial.println("*****************************************");
Serial.println("");\
server.handleClient();
ultrasonic();
Blynk.run();//Run the Blynk library
ReadACsensor();
checkForStairRelayChange(); //BETA
checkForGarageRelayChange(); //BETA
LcdPrintInLoop(); //LCD print all info on LCD
//Check_Right_Left_DRN_FLN_Change(); //DRN COMMENTED
//Serial.println("Debug: under check DRN function in Loop"); //DRN COMMENTED
checkFlowRate();
//Serial.println("Debug: under checkFlowRate function in Loop");
ETforGarage2BDone();
StairMotorBurnAvoidance();
GarageMotorBurnAvoidance();
//FirebaseinLoop();
/*Firebase.RTDB.setString(&fbdo, "Garage/Flow Rate in Loop", TimeNoSpace + String(" FlowRateChar= ") + String(FlowRateGarageRightChar)); //write Roof Distance Recv from Firebase
ETforGarage2BDone();*/
/*unsigned long currentMillisForCheckFunc = millis(); //check if the water increasing or decrasing
if (currentMillisForCheckFunc - previousMillisForCheckFunc >= intervalForCheckFunc) {
// save the last time you blinked the LED
previousMillisForCheckFunc = currentMillisForCheckFunc;
ArrayRight[I_CheckBoth]=distance;
ArrayLeft[I_CheckBoth]=distanceLeft;
//Serial.printf("\nRight Array %d is %d\n",I_CheckBoth, ArrayRight[I_CheckBoth]);
// Serial.printf("\nLeftArray %d is %d\n",I_CheckBoth, ArrayLeft[I_CheckBoth]);
if (I_CheckBoth==1){ //if we have two readings
//for (int i=0;i<2;i++){
// arrFlowRate[i]=ArrayRight[i];
//}
// int sizeofAutoCheckArray = sizeof(autoCheckArray) / sizeof(autoCheckArray[0]);
//Serial.printf("\nn= %d\n",n);
CheckRightFunc(ArrayRight); //call the function and send last two measurements of Right tank to check if it is increasing or decreasing
CheckLeftFunc(ArrayLeft); //call the function and send last two measurements of Left tank to check if it is increasing or decreasing
//checkFlowRate();
I_CheckBoth=-1;
}
// else
I_CheckBoth++;
} // end of Millis If*/
// Firebase.RTDB.setString(&fbdo, "Garage/Debugging/Flow Rate in Loop", TimeNoSpace + String(" FlowRateChar= ") + String(FlowRateGarageRightChar) + String(" FlowRateFloat= ") + String(FlowRateGarageRight) ); //write Roof Distance Recv from Firebase
} //end of WL_CONNECTED IF
else {
Serial.println("WiFi not connected!");
LCD.clear(); //LCD
LCD.setCursor(0, 0);//LCD
LCD.print("Offline...!");//LCD
LCD.setCursor(0, 1);//LCD
LCD.print("Lets Try Again..");//LCD
}
//delay(500); //
delay(10);
}
}
#ifndef FireBaseHeadWrite_h
#define FireBaseHeadWrite_h
#include <Firebase_ESP_Client.h>
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
// Insert Firebase project API Key
#define API_KEY "AIzaSyAMgonjwth6HShHj02xkTOyFdGh7Vh5WJI"
// Insert RTDB URLefine the RTDB URL */
#define DATABASE_URL "https://water-system-825d3-default-rtdb.asia-southeast1.firebasedatabase.app/"
//Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth FirebaseAuthin;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
bool signupOK = false;
//String Buffer;
void FirebaseInSetup(){
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the RTDB URL (required) */
config.database_url = DATABASE_URL;
/* Sign up */
if (Firebase.signUp(&config, &FirebaseAuthin, "", "")){
Serial.println("ok");
signupOK = true;
}
else{
Serial.printf("%s\n", config.signer.signupError.message.c_str());
}
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
Firebase.begin(&config, &FirebaseAuthin);
Firebase.reconnectWiFi(true);
}
void FireBaseEvent(char path[12],char event[24]){
if (Firebase.ready() && signupOK){
/*if ((Firebase.RTDB.setString(&fbdo, String(path)+ TimeNoSpace, String(event))) ){ //Logging Events on Firebase
Serial.print("PASSED ");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.print("FAILED Writing Event ");
Serial.println("REASON: " + fbdo.errorReason());
}*/
Firebase.RTDB.setString(&fbdo, String(path)+ TimeNoSpace, String(event)); //Logging Events on Firebase
}
else //(Firebase.ready() && signupOK) is FALSE
{
Serial.println("Firebase is not Ready && signup isn't OK");
}
}
void parseStringForFirebaseRead(String FirebaseBuffer) {
int commaIndex = FirebaseBuffer.indexOf(','); // Find the first comma
RoofFireBaseDistance = FirebaseBuffer.substring(0, commaIndex).toInt(); // Extract substring before the first comma and convert to integer
FirebaseBuffer = FirebaseBuffer.substring(commaIndex + 1); // Remove the extracted substring and the comma
commaIndex = FirebaseBuffer.indexOf(','); // Find the next comma
RoofFireBaseHourInteger = FirebaseBuffer.substring(0, commaIndex).toInt(); // Extract substring before the next comma and convert to integer
FirebaseBuffer = FirebaseBuffer.substring(commaIndex + 1); // Remove the extracted substring and the comma
commaIndex = FirebaseBuffer.indexOf(','); // Find the next comma
RoofFireBaseMinuteInteger = FirebaseBuffer.substring(0, commaIndex).toInt(); // Extract substring before the next comma and convert to integer
FirebaseBuffer = FirebaseBuffer.substring(commaIndex + 1); // Remove the extracted substring and the comma
IsRoofIncreasing = FirebaseBuffer.toInt(); // The remaining string should be the last number, so directly convert to integer
/*Serial.print("num1: ");
Serial.println(num1);
Serial.print("num2: ");
Serial.println(num2);
Serial.print("num3: ");
Serial.println(num3);*/
/*
commaIndex = FirebaseBuffer.indexOf(','); // Find the next comma
num4 = FirebaseBuffer.substring(0, commaIndex).toInt(); // Extract substring before the next comma and convert to integer
FirebaseBuffer = FirebaseBuffer.substring(commaIndex + 1); // Remove the extracted substring and the comma
num5 = FirebaseBuffer.toInt(); // The remaining string should be the last number, so directly convert to integer
*/
// FirebaseBuffer="";
}
void FirebaseinLoop(){
//
// if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 1 || sendDataPrevMillis == 0)){
//sendDataPrevMillis = millis();
if (Firebase.ready() && signupOK ){
/* if (Firebase.RTDB.getInt(&fbdo, "/Roof/Roof Distance")) {
RoofFireBaseDistance = fbdo.intData();
Firebase.RTDB.getInt(&fbdo, "/Roof/Hour");
RoofFireBaseHourInteger = fbdo.intData();
Firebase.RTDB.getInt(&fbdo, "/Roof/Minute");
RoofFireBaseMinuteInteger=fbdo.intData();*/
if (Firebase.RTDB.getString(&fbdo, "/Roof/Read From")) {
// Buffer=fbdo.stringData();
parseStringForFirebaseRead(fbdo.stringData()); // parsing string from firebase to seperates integers
unsigned long currentMillisForRoofOfflineCheck= millis(); //check if the water increasing or decrasing
if( (HourInteger==RoofFireBaseHourInteger) &&( (MinuteInteger==RoofFireBaseMinuteInteger) || (MinuteInteger==RoofFireBaseMinuteInteger-1)||(MinuteInteger==RoofFireBaseMinuteInteger+1) || (MinuteInteger==RoofFireBaseMinuteInteger-2)||(MinuteInteger==RoofFireBaseMinuteInteger+2) || (MinuteInteger==RoofFireBaseMinuteInteger-3)||(MinuteInteger==RoofFireBaseMinuteInteger+3) )){ //check if new data within 3 mnt delay
previousMillisForRoofOfflineCheck = currentMillisForRoofOfflineCheck;
Serial.print("Roof Tank [Firebase]= ");
Serial.println(RoofFireBaseDistance);
Serial.println(RoofFireBaseHourInteger);
Serial.println(RoofFireBaseMinuteInteger);
}
else {
RoofFireBaseDistance=403;
Serial.printf("\nRoof Tank [Firebase]=%d...Out of Date..\n",RoofFireBaseDistance); //Roof is Offline
Serial.println("emergency_alert (Roof Offline [Outadated Data]) notification will be sent each 3 Minutes");
if (currentMillisForRoofOfflineCheck - previousMillisForRoofOfflineCheck >= intervalForRoofOfflineCheck) {
previousMillisForRoofOfflineCheck = currentMillisForRoofOfflineCheck;
Blynk.logEvent("emergency_alert",EmergencyAlert);
Serial.println("emergency_alert (Roof Offline [Outadated Data]) notification sent");
}
//unsigned long currentMillisForRoofOfflineCheck= millis(); //check if the water increasing or decrasing
//if (currentMillisForRoofOfflineCheck - previousMillisForRoofOfflineCheck >= intervalForRoofOfflineCheck) {
// save the last time you blinked the LED
// previousMillisForRoofOfflineCheck = currentMillisForRoofOfflineCheck;
//Blynk.logEvent("emergency_alert",EmergencyAlert);
// Serial.println("emergency_alert (Roof Offline [Outadated Data]) notification sent");
// }
}
}
else //no data available in Firebase // for if (Firebase.RTDB.getInt(&fbdo, "/test/Roof Distance")) {
{
RoofFireBaseDistance=404;
Serial.println("Read From Firebase Error: Roof Tank [Firebase]= Not Exist");
Serial.println("emergency_alert (Roof Offline [No Data Available]) notification will be sent each 3 Minutes");
unsigned long currentMillisForRoofOfflineCheckDeletedTree= millis(); //check if the water increasing or decrasing
if (currentMillisForRoofOfflineCheckDeletedTree - previousMillisForRoofOfflineCheck >= intervalForRoofOfflineCheck) {
previousMillisForRoofOfflineCheckDeletedTree = currentMillisForRoofOfflineCheckDeletedTree;
Blynk.logEvent("emergency_alert",EmergencyAlert);
Serial.println("emergency_alert (Roof Offline [No Data Available]) notification sent");
}
}
//}
//#5 if ((Firebase.RTDB.setString(&fbdo, "Garage/RIGHT/", TimeNoSpace + String(" / BlynkDistance: ") + String(blynkDistance)+"cm "+String(" / ") + RightModeLCD +String(" / Actual: ") + String(distance)+String(" / ")+String(blynkDistancePercentage))) && (Firebase.RTDB.setString(&fbdo, "Garage/LEFT/", TimeNoSpace + String(" / BlynkDistance: ") + String(blynkDistanceLeft)+"cm "+String(" / ")+ LeftModeLCD +String(" / Actual: ") + String(distanceLeft)+String(" / ")+String(blynkDistanceLeftPercentage))) )
Firebase.RTDB.setString(&fbdo, "Garage/RIGHT/", TimeNoSpace + String(" / BlynkDistance: ") + String(blynkDistance)+"cm "+String(" / ") + RightModeLCD +String(" / Actual: ") + String(distance)+String(" / ")+String(blynkDistancePercentage));
Firebase.RTDB.setString(&fbdo, "Garage/LEFT/", TimeNoSpace + String(" / BlynkDistance: ") + String(blynkDistanceLeft)+"cm "+String(" / ")+ LeftModeLCD +String(" / Actual: ") + String(distanceLeft)+String(" / ")+String(blynkDistanceLeftPercentage)) ;
//for first #5if { //Logging data on Firebase
Firebase.RTDB.setString(&fbdo, "Garage/Boot Time", BootTime ); //Save Boot time on firebase
// Firebase.RTDB.setString(&fbdo, "Garage/Flow Rate/", String(FlowRateGarageRight) +String(" cm / minute") + String(" ~~ Estimated Time: ")+String(ETforGarage2BDoneFirebase)); //write Roof Distance Recv from Firebase
Firebase.RTDB.setString(&fbdo, "Garage/Flow Rate/", String(TimeNoSpace) +String(" || ")+ String(FlowRateGarageRight) +String(" cm/minute") + String(" || Estimated Time: ")+String(ETforGarage2BDoneFirebase)); //write Roof Distance Recv from Firebase
Firebase.RTDB.setString(&fbdo, "Garage/RoofFireBaseDistance/", TimeNoSpace + String(" / RoofBlynkDistance: ") + String(RoofFireBaseDistance) + String("cm / ") + String (RoofFireBaseDistancePercentage)); //write Roof Distance Recv from Firebase
Firebase.RTDB.setString(&fbdo, "Garage/Wi-Fi/",String(RSSI_Percentage) + "% / "+ String(WiFi.RSSI()) + String(" dBm || "+ String(WiFi.BSSIDstr())+String(" || ")+localIPADString));
Firebase.RTDB.setString(&fbdo, "Garage/Motors/Stair Motor State/", String(StairMotorLCDState) + String(" / ") + String(StairMotorRelayStateLCD) + " / "+ String(TimerStairsForFirebase)+ " / WriteCountDownStairs= " +String (WriteCountDownStairs) ); //write StairMotorLCDState om Firebase
Firebase.RTDB.setString(&fbdo, "Garage/Motors/Garge Motor State/", String(GarageMotorLCDState)+ String(" / ") + String(GarageMotorRelayStateLCD)+ " / "+ String(TimerGarageForFirebase) + " / WriteCountDownGarage= "+String (WriteCountDownGarage) + String(" / ")+ " WasFilledWithin2HoursGarage= "+String(WasFilledWithin2HoursGarage)+ String(" / ")+ " IsRoofIncreasing= "+String(IsRoofIncreasing)); //write GarageMotorLCDState om Firebase
/* Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());*/
/* Firebase.RTDB.setString(&fbdo, "Garage/Wi-Fi/RSSI", String(RSSI_Percentage) + "% ~~~ "+ String(WiFi.RSSI()) + String(" dBm "));
Firebase.RTDB.setString(&fbdo, "Garage/Wi-Fi/MAC Address", String(WiFi.BSSIDstr()) );
Firebase.RTDB.setString(&fbdo, "Garage/Wi-Fi/Local IP", localIPADString );
*/
/* }
else {
Serial.println("FAILED Writing Current Status");
Serial.println("REASON: " + fbdo.errorReason());
}*/
}
else
{
Serial.println("Firebase not ready OR signup not OK");
Serial.println("REASON: " + fbdo.errorReason());
}
} //end of Function
//with IF statement
/*
if ((Firebase.RTDB.setString(&fbdo, "Garage/RIGHT/", TimeNoSpace + String(" / BlynkDistance: ") + String(blynkDistance)+"cm "+String(" / ") + RightModeLCD +String(" / Actual: ") + String(distance)+String(" / ")+String(blynkDistancePercentage))) && (Firebase.RTDB.setString(&fbdo, "Garage/LEFT/", TimeNoSpace + String(" / BlynkDistance: ") + String(blynkDistanceLeft)+"cm "+String(" / ")+ LeftModeLCD +String(" / Actual: ") + String(distanceLeft)+String(" / ")+String(blynkDistanceLeftPercentage))) )
{ //Logging data on Firebase
Firebase.RTDB.setString(&fbdo, "Garage/Boot Time", BootTime ); //Save Boot time on firebase
// Firebase.RTDB.setString(&fbdo, "Garage/Flow Rate/", String(FlowRateGarageRight) +String(" cm / minute") + String(" ~~ Estimated Time: ")+String(ETforGarage2BDoneFirebase)); //write Roof Distance Recv from Firebase
Firebase.RTDB.setString(&fbdo, "Garage/Flow Rate/", String(TimeNoSpace) +String(" || ")+ String(FlowRateGarageRight) +String(" cm/minute") + String(" || Estimated Time: ")+String(ETforGarage2BDoneFirebase)); //write Roof Distance Recv from Firebase
Firebase.RTDB.setString(&fbdo, "Garage/RoofFireBaseDistance/", TimeNoSpace + String(" / RoofBlynkDistance: ") + String(RoofFireBaseDistance) + String("cm / ") + String (RoofFireBaseDistancePercentage)); //write Roof Distance Recv from Firebase
Firebase.RTDB.setString(&fbdo, "Garage/Wi-Fi/",String(RSSI_Percentage) + "% / "+ String(WiFi.RSSI()) + String(" dBm || "+ String(WiFi.BSSIDstr())+String(" || ")+localIPADString));
Firebase.RTDB.setString(&fbdo, "Garage/Motors/Stair Motor State/", String(StairMotorLCDState) + String(" / ") + String(StairMotorRelayStateLCD) + " / "+ String(TimerStairsForFirebase)+ " / WriteCountDownStairs= " +String (WriteCountDownStairs) ); //write StairMotorLCDState om Firebase
Firebase.RTDB.setString(&fbdo, "Garage/Motors/Garge Motor State/", String(GarageMotorLCDState)+ String(" / ") + String(GarageMotorRelayStateLCD)+ " / "+ String(TimerGarageForFirebase) + " / WriteCountDownGarage= "+String (WriteCountDownGarage) + String(" / ")+ " WasFilledWithin2HoursGarage= "+String(WasFilledWithin2HoursGarage)); //write GarageMotorLCDState om Firebase
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
//Firebase.RTDB.setString(&fbdo, "Garage/Wi-Fi/RSSI", String(RSSI_Percentage) + "% ~~~ "+ String(WiFi.RSSI()) + String(" dBm "));
//Firebase.RTDB.setString(&fbdo, "Garage/Wi-Fi/MAC Address", String(WiFi.BSSIDstr()) );
//Firebase.RTDB.setString(&fbdo, "Garage/Wi-Fi/Local IP", localIPADString );
}
else {
Serial.println("FAILED Writing Current Status");
Serial.println("REASON: " + fbdo.errorReason());
}
}
else
{
Serial.println("Firebase not ready OR signup not OK");
Serial.println("REASON: " + fbdo.errorReason());
}
}
*/
#endif