#include <WiFi.h>
#include <FirebaseESP32.h>
/* 2. Define the API Key */
#define API_KEY "AIzaSyDR3xRTb3sXP-gYFYCWJjoP7quFXMir4HQ"
/* 3. Define the RTDB URL */
#define DATABASE_URL "https://freertos-mpu6050-default-rtdb.asia-southeast1.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 "huy123"
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
const int ledPin = 5;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(115200);
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
/* 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;
// Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager
Firebase.reconnectNetwork(true);
// Since v4.4.x, BearSSL engine was used, the SSL buffer need to be set.
// Large data transmission may require larger RX buffer, otherwise connection issue or data read time out can be occurred.
fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */);
// Limit the size of response payload to be collected in FirebaseData
fbdo.setResponseSize(2048);
Firebase.begin(&config, &auth);
Firebase.setDoubleDigits(5);
config.timeout.serverResponse = 10 * 1000;
}
void loop()
{
// Firebase.ready() should be called repeatedly to handle authentication tasks.
if (Firebase.ready() && (millis() - sendDataPrevMillis > 1000 || sendDataPrevMillis == 0))
{
sendDataPrevMillis = millis();
int ledState;
if (Firebase.getInt(&fbdo, "/led/state", &ledState)) {
digitalWrite(ledPin, ledState);
} else {
Serial.println(fbdo.errorReason().c_str());
}
}
}