How to set data to Firebase.RTDB with a string in if and else statements in proper way?

Recently i’m trying to do set data in RTDB with if and else statements.
ESP8266 is set on what it should be working on V.3.0.0
All of the libraries and packages are working at 100% and there is no error at all it uploads to the board perfectly but it doesn’t set data in firebase!!!
i’m following the examples that we got on github.

here is it:

#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 "AIzaSyAu**********"

/* 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-******************************" //.firebaseio.com or ..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 "jLPap1rLyrob********************"
/* 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;

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(".");
    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()
{
if (Firebase.ready()){

if (fireStatus == "ON") 
{                                                          // compare the input of led status received from firebase
  Serial.println("Led Turned ON");                                                        
  digitalWrite(led, HIGH);  
  String path = "/users/";
  path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
  path += "/pump";
  Serial.printf("Set onAndOff... %sn", Firebase.RTDB.set(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());// make external led ON
} 
else if (fireStatus == "OFF") 
{                                                  // compare the input of led status received from firebase
  Serial.println("Led Turned OFF");
  digitalWrite(led, LOW); 
  String path = "/users/";
  path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
  path += "/pump";
  Serial.printf("Set onAndOff... %sn", Firebase.RTDB.set(&fbdo, path.c_str(), fireStatus) ? "ok" : fbdo.errorReason().c_str());// make external led OFF
}else 
{
  Serial.println("Command Error! Please send ON/OFF");
}
  delay(100);
}
}

Source: Windows Questions C++

LEAVE A COMMENT