There are so many tutorials that discuss how to connect the ESP8266, particularly using a secret key. However, it actually makes the read process between the ESP8266 and Firebase very slow (it takes more than one second to retrieve the data).
This tutorial will provide valuable information on how to establish the fastest connection between the ESP8266 and Firebase using the Web API key.
Install the Firebase client in the Arduino Library manager. This library allows programmers to use the command : #include <Firebase_ESP_Client.h>
The Web API key will not appear by default because it is not yet activated.
To activate the connection between the ESP8266 and Firebase using the Web API key, you need to configure the Firebase project as follows :
Click Authentication Menu - Sign-In method - Add new provider - Email/Password
Access menu Authentication - Users - Add user - then insert the email and password
Web API Key will appear then copy it
Now this is the source cade that can be usesd for read and update the firebase data :
#include <Arduino.h>
#include <ESP8266WiFi.h>
// Make sure you include the main library header
#include <Firebase_ESP_Client.h>
// Provide the token generation process info.
#include "addons/TokenHelper.h"
// Provide the RTDB payload printing info.
#include "addons/RTDBHelper.h"
// Define WiFi credentials
#define WIFI_SSID "Infinix"
#define WIFI_PASSWORD "alpha3"
// Define Firebase project credentials
#define WEB_API_KEY "AIzaSyCb1YXRbtjFFE5OP-1YdgDCo1zRKkxE"
#define DATABASE_URL "https://monitor-41aaf-rtdb.asia-southeast1.firebasedatabase.app/"
// Define Firebase user credentials
#define USER_EMAIL "labinside01@gmail.com"
#define USER_PASS "Roo57325yyffyyyyy29*"
// Declare the object that holds Firebase data for requests and responses
FirebaseData fbdo;
// Declare the authentication and configuration objects
FirebaseAuth auth;
FirebaseConfig config;
// Variable to store the authentication status
bool isAuthenticated = false;
// Example function to handle async data (if you use streams)
void streamCallback(FirebaseStream data)
{
Serial.printf("stream path, %s\nevent path, %s\ndata type, %s\nevent type, %s\n\n",
data.streamPath().c_str(),
data.dataPath().c_str(),
data.dataType().c_str(),
data.eventType().c_str());
printResult(data); //See addons/RTDBHelper.h
Serial.println();
}
void setup()
{
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH);
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 project credentials */
config.api_key = WEB_API_KEY;
config.database_url = DATABASE_URL;
/* Assign the user sign in credentials */
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASS;
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
// Initialize the Firebase library
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
digitalWrite(BUILTIN_LED, LOW);
}
void loop()
{
if (Firebase.ready()) {
if (!isAuthenticated) {
Serial.println("Firebase is authenticated and ready!");
isAuthenticated = true;
}
if (Firebase.RTDB.getInt(&fbdo, "/vehicle_1/alarm")) {
if (fbdo.dataType() == "int") {
int alarmStatus = fbdo.intData();
Serial.print("Alarm status is: ");
Serial.println(alarmStatus);
Firebase.RTDB.setInt(&fbdo, "/vehicle_1/alarm", 1);
}
} else {
Serial.println("Failed to get integer. Reason: " + fbdo.errorReason());
}
} else {
isAuthenticated = false;
Serial.println("Waiting for Firebase authentication...");
}
delay(1); // Wait 5 seconds before the next read
}
No comments:
Post a Comment