#include <Arduino.h>
#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 "CAN COFFEE"
#define WIFI_PASSWORD "Icandoit"
// For the following credentials, see examples/Authentications/SignInAsUser/EmailPassword/EmailPassword.ino
/* 2. Define the API Key */
#define API_KEY "AIzaSyBZ93uN2Vgq_HF5VuPymVQxeH4rr-AOvR8"
/* 3. Define the RTDB URL */
#define DATABASE_URL "https://doannhung-ecadc-default-rtdb.asia-southeast1.firebasedatabase.app" //<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 "ahihikk1"
// Define Firebase Data object
FirebaseData fbdo;
int a=0;
FirebaseAuth auth;
FirebaseConfig config;
int value;
int count=500;
int b=50;
unsigned long sendDataPrevMillis = 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
Firebase.reconnectWiFi(true);
// required for large file data, increase Rx size as needed.
Firebase.begin(&config,&auth);
Firebase.setDoubleDigits(5);
}
void loop()
{
if (Firebase.ready() && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0))
{
sendDataPrevMillis = millis();
Firebase.setInt(fbdo, F("/do-am/1"), 123);
Firebase.setInt(fbdo, F("/nhiet-do/1"), 321);
}
Firebase.getInt(fbdo,F("/value"));
value = fbdo.intData();
//Serial.printf("nhietdo %d %d",value,a);
if (value == 1 && a == 0)
{
digitalWrite(L1, HIGH);
Serial.printf("value: %d",value);
a=1;
}
else if (value == 0 && a == 1){
digitalWrite(L1, LOW);
Serial.printf("value %d",value);
a=0;
}
delay(200);
}