RTDB 100th "set" fails with ESP32 [Resolved] #64
Replies: 4 comments 8 replies
-
That is a ESP32 If you still want to use ESP32 |
Beta Was this translation helpful? Give feedback.
-
Thanks for Info! |
Beta Was this translation helpful? Give feedback.
-
You should know the queue concept in this library in order to use async functions. The An async client contains only one FIFO queue because we have only one SSL client to handle our tasks. The You don't have to restart your device because of network problems. The WiFi was automatically reconnected in this library and it has option to disable to allow user or third-party library to handle WiFi reconnection. If you often get the error |
Beta Was this translation helpful? Give feedback.
-
This is the example that uses 3 async clients for Stream and 3 async clients for set operation. The data was sent to server every 3 to 5 seconds and event was received ups on the data sent. This can run for days and months under all network conditions without problems. This runs fine on ESP32 Core v3.0.2.
#include <Arduino.h>
#include <WiFi.h>
#include <FirebaseClient.h>
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"
#define API_KEY "Web_API_KEY"
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"
#define DATABASE_URL "URL"
void asyncCB(AsyncResult &aResult);
void printResult(AsyncResult &aResult);
DefaultNetwork network;
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD);
FirebaseApp app;
WiFiClient basic_client1, basic_client2, basic_client3, basic_client4, basic_client5, basic_client6;
ESP_SSLClient ssl_client1, ssl_client2, ssl_client3, ssl_client4, ssl_client5, ssl_client6;
using AsyncClient = AsyncClientClass;
AsyncClient aClient1(ssl_client1, getNetwork(network)), aClient2(ssl_client2, getNetwork(network)), aClient3(ssl_client3, getNetwork(network)), aClient4(ssl_client4, getNetwork(network)), aClient5(ssl_client5, getNetwork(network)), aClient6(ssl_client6, getNetwork(network));
RealtimeDatabase Database;
unsigned long ms1 = 0, ms2 = 0;
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();
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...");
ssl_client1.setClient(&basic_client1);
ssl_client2.setClient(&basic_client2);
ssl_client3.setClient(&basic_client3);
ssl_client4.setClient(&basic_client4);
ssl_client5.setClient(&basic_client5);
ssl_client6.setClient(&basic_client6);
ssl_client1.setInsecure();
ssl_client2.setInsecure();
ssl_client3.setInsecure();
ssl_client4.setInsecure();
ssl_client5.setInsecure();
ssl_client6.setInsecure();
ssl_client1.setBufferSizes(2048, 1024);
ssl_client2.setBufferSizes(2048, 1024);
ssl_client3.setBufferSizes(2048, 1024);
ssl_client4.setBufferSizes(2048, 1024);
ssl_client5.setBufferSizes(2048, 1024);
ssl_client6.setBufferSizes(2048, 1024);
ssl_client1.setDebugLevel(1);
ssl_client2.setDebugLevel(1);
ssl_client3.setDebugLevel(1);
ssl_client4.setDebugLevel(1);
ssl_client5.setDebugLevel(1);
ssl_client6.setDebugLevel(1);
ssl_client1.setSessionTimeout(150);
ssl_client2.setSessionTimeout(150);
ssl_client3.setSessionTimeout(150);
ssl_client4.setSessionTimeout(150);
ssl_client5.setSessionTimeout(150);
ssl_client6.setSessionTimeout(150);
initializeApp(aClient6, app, getAuth(user_auth), asyncCB, "authTask");
app.getApp<RealtimeDatabase>(Database);
Database.url(DATABASE_URL);
Database.setSSEFilters("get,put,patch,keep-alive,cancel,auth_revoked");
Database.get(aClient1, "/test/stream/path1", asyncCB, true /* SSE */, "streamTask1");
Database.get(aClient2, "/test/stream/path2", asyncCB, true /* SSE */, "streamTask2");
Database.get(aClient3, "/test/stream/path3", asyncCB, true /* SSE */, "streamTask3");
}
void loop()
{
app.loop();
Database.loop();
if (millis() - ms1 > 3000 && app.ready())
{
ms1 = millis();
JsonWriter writer;
Database.set<int>(aClient4, "/test/stream/path1/number", random(100, 199), asyncCB, "setTask1");
Database.set<int>(aClient5, "/test/stream/path2/number", random(200, 299), asyncCB, "setTask2");
Database.set<int>(aClient6, "/test/stream/path2/number", random(300, 399), asyncCB, "setTask3");
}
if (millis() - ms2 > 5000 && app.ready())
{
ms2 = millis();
Database.set<int>(aClient4, "/test/stream/path1/number", random(400, 499), asyncCB, "setTask4");
Database.set<int>(aClient5, "/test/stream/path3/number", random(500, 599), asyncCB, "setTask5");
Database.set<int>(aClient6, "/test/stream/path3/number", random(600, 699), asyncCB, "setTask6");
}
}
void asyncCB(AsyncResult &aResult)
{
printResult(aResult);
}
void printResult(AsyncResult &aResult)
{
if (aResult.isEvent())
{
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.appEvent().message().c_str(), aResult.appEvent().code());
}
if (aResult.isDebug())
{
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
}
if (aResult.isError())
{
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
}
if (aResult.available())
{
RealtimeDatabaseResult &RTDB = aResult.to<RealtimeDatabaseResult>();
if (RTDB.isStream())
{
Serial.println("----------------------------");
Firebase.printf("task: %s\n", aResult.uid().c_str());
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());
}
else
{
Serial.println("----------------------------");
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
}
Firebase.printf("Free Heap: %d\n", ESP.getFreeHeap());
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi,
Any idea what's wrong when 100th set to RTDB fails.
Some info & observations:
Always 100th.
In case of async "set", sw stops receiving callbacks after 99 successful "set"s.
Tested with different timings etc.
Seems like TCP gets broken (SSL error seen with WiFiClientSecure. With ESP_SSLClient: TCP receive timeout).
Tested with sync & async.
Tested with TCP keepalive.
Reconnect happens after error when using ESP_SSLClient -> works again for next 99 "sets"s.
Code based on FirebaseClient examples.
HW: ESP32.
Maybe bad quess .. but can it be so that RTDB sees each "set" as new "connection" and closes one because of 100 connection limit?
Error log sample (With ESP_SSLClient):
"
Debug msg: Terminating the server connection...
Error msg: TCP receive time out, code: -3
....
Debug msg: Terminating the server connection... >> Connecting to server...
"
Code sample :
....
static int iCBWaiting = 0;
void loop()
{
app.loop();
Database.loop();
static unsigned long last = 0;
if(millis() - last < 2000)
return;
last = millis();
if(!app.ready()) {
Serial.println("***** !app.ready() *****");
return;
}
if(iCBWaiting != 0) {
Serial.print("***** iCBWaiting:");
Serial.println(iCBWaiting);;
return;
}
Database.set(aClient, "/test/int", a, asyncCB);
iCBWaiting ++;
}
void asyncCB(AsyncResult &aResult)
{
if(aCBWaiting > 0)
aCBWaiting--;
...
Beta Was this translation helpful? Give feedback.
All reactions