I am working on a project where I need an Arduino Nano ESP32 to send data to a firebase realtime database. I am using mobizt FirebaseClient library and have made sure my database rules are set so that read and write are both true. I have tried various examples in github repos and online tutorials but I am still having trouble getting them to connect. This is what I have right now, I removed the authentication steps for now as I just want to get something working.
#define ENABLE_USER_AUTH
#define ENABLE_DATABASE
#include <WiFi.h>
#include <FirebaseClient.h>
#include <WiFiClientSecure.h>
#include <time.h>
// Network and Firebase credentials
#define WIFI_SSID "network ssid"
#define WIFI_PASSWORD "password"
#define API_KEY "api key"
#define DATABASE_URL "url"
#define USER_EMAIL "email"
#define USER_PASS "password"
// This creates the UserAuth object, and the API_KEY is encapsulated within it.
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASS);
WiFiClientSecure ssl_client1;
using AsyncClient = AsyncClientClass;
AsyncClient async_client1(ssl_client1);
// Initialize Firebase objects
FirebaseApp app; // This acts as the authentication handler
RealtimeDatabase Database; // This handles Realtime Database operations
// Global variable to store the last error message from AsyncClient
String lastClientErrorMessage;
// Callback function for asynchronous operations, including initializeApp and Database.set
void processData(AsyncResult &aResult) {
if (aResult.isError()) {
Serial.printf("Error from callback (%s): %s\n", aResult.uid().c_str(), aResult.error().message().c_str());
lastClientErrorMessage = aResult.error().message(); // Store the error
} else if (aResult.isEvent()) {
Serial.printf("Event (%s): %s\n", aResult.uid().c_str(), aResult.appEvent().message().c_str());
} else if (aResult.isDebug()) {
Serial.printf("Debug (%s): %s\n", aResult.uid().c_str(), aResult.debug().c_str());
}
}
// Function to initialize NTP time synchronization
void initTime() {
Serial.println("Synchronizing time with NTP server...");
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // UTC offset 0, DST offset 0
time_t now = time(nullptr);
while (now < 1000) { // Wait for time to be set (time_t 0-999 is usually not set)
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("\nTime synchronized.");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.printf("Current UTC time: %s", asctime(&timeinfo));
}
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("\nWiFi Connected. IP Address: " + WiFi.localIP().toString());
ssl_client1.setInsecure();
ssl_client1.setHandshakeTimeout(5);
initializeApp(async_client1, app, getAuth(user_auth), processData, "AuthInit");
Database.url(DATABASE_URL);
pinMode(A4, INPUT);
}
void loop() {
app.loop();
int sensorValue = analogRead(A4);
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
Database.set<int>(async_client1, "/test/lightValue", sensorValue, processData, "SensorWrite");
Serial.println("Initiated data write to Realtime Database. Check callback for status (UID: SensorWrite).");
delay(10000);
}
The message that is printed out is
Initiated data write to Realtime Database. Check callback for status (UID: SensorWrite).
Sensor Value: 51
Error from callback (task_2259): app was not assigned
Any help would be greatly appreciated!