Need help updating firmware via Firebase storage #597
-
Hello mobizt!
I used this example to update the firmware, and this example to use streamcallback. My setup configuration is: config.api_key = API_KEY;
config.database_url = DATABASE_URL;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.token_status_callback = tokenStatusCallback;
Firebase.reconnectNetwork(true);
fbdo.setBSSLBufferSize(4096, 1024);
stream.setBSSLBufferSize(2048, 1024);
config.fcs.download_buffer_size = 2048;
Firebase.begin(&config, &auth);
if (!Firebase.RTDB.beginStream(&stream, DEVICE_PART_COMMAND))
Serial.printf("sream begin error, %s\n\n", stream.errorReason().c_str());
Firebase.RTDB.setStreamCallback(&stream, streamCallback, streamTimeoutCallback); This is the function that performs downloadOTA. void showRam()
{
HeapSelectIram ephemeral;
Serial.printf("IRAM free: %6d bytes\n", ESP.getFreeHeap());
{
HeapSelectDram ephemeral;
Serial.printf("DRAM free: %6d bytes\n", ESP.getFreeHeap());
}
}
void loopOTA()
{
if (Firebase.ready() && !taskCompleted)
{
Serial.println("Befor clear");
showRam();
stream.pauseFirebase(true);
stream.clear();
Serial.println("After clear");
showRam();
taskCompleted = true;
Serial.println("\nDownload firmware file...\n");
if (!Firebase.Storage.downloadOTA(&fbdo, STORAGE_BUCKET_ID, "firmware.bin", fcsDownloadCallback))
Serial.println(fbdo.errorReason());
}
} In case of success, when I initialize
An error occurred when I changed the variable
Please help me see where I went wrong in trying to combine these two examples into one. Thank! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
As I previously replied
You have no choices in this limited memory device (ESP8266), you have to completely stop using stream by closing the session to release the memory before using OTA download. You should call https://github.com/mobizt/Firebase-ESP-Client/tree/main/src#remove-stream-callback-functions |
Beta Was this translation helpful? Give feedback.
-
I followed your instructions but my Esp8266 still reboots. I then tried reconfiguring it showRam();
Serial.println("Befor clear");
stream.pauseFirebase(true);
stream.clear();
Firebase.RTDB.removeStreamCallback(&stream);
Firebase.reset(&config);
Firebase.begin(&config, &auth);
Serial.println("After clear");
showRam(); I printed out the RAM after freeing the stream and had ~24k of DRAM available for downloadOTA
And this is the RAM after I reconfigured it. After
|
Beta Was this translation helpful? Give feedback.
-
The error is because dangling pointer in your code due to you do not properly guard the usage of Here is the simple working code. #include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Firebase_ESP_Client.h>
#include <addons/TokenHelper.h>
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"
#define API_KEY "API_KEY"
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"
#define STORAGE_BUCKET_ID "BUCKET-NAME.appspot.com"
FirebaseData stream;
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
volatile bool dataChanged = false;
bool ota = false;
void streamCallback(FirebaseStream data)
{
Serial.printf("sream 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);
Serial.println();
Serial.printf("Received stream payload size: %d (Max. %d)\n\n", data.payloadLength(), data.maxPayloadLength());
dataChanged = true;
}
void streamTimeoutCallback(bool timeout)
{
if (timeout)
Serial.println("stream timed out, resuming...\n");
if (!stream.httpConnected())
Serial.printf("error code: %d, reason: %s\n\n", stream.httpCode(), stream.errorReason().c_str());
}
void fcsDownloadCallback(FCS_DownloadStatusInfo info)
{
if (info.status == firebase_fcs_download_status_init)
{
Serial.printf("Downloading firmware %s (%d)\n", info.remoteFileName.c_str(), info.fileSize);
}
else if (info.status == firebase_fcs_download_status_download)
{
Serial.printf("Downloaded %d%s, Elapsed time %d ms\n", (int)info.progress, "%", info.elapsedTime);
}
else if (info.status == firebase_fcs_download_status_complete)
{
Serial.println("Update firmware completed.");
Serial.println();
Serial.println("Restarting...\n\n");
delay(2000);
#if defined(ESP32) || defined(ESP8266)
ESP.restart();
#elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
rp2040.restart();
#endif
}
else if (info.status == firebase_fcs_download_status_error)
{
Serial.printf("Download firmware failed, %s\n", info.errorMsg.c_str());
}
}
void setup()
{
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
unsigned long ms = millis();
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);
config.api_key = API_KEY;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.database_url = DATABASE_URL;
config.token_status_callback = tokenStatusCallback;
Firebase.reconnectNetwork(true);
fbdo.setBSSLBufferSize(1024, 1024);
stream.setBSSLBufferSize(1024, 1024);
Firebase.begin(&config, &auth);
if (!Firebase.RTDB.beginStream(&stream, "/test/stream/data"))
Serial.printf("sream begin error, %s\n\n", stream.errorReason().c_str());
Firebase.RTDB.setStreamCallback(&stream, streamCallback, streamTimeoutCallback);
}
void loop()
{
if (Firebase.ready() && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0))
{
sendDataPrevMillis = millis();
if (ota)
return;
Serial.printf("Set int... %s\n\n", Firebase.RTDB.set(&fbdo, "/test/stream/data", random(100, 1000)) ? "ok" : fbdo.errorReason().c_str());
}
if (dataChanged)
{
dataChanged = false;
ota = true;
fbdo.clear();
stream.clear();
Firebase.RTDB.removeStreamCallback(&stream);
Serial.println("\nDownload firmware file...\n");
if (!Firebase.Storage.downloadOTA(&fbdo, STORAGE_BUCKET_ID, "esp8266.bin", fcsDownloadCallback))
Serial.println(fbdo.errorReason());
}
}
|
Beta Was this translation helpful? Give feedback.
-
Hello Mobitz, First of all, thank you and this community to creating and updated this library. I'm encountering a memory issue related to managing Firebase streams on my ESP32. Specifically, when I use pauseFirebase() without calling clear() and setStreamCallback(), there's no memory leak. However, after each call to stepperFunction with setStreamCallback(), almost 20k bytes of memory are consumed and never returned, indicating a memory leak, right? I need to pause streams to prevent the stepper motor from operating with awkward sounds due to continuous stream updates. I use ESP32's core 1 for other functions, requiring the stepper motor to run on core 0. Could you please help me identify any faults in the following code snippet?
One of my streamCallBack function looks like this:
I am checking the datachanged in the loop and if so, then call the stepperFunction. Also, I am checking the free heap in the loop.
|
Beta Was this translation helpful? Give feedback.
As I previously replied
You have no choices in this limited memory device (ESP8266), you have to completely stop using stream by closing the session to release the memory before using OTA download.
You should call
stream.clear()
and remove its callback to terminate it.https://git…