#include <WiFi.h>
#include <FirebaseESP32.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 "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// For the following credentials, see examples/Authentications/SignInAsUser/EmailPassword/EmailPassword.ino
/* 2. Define the API Key */
#define API_KEY "AIzaSyD5M_IMulXH-0-fPkSSOpJGVV3qGyecUWc"
/* 3. Define the RTDB URL */
#define DATABASE_URL "smart-bag-1c8c4-default-rtdb.firebaseio.com" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
/* 4. Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "123456"
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
unsigned long count = 0;
unsigned int num = 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(".");
delay(300);
}
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 (required) */
config.database_url = DATABASE_URL;
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
// Or use legacy authenticate method
// config.database_url = DATABASE_URL;
// config.signer.tokens.legacy_token = "<database secret>";
// To connect without auth in Test Mode, see Authentications/TestMode/TestMode.ino
//////////////////////////////////////////////////////////////////////////////////////////////
// Please make sure the device free Heap is not lower than 80 k for ESP32 and 10 k for ESP8266,
// otherwise the SSL connection will fail.
//////////////////////////////////////////////////////////////////////////////////////////////
Firebase.begin(&config, &auth);
// Comment or pass false value when WiFi reconnection will control by your code or third party library
Firebase.reconnectWiFi(true);
Firebase.setDoubleDigits(5);
/** Timeout options.
//WiFi reconnect timeout (interval) in ms (10 sec - 5 min) when WiFi disconnected.
config.timeout.wifiReconnect = 10 * 1000;
//Socket connection and SSL handshake timeout in ms (1 sec - 1 min).
config.timeout.socketConnection = 10 * 1000;
//Server response read timeout in ms (1 sec - 1 min).
config.timeout.serverResponse = 10 * 1000;
//RTDB Stream keep-alive timeout in ms (20 sec - 2 min) when no server's keep-alive event data received.
config.timeout.rtdbKeepAlive = 45 * 1000;
//RTDB Stream reconnect timeout (interval) in ms (1 sec - 1 min) when RTDB Stream closed and want to resume.
config.timeout.rtdbStreamReconnect = 1 * 1000;
//RTDB Stream error notification timeout (interval) in ms (3 sec - 30 sec). It determines how often the readStream
//will return false (error) when it called repeatedly in loop.
config.timeout.rtdbStreamError = 3 * 1000;
Note:
The function that starting the new TCP session i.e. first time server connection or previous session was closed, the function won't exit until the
time of config.timeout.socketConnection.
You can also set the TCP data sending retry with
config.tcp_data_sending_retry = 1;
*/
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop()
{
if (Firebase.ready())
{
// sendDataPrevMillis = millis();
// Serial.printf("Lights on: %s\n", Firebase.getInt(fbdo, F("/contacts/-NJV6JGdV5OOfD1lp2yv/lights")) ? String(fbdo.to<int>()).c_str() : fbdo.errorReason().c_str());
// FirebaseJson json;
if ((Firebase.getInt(fbdo, F("/contacts/-NJV6JGdV5OOfD1lp2yv/lights")))) {
if (fbdo.dataType() == "int") {
num = fbdo.intData();
}
}
(num >= 1 ? digitalWrite(2, HIGH) : digitalWrite(2, LOW));
(num >= 2 ? digitalWrite(4, HIGH) : digitalWrite(4, LOW));
(num >= 3 ? digitalWrite(5, HIGH) : digitalWrite(5, LOW));
}
}