How If/else statement works with firebase RTDB in a proper way?

currently my LED light works perfectly with esp8266 and firebase, but when i try to turn it OFF it keeps ON so i think i did something wrong with the code.
I have tried to switch but the same problem keeps occurring, so what is the solution for this problem?

Here is the code:

    #if defined(ESP32)
    #include <WiFi.h>
    #elif defined(ESP8266)
    #include <ESP8266WiFi.h>
    #endif
    #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>
    
    /* 1. Define the WiFi credentials */
    #define WIFI_SSID "Abdallah Agha"
    #define WIFI_PASSWORD "abdallah123"
    
    /** 2. Define the API key
     * 
     * The API key (required) can be obtained since you created the project and set up 
     * the Authentication in Firebase console. Then you will get the API key from
     * Firebase project Web API key in Project settings, on General tab should show the
     * Web API Key.
     * 
     * You may need to enable the Identity provider at https://console.cloud.google.com/customer-identity/providers 
     * Select your project, click at ENABLE IDENTITY PLATFORM button.
     * The API key also available by click at the link APPLICATION SETUP DETAILS.
     * 
    */
    #define API_KEY "AIzaSy*****************"
    
    /* 3. Define the user Email and password that already registerd or added in your project */
    #define USER_EMAIL "[email protected]"
    #define USER_PASSWORD "123456"
    
    /* 4. If work with RTDB, define the RTDB URL */
    #define DATABASE_URL "smart******************" //<databaseName>.firebaseio.com or<databaseName>.<region>.firebasedatabase.app
    
    /** 5. Define the database secret (optional)
     * 
     * This database secret needed only for this example to modify the database rules
     * 
     * If you edit the database rules yourself, this is not required.
    */
    #define DATABASE_SECRET "jLPap1****************"
    
    /* 6. Define the Firebase Data object */
    FirebaseData fbdo;
    
    /* 7. Define the FirebaseAuth data for authentication data */
    FirebaseAuth auth;
    
    /* 8. Define the FirebaseConfig data for config data */
    FirebaseConfig config;
    
    unsigned long dataMillis = 0;
    int count = 0;
    
    String fireStatus = "";                                                     // led status received from firebase
    int led = 5; 
    
    void setup()
    {
    
        Serial.begin(115200);
        WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
        Serial.print("Connecting to Wi-Fi");
        while (WiFi.status() != WL_CONNECTED)
        {
            Serial.print(".");
            pinMode(led, OUTPUT);
            delay(100);
        }
        Serial.println();
        Serial.print("Connected with IP: ");
        Serial.println(WiFi.localIP());
        Serial.println();
    
        Serial.printf("Firebase Client v%snn", FIREBASE_CLIENT_VERSION);
    
        /* Assign the api key (required) */
        config.api_key = API_KEY;
    
        /* Assign the user sign in credentials */
        auth.user.email = USER_EMAIL;
        auth.user.password = USER_PASSWORD;
    
        /* Assign the RTDB URL */
        config.database_url = DATABASE_URL;
    
        Firebase.reconnectWiFi(true);
        fbdo.setResponseSize(4096);
    
        String base_path = "/UsersData/";
    
        /* Assign the callback function for the long running token generation task */
        config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
    
        /** Assign the maximum retry of token generation */
        config.max_token_generation_retry = 100;
    
        /* Initialize the library with the Firebase authen and config */
        Firebase.begin(&config, &auth);
    
        /** Now modify the database rules (if not yet modified)
         *
         * The user, <user uid> in this case will be granted to read and write
         * at the certain location i.e. "/UsersData/<user uid>".
         * 
         * If you database rules has been modified, please comment this code out.
         * 
         * The character $ is to make a wildcard variable (can be any name) represents any node key 
         * which located at some level in the rule structure and use as reference variable
         * in .read, .write and .validate rules
         * 
         * For this case $userId represents any <user uid> node that places under UsersData node i.e.
         * /UsersData/<user uid> which <user uid> is user UID.
         * 
         * Please check your the database rules to see the changes after run the below code.
        */
        
        String var = "$userId";
        String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
        Firebase.RTDB.setReadWriteRules(&fbdo, base_path.c_str(), var.c_str(), val.c_str(), val.c_str(), DATABASE_SECRET);
    }
    
    void loop()
    {

The problem is here I might be using it in a wrong way:

      if (millis() - dataMillis > 1000 && Firebase.ready() && "ON"){
            dataMillis = millis();
            String path = "/users/";
            path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
            path += "/pump";
            Serial.println("Led Turned ON");                                                        
            digitalWrite(led, HIGH);       
            Serial.printf("Set int... %sn", Firebase.RTDB.setString(&fbdo, path.c_str(), "ON") ? "ok" : fbdo.errorReason().c_str());
            
        }
        else if(millis() - dataMillis > 1000 && Firebase.ready() &&"OFF"){
            dataMillis = millis();
            String path = "/users/";
            path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
            path += "/pump";
            Serial.println("Led Turned OFF");                                                        
            digitalWrite(led, LOW);       
            Serial.printf("Set int... %sn", Firebase.RTDB.setString(&fbdo, path.c_str(), "OFF") ? "ok" : fbdo.errorReason().c_str());
        } else 
      {
        Serial.println("Command Error! Please send ON/OFF");
      }
        delay(100);
    }

Source: Windows Questions C++

LEAVE A COMMENT