Replies: 8 comments
-
You should set the debug option in the IDE to show the information from WiFi and WiFiClientSecure. You should avoid running the Firebase code inside the callback because it uses stack which may cause the WiFiClientSecure fails to connect because of memory allocation failure. You can run it in another FreeRTOS task or put in the function that calling from the main loop by checking some status flag variable. You should leave the code in the callback to run as fast as possible with small memory usage. The network task should be avoided to run inside it. FYI. |
Beta Was this translation helpful? Give feedback.
-
The serial printing shows that esp now is not functioning as the |
Beta Was this translation helpful? Give feedback.
-
ok, I am following your advices and I have this code now, I have de 2.0.5 version because de arduino ide doesn't have the 2.1.0 version. // receiver
#include <esp_now.h>
#include <WiFi.h>
#define ENABLE_USER_CONFIG
#define ENABLE_USER_AUTH
#define ENABLE_DATABASE
#include <FirebaseClient.h>
#include "ExampleFunctions.h"
#define WIFI_SSID "xxxxxx"
#define WIFI_PASSWORD "xxxxxx"
#define API_KEY "xxxxxxxx"
#define DATABASE_URL "xxxxxxxx"
#define USER_EMAIL "xxxxxxxx"
#define USER_PASSWORD "xxxxxxxx"
void processData(AsyncResult &aResult);
SSL_CLIENT ssl_client;
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client);
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD, 3600 /* expire period in seconds (<3600) */);
FirebaseApp app;
RealtimeDatabase Database;
struct struct_message {
uint8_t senderId;
float temperature;
float humidity;
};
struct_message incomingData;
bool newDataFlag = false;
struct_message dataToProcess;
void show_status(bool status)
{
if (status)
Serial.println("Success");
else
Firebase.printf("Error, msg: %s, code: %d\n", aClient.lastError().message().c_str(), aClient.lastError().code());
}
void OnMessageReceived(const esp_now_recv_info_t* recv_info, const uint8_t* data, int len) {
memcpy(&incomingData, data, sizeof(incomingData));
Serial.println("Data received:");
Serial.printf("Sensor ID: %d, Temp: %.2f, Hum: %.2f\n", incomingData.senderId, incomingData.temperature, incomingData.humidity);
dataToProcess = incomingData;
newDataFlag = true;
}
void RegisterPeeks() {
const uint8_t MAC_SENDER_1[] = {0x08, 0xa6, 0xf7, 0xb0, 0xc5, 0x70}; // 08:a6:f7:b0:c5:70
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, MAC_SENDER_1, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
peerInfo.ifidx = WIFI_IF_STA;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
}
}
void initEspNow() {
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnMessageReceived);
RegisterPeeks();
}
void wifiConnect() {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void firebaseConfig() {
set_ssl_client_insecure_and_buffer(ssl_client);
//initializeApp(aClient, app, getAuth(user_auth), auth_debug_print, "🔐 authTask");
initializeApp(aClient, app, getAuth(user_auth), 120 * 1000, auth_debug_print);
app.getApp<RealtimeDatabase>(Database);
Database.url(DATABASE_URL);
}
void setup() {
Serial.begin(115200);
delay(2000);
wifiConnect();
firebaseConfig();
initEspNow();
}
void loop() {
app.loop();
if (app.ready() && newDataFlag) {
newDataFlag = false;
String path = "/datos";
bool status;
if (dataToProcess.senderId == 1) {
status = Database.set<float>(aClient, path + "/t", dataToProcess.temperature);
show_status(status);
status = Database.set<float>(aClient, path + "/h", dataToProcess.humidity);
show_status(status);
} else {
status = Database.set<float>(aClient, path + "/h2", dataToProcess.humidity);
show_status(status);
}
}
delay(600);
}
void processData(AsyncResult &aResult)
{
// Exits when no result available when calling from the loop.
if (!aResult.isResult())
return;
if (aResult.isEvent())
{
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.eventLog().message().c_str(), aResult.eventLog().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())
{
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
}
} but the serial monitor continues wait and not continue the execution.
|
Beta Was this translation helpful? Give feedback.
-
The problem is esp now and nothing to do with Firebase library. If everything is ok at least you should see the info that printed from this. Serial.println("Data received:");
Serial.printf("Sensor ID: %d, Temp: %.2f, Hum: %.2f\n", incomingData.senderId, incomingData.temperature, incomingData.humidity);
|
Beta Was this translation helpful? Give feedback.
-
Your code is stuck on |
Beta Was this translation helpful? Give feedback.
-
You should print something before exiting from |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Thanks a lot, bro. You're right. The problem was the channel. It should be the same for Wi-Fi and ESP-Now. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I have the next code but when compile de code the serial monitor print the next, and I don't know why it waits.
It just stays there and doesn't print or send anything.
Can someone please help me?
Beta Was this translation helpful? Give feedback.
All reactions