Fixed esp_now_init(), added message queue to not block the callback function
This commit is contained in:
@@ -6,18 +6,29 @@ constexpr const char * const TAG = "BOBBY_ESP_NOW";
|
|||||||
#include <espnowwrapper.h>
|
#include <espnowwrapper.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
|
|
||||||
|
#include "globals.h"
|
||||||
|
|
||||||
namespace espnow {
|
namespace espnow {
|
||||||
|
|
||||||
|
std::deque<esp_now_message_t> message_queue{};
|
||||||
|
|
||||||
void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len)
|
void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len)
|
||||||
{
|
{
|
||||||
|
ESP_LOGI(TAG, "Received data");
|
||||||
const std::string data_str(data, data+data_len);
|
const std::string data_str(data, data+data_len);
|
||||||
|
|
||||||
size_t sep_pos = data_str.find(":");
|
size_t sep_pos = data_str.find(":");
|
||||||
if (std::string::npos != sep_pos)
|
if (std::string::npos != sep_pos)
|
||||||
{
|
{
|
||||||
std::string msg_type = data_str.substr(0, sep_pos);
|
std::string msg_type = data_str.substr(0, sep_pos);
|
||||||
std::string msg = data_str.substr(sep_pos, data_str.length()-1);
|
std::string msg = data_str.substr(sep_pos+1, data_str.length()-3); // - 3 may needs to be converted to sep_pos+1
|
||||||
ESP_LOGI(TAG, "Type: %s - Message: %s", msg_type.c_str(), msg.c_str());
|
ESP_LOGI(TAG, "Type: %s - Message: %s", msg_type.c_str(), msg.c_str());
|
||||||
|
|
||||||
|
const esp_now_message_t message{
|
||||||
|
.content = msg,
|
||||||
|
.type = msg_type
|
||||||
|
};
|
||||||
|
message_queue.push_back(message);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -27,9 +38,47 @@ void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len)
|
|||||||
|
|
||||||
void initESPNow()
|
void initESPNow()
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Initializing esp now...");
|
ESP_LOGI(TAG, "Initializing esp-now...");
|
||||||
ESPNow.reg_recv_cb(onReceive);
|
|
||||||
ESPNow.init();
|
if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "cannot execute esp_now_init(): tcp stack is down.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (const auto error = esp_now_init(); error != ESP_OK)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "esp_now_init() failed with %s", esp_err_to_name(error));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (const auto error = esp_now_register_recv_cb(onReceive); error != ESP_OK)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "esp_now_register_recv_cb() failed with %s", esp_err_to_name(error));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle()
|
||||||
|
{
|
||||||
|
if(message_queue.size())
|
||||||
|
{
|
||||||
|
for (const esp_now_message_t &msg : message_queue)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "queue has processed message of type '%s' with content '%s'", msg.type.c_str(), msg.content.c_str());
|
||||||
|
message_queue.pop_front();
|
||||||
|
|
||||||
|
if (msg.type == "T")
|
||||||
|
{
|
||||||
|
// convert msg.content into uint64_t
|
||||||
|
// onRecvTs(converted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const esp_now_message_t &msg : message_queue)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "%s", msg.content.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onRecvTs(uint64_t millis)
|
void onRecvTs(uint64_t millis)
|
||||||
|
@@ -1,11 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#ifdef FEATURE_ESPNOW
|
#ifdef FEATURE_ESPNOW
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <deque>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace espnow {
|
namespace espnow {
|
||||||
constexpr const uint8_t broadcast_address[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
constexpr const uint8_t broadcast_address[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||||
|
struct esp_now_message_t {
|
||||||
|
const std::string content;
|
||||||
|
const std::string type;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern std::deque<esp_now_message_t> message_queue;
|
||||||
|
|
||||||
void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len);
|
void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len);
|
||||||
void initESPNow();
|
void initESPNow();
|
||||||
|
void handle();
|
||||||
void onRecvTs(uint64_t millis);
|
void onRecvTs(uint64_t millis);
|
||||||
} // namespace espnow
|
} // namespace espnow
|
||||||
#endif
|
#endif
|
||||||
|
@@ -412,6 +412,10 @@ extern "C" void app_main()
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef FEATURE_ESPNOW
|
||||||
|
espnow::handle();
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef FEATURE_CLOUD
|
#ifdef FEATURE_CLOUD
|
||||||
if (!lastCloudCollect || now - *lastCloudCollect >= std::chrono::milliseconds{settings.boardcomputerHardware.timersSettings.cloudCollectRate})
|
if (!lastCloudCollect || now - *lastCloudCollect >= std::chrono::milliseconds{settings.boardcomputerHardware.timersSettings.cloudCollectRate})
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user