Multi Query #27
-
Hi friends, I need help with new library. What is correct way to do this 3 jobs? Regards! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
In case of crash, library does not cause the crash based on your information. You can call push, set, get, patch functions as you want without problems because there is no chance of crash as all tasks will be stored in FIFO queue. The possible crash is because dangling pointer problem and you should read the Readme carefully for using this library correctly otherwise you should use sync functions instead. In case of using many async clients, you have to check your device free heap to avoid out of memory situation as the |
Beta Was this translation helpful? Give feedback.
-
You also have to read the Readme for choosing the proper memory option for ESP8266. |
Beta Was this translation helpful? Give feedback.
-
How many clients should I use in my case? One for stream, second for get, set, push, etc. or many for each operation. I've read documentation and examples but it's not very clear when you have several different requests how to execute them |
Beta Was this translation helpful? Give feedback.
-
Make new project with include onli FirebaseClient and WiFiManager last version. Here is the code. When I change "/last" node, stream print to console "type, data, event and path", after that Get "/garage/relay/relayType" and print to console, if relayType is 1, it's ok, but if 2 - ESP crash when try to push to "/log/open". Where I'm wrong? Here is my code: #include <WiFiManager.h>
#include <FirebaseClient.h>
#define API_KEY "####################"
#define USER_EMAIL "##@##.com"
#define USER_PASSWORD "######"
#define DATABASE_URL "https://#######################"
void asyncCB(AsyncResult &aResult);
void printResult(AsyncResult &aResult);
DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD);
FirebaseApp app;
#if defined(ESP32) || defined(ESP8266) || defined(PICO_RP2040)
#include <WiFiClientSecure.h>
WiFiClientSecure ssl_client;
#elif defined(ARDUINO_ARCH_SAMD)
#include <WiFiSSLClient.h>
WiFiSSLClient ssl_client;
#endif
// In case the keyword AsyncClient using in this example was ambigous and used by other library, you can change
// it with other name with keyword "using" or use the class name AsyncClientClass directly.
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client, getNetwork(network));
#if defined(ESP32) || defined(ESP8266) || defined(PICO_RP2040)
WiFiClientSecure ssl_client2;
#elif __has_include(<WiFiSSLClient.h>)
WiFiSSLClient ssl_client2;
#endif
AsyncClient aClient2(ssl_client2, getNetwork(network));
RealtimeDatabase Database;
AsyncResult aResult_no_callback;
unsigned long ms = 0;
void setup()
{
Serial.begin(115200);
WiFiManager wm;
bool res;
res = wm.autoConnect("AutoConnectAP","password");
if(!res) {
Serial.println("Failed to connect");
// ESP.restart();
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
// 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();
Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
Serial.println("Initializing app...");
#if defined(ESP32) || defined(ESP8266) || defined(PICO_RP2040)
ssl_client.setInsecure();
ssl_client2.setInsecure();
#if defined(ESP8266)
ssl_client.setBufferSizes(4096, 1024);
ssl_client2.setBufferSizes(4096, 1024);
#endif
#endif
app.setCallback(asyncCB);
initializeApp(aClient2, app, getAuth(user_auth));
// Waits for app to be authenticated.
// For asynchronous operation, this blocking wait can be ignored by calling app.loop() in loop().
ms = millis();
while (app.isInitialized() && !app.ready() && millis() - ms < 120 * 1000);
app.getApp<RealtimeDatabase>(Database);
Database.url(DATABASE_URL);
Database.get(aClient, "/garage", asyncCB, true /* SSE mode */);
}
void loop()
{
app.loop();
Database.loop();
}
void asyncCB(AsyncResult &aResult)
{
printResult(aResult);
}
void printResult(AsyncResult &aResult)
{
if (aResult.appEvent().code() > 0)
{
Firebase.printf("Event msg: %s, code: %d\n", aResult.appEvent().message().c_str(), aResult.appEvent().code());
}
if (aResult.isDebug())
{
Firebase.printf("Debug msg: %s\n", aResult.debug().c_str());
}
if (aResult.isError())
{
Firebase.printf("Error msg: %s, code: %d\n", aResult.error().message().c_str(), aResult.error().code());
}
if (aResult.available())
{
RealtimeDatabaseResult &RTDB = aResult.to<RealtimeDatabaseResult>();
if (RTDB.isStream())
{
Firebase.printf("event: %s\n", RTDB.event().c_str());
Firebase.printf("path: %s\n", RTDB.dataPath().c_str());
Firebase.printf("data: %s\n", RTDB.to<const char *>());
Firebase.printf("type: %d\n", RTDB.type());
if (strcmp(RTDB.dataPath().c_str(), "/last") == 0) {
Database.get(aClient2, "/garage/relay/relayType", asyncCB);
}
}
else
{
Firebase.printf("payload: %s\n", aResult.c_str());
int v2 = RTDB.to<int>();
if (v2 == 1) {
Serial.println(v2);
} else if (v2 == 2) {
Serial.println(v2);
object_t ts_json;
JsonWriter writer;
writer.create(ts_json, ".sv", "timestamp"); /
Database.push<object_t>(aClient2, "/log/open", ts_json, asyncCB);
}
}
}
}
|
Beta Was this translation helpful? Give feedback.
It is reasonable for you to understand like that If you don't have much experienced in ESP8266 with
WiFiClientSecure
andHTTPClient
library programming.As you 've seen from documentation, it wrote that about 1k of memory consumed for each async/sync task stored in a async client's queue that is quite low amount of memory.
Your device may lose only 3k for running 3 async/sync tasks at a time which this excludes the server response payload that can be large or small memory to be additionally consumed.
And when it comes into the real work that external SSL Client (
WiFiClientSecure
) was used as a client for async client, the usage memory is mostly dependent of the SSL Client usage as it requ…