Can somebody Help me please with this if Else Statements issue?

I'm trying to set LED light data in Firebase RTDB through the userID, it works but the light keeps ON, it wont turn OFF when i change it to OFF!?
I have used Strings values and int values but still the same result!?
Looked at the examples from the Firebase_client package and now added this line of code: fireStatus = Firebase.RTDB.getInt("pump");

After all it returned this error: no matching function for call to 'FB_RTDB::getInt(const char [5])'

Here is my code:

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif

#include <Firebase_ESP_Client.h>
#include <addons/TokenHelper.h>
#include <addons/RTDBHelper.h>

#define WIFI_SSID "Abdallah Agha"
#define WIFI_PASSWORD "abdallah123"
#define API_KEY "AIza****************************"
#define USER_EMAIL "a1@a.com"
#define USER_PASSWORD "123456"
#define DATABASE_URL "smar*****************************" 
#define DATABASE_SECRET "jLPap1***********************************"

FirebaseData fbdo;

FirebaseAuth auth;

FirebaseConfig config;

int fireStatus = 0;
                                                   


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(D1, OUTPUT);
    delay(100);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();

Serial.printf("Firebase Client v%s\n\n", 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;

Firebase.begin(&config, &auth);

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()
{
fireStatus = Firebase.RTDB.getInt("pump");
if(fireStatus == 1, Firebase.ready()){
String path = "/users/";
path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
path += "/pump";
Serial.printf("Led Turned ON", Firebase.RTDB.setInt(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());
digitalWrite(D1, HIGH);
}
else if(fireStatus == 0, Firebase.ready()){
String path = "/users/";
path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
path += "/pump";
Serial.printf("Led Turned OFF", Firebase.RTDB.setInt(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());
digitalWrite(D1, LOW);
}
else{
Serial.println("Command Error! Please send ON/OFF");
}

delay(100);

}

Not sure what you're trying to do here, but I doubt that it's doing what you expect:

  if (fireStatus == 1, Firebase.ready())

@wildbill types faster making the same point.

Pretty sure you don’t want to use the comma operator here…

a7

userID according to the documentation is saved in Firebase.ready() but it seems like in the new version if you used it or not it dosen't matter it will still set data with userID.
So what i have done now in the code i removed the Firebase.ready(), but unfortunately, the same issue stills appear but in backwards when i change from 0 to 1 it keeps changing to 0 after 1 second, here is the code:

void loop()
{

if(fireStatus == 1){
String path = "/users/";
path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Email/Password
path += "/pump";
Serial.printf("Led Turned ON", Firebase.RTDB.setInt(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());
digitalWrite(D1, HIGH);
}

else if (fireStatus == 0){
String path = "/users/";
path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Email/Password
path += "/pump";
Serial.printf("Led Turned OFF", Firebase.RTDB.setInt(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());
digitalWrite(D1, LOW);
}

delay(100);

}

Hello,
I have removed the comma but the same issue stills appear, so for the Firebase.ready() it seems like in the new version if it has been used or not it's okay it will still set data with userID.
Overall i removed it but unfortunately the same problem appeared but now in backwards the 0 value keeps running but when i change to 1 it will get back to 0.

So in firebase it appears, when i change the 0 to 1 in firebase it will get back to 0.

Here is the code:
void loop()
{

if(fireStatus == 1){
String path = "/users/";
path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Email/Password
path += "/pump";
Serial.printf("Led Turned ON", Firebase.RTDB.setInt(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());
digitalWrite(D1, HIGH);
}

else if (fireStatus == 0){
String path = "/users/";
path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Email/Password
path += "/pump";
Serial.printf("Led Turned OFF", Firebase.RTDB.setInt(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());
digitalWrite(D1, LOW);
}

delay(100);

}

Can you post your code again - all of it.

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif

#include <Firebase_ESP_Client.h>
#include <addons/TokenHelper.h>
#include <addons/RTDBHelper.h>

#define WIFI_SSID "Abdallah Agha"
#define WIFI_PASSWORD "abdallah123"
#define API_KEY "AIza****************************"
#define USER_EMAIL "a1@a.com"
#define USER_PASSWORD "123456"
#define DATABASE_URL "smar*****************************" 
#define DATABASE_SECRET "jLPap1***********************************"

FirebaseData fbdo;

FirebaseAuth auth;

FirebaseConfig config;

int fireStatus = 0;


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(D1, OUTPUT);
    delay(100);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();

Serial.printf("Firebase Client v%s\n\n", 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;

Firebase.begin(&config, &auth);

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()
{

 if(fireStatus == 1){
        String path = "/users/";
        path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Email/Password
        path += "/pump";
        Serial.printf("Led Turned ON", Firebase.RTDB.setInt(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());                                                        
        digitalWrite(D1, HIGH);      
      }
      
  else if (fireStatus == 0){
        String path = "/users/";
        path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Email/Password
        path += "/pump";
        Serial.printf("Led Turned OFF", Firebase.RTDB.setInt(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());                                                        
        digitalWrite(D1, LOW);
        }
            
    delay(100);

}

It looks like you threw away the line that sets fireStatus.

Can you please direct me where exactly?

In your first post, the first line of loop reads fireStatus from the database. Now that's gone, but you're still checking it with your if statements.

Unfortunately it throwed this error:
no matching function for call to 'FB_RTDB::getInt(const char [5])'

changed it to this
fireStatus = Firebase.RTDB.getInt(&fbdo, "/users/pump");
it worked

But also it returns the same issue :frowning: sorry guys for headache you but it's been around 48 hours for me trying to figure out this issue but no use! anyway i'm not giving up, hope someone could help and i will keep searching.

I don't understand what you're trying to do. You read the status from firebase and light the LED accordingly. Why do you then set the status to the same thing you just read?

What i'm trying to do is allowing the user to turn the LED light ON and OFF from firebase through out userID.

I assumed as much. Why do it from the code too? All you need do is read the status and set the led.

That's it.

1 Like

@wildbill
Actually after a deep analysis of the code you were right I can do what you referred to, which is turn the light ON & OFF from firebase Rtdb & what is really happening its not getting data from user uid through this:
firebaseStatus = Firebase. RTDB.get(&fbdo, path.c_str());
The problem is in: path.c_str();
path.c_str() - - > refers to - - >
String path = "/users";
path += auth.token.uid.c_str();
path += "/pump" ;
Is there any other way to get data through user uid than this one!

Post your code, in code tags please.

So it's working, but you have an issue with user identification - is that it?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.