#include <WiFi.h>
#include <FirebaseESP32.h>
#include <addons/TokenHelper.h>
//WIFI configuration
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
/* 2.Define the API key and database url */
#define API_KEY "AIzaSyB0bbOygm2jr-U3d8YaywEUhOvLqH-YpIw"
#define DATABASE_URL "https://ledcontrol-60266-default-rtdb.asia-southeast1.firebasedatabase.app/"
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "123456"
//define firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
void setup() {
// put your setup code here, to run once:
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();
/*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;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
pinMode(14, OUTPUT);
}
String read_data = "";
void loop() {
if(Firebase.getString(fbdo,"/status")){
if(fbdo.dataType()=="string"){
read_data = fbdo.stringData();
Serial.print("Data receieved: ");
Serial.println(read_data);
}
}
if(read_data == "ON"){
digitalWrite(14, HIGH);
}
else{
digitalWrite(14, LOW);
}
}
//FirebaseDatabase database = FirebaseDatabase.getInstance();
//DatabaseReference myRef = database.getReference("status");