Added esp now garage opener
This commit is contained in:
@ -14,6 +14,7 @@
|
||||
#include "displays/menus/mainmenu.h"
|
||||
#include "globals.h"
|
||||
#include "texts.h"
|
||||
#include "espnowfunctions.h"
|
||||
|
||||
#ifdef FEATURE_GARAGE
|
||||
void GarageDisplay::start()
|
||||
@ -41,6 +42,14 @@ void GarageDisplay::redraw()
|
||||
|
||||
void GarageDisplay::confirm()
|
||||
{
|
||||
#ifdef FEATURE_ESPNOW
|
||||
if (const auto error = espnow::send_espnow_message(fmt::format("BOBBYOPEN:garage:{}", "TOKEN")); error != ESP_OK)
|
||||
{
|
||||
ESP_LOGE("BOBBY", "send_espnow_message() failed with: %s", esp_err_to_name(error));
|
||||
return;
|
||||
}
|
||||
espgui::switchScreen<MainMenu>();
|
||||
#endif
|
||||
}
|
||||
|
||||
void GarageDisplay::back()
|
||||
|
@ -3,18 +3,23 @@ constexpr const char * const TAG = "BOBBY_ESP_NOW";
|
||||
#include "espnowfunctions.h"
|
||||
|
||||
#include <string>
|
||||
#include <espnowwrapper.h>
|
||||
|
||||
#include <espchrono.h>
|
||||
#include <esp_log.h>
|
||||
#include <numberparsing.h>
|
||||
|
||||
#include "globals.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace espnow {
|
||||
|
||||
std::deque<esp_now_message_t> message_queue{};
|
||||
std::list<esp_now_peer_info_t> peers{};
|
||||
uint8_t initialized{0};
|
||||
|
||||
void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len)
|
||||
{
|
||||
ESP_LOGI(TAG, "Received data");
|
||||
ESP_LOGD(TAG, "Received data");
|
||||
const std::string data_str(data, data+data_len);
|
||||
|
||||
size_t sep_pos = data_str.find(":");
|
||||
@ -22,7 +27,7 @@ void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len)
|
||||
{
|
||||
std::string msg_type = data_str.substr(0, sep_pos);
|
||||
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_LOGD(TAG, "Type: %s - Message: %s", msg_type.c_str(), msg.c_str());
|
||||
|
||||
const esp_now_message_t message{
|
||||
.content = msg,
|
||||
@ -40,50 +45,129 @@ void initESPNow()
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing esp-now...");
|
||||
|
||||
if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled)
|
||||
if (initialized < 1)
|
||||
{
|
||||
ESP_LOGW(TAG, "cannot execute esp_now_init(): tcp stack is down.");
|
||||
return;
|
||||
if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled)
|
||||
{
|
||||
ESP_LOGW(TAG, "cannot execute esp_now_init(): tcp stack is down.");
|
||||
return;
|
||||
}
|
||||
else
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
if (const auto error = esp_now_init(); error != ESP_OK)
|
||||
if (initialized < 2)
|
||||
{
|
||||
ESP_LOGE(TAG, "esp_now_init() failed with %s", esp_err_to_name(error));
|
||||
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;
|
||||
}
|
||||
else
|
||||
initialized = 2;
|
||||
}
|
||||
|
||||
if (const auto error = esp_now_register_recv_cb(onReceive); error != ESP_OK)
|
||||
if (initialized < 3)
|
||||
{
|
||||
ESP_LOGE(TAG, "esp_now_register_recv_cb() 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;
|
||||
}
|
||||
else
|
||||
initialized = 3;
|
||||
}
|
||||
|
||||
if (initialized < 4)
|
||||
{
|
||||
peers.push_back(esp_now_peer_info_t{});
|
||||
esp_now_peer_info_t &peer = peers.back();
|
||||
std::memcpy(peer.peer_addr, broadcast_address, sizeof(peer.peer_addr));
|
||||
peer.channel = 0;
|
||||
/*
|
||||
if (settings.wifiSettings.wifiApEnabled)
|
||||
peer.ifidx = WIFI_IF_AP;
|
||||
else if (settings.wifiSettings.wifiStaEnabled)*/
|
||||
peer.ifidx = WIFI_IF_STA;
|
||||
|
||||
if (const auto error = esp_now_add_peer(&peers.back()); error != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "esp_now_add_peer() failed with %s", esp_err_to_name(error));
|
||||
return;
|
||||
}
|
||||
else
|
||||
initialized = 4;
|
||||
}
|
||||
|
||||
initialized = 255;
|
||||
}
|
||||
|
||||
void handle()
|
||||
{
|
||||
if (initialized < 255 && (settings.wifiSettings.wifiApEnabled || settings.wifiSettings.wifiStaEnabled))
|
||||
{
|
||||
initESPNow();
|
||||
return;
|
||||
}
|
||||
|
||||
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());
|
||||
ESP_LOGD(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);
|
||||
if (const auto result = cpputils::fromString<uint64_t>(msg.content); result)
|
||||
{
|
||||
onRecvTs(*result);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "could not parse number: %.*s", result.error().size(), result.error().data());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Unkown Type: %s - Message: %s", msg.type.c_str(), msg.content.c_str());
|
||||
}
|
||||
}
|
||||
for (const esp_now_message_t &msg : message_queue)
|
||||
{
|
||||
ESP_LOGI(TAG, "%s", msg.content.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void onRecvTs(uint64_t millis)
|
||||
{
|
||||
const auto milliseconds = std::chrono::milliseconds(millis);
|
||||
const auto timepoint = espchrono::utc_clock::time_point(milliseconds);
|
||||
time_set_now(timepoint);
|
||||
}
|
||||
|
||||
esp_err_t send_espnow_message(std::string message)
|
||||
{
|
||||
if (initialized < 255)
|
||||
return ESP_ERR_ESPNOW_NOT_INIT;
|
||||
|
||||
if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled)
|
||||
{
|
||||
return ESP_ERR_ESPNOW_IF;
|
||||
}
|
||||
|
||||
if (peers.size() < 1)
|
||||
{
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
for (const auto &peer : peers)
|
||||
{
|
||||
if(const auto error = esp_now_send(peer.peer_addr, (const uint8_t*)message.data(), message.size()); error != ESP_OK)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
else
|
||||
ESP_LOGI(TAG, "Successfully executed esp_now_send()");
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
} // namespace espnow
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <esp_now.h>
|
||||
|
||||
namespace espnow {
|
||||
constexpr const uint8_t broadcast_address[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
@ -12,10 +14,12 @@ struct esp_now_message_t {
|
||||
};
|
||||
|
||||
extern std::deque<esp_now_message_t> message_queue;
|
||||
extern std::list<esp_now_peer_info_t> peers;
|
||||
|
||||
void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len);
|
||||
void initESPNow();
|
||||
void handle();
|
||||
void onRecvTs(uint64_t millis);
|
||||
esp_err_t send_espnow_message(std::string message);
|
||||
} // namespace espnow
|
||||
#endif
|
||||
|
@ -1,42 +0,0 @@
|
||||
#include "espnowwrapper.h"
|
||||
|
||||
#include <list>
|
||||
#include <cstring>
|
||||
|
||||
namespace espnow {
|
||||
class ESPNowW32 : public IESPNowW {
|
||||
public:
|
||||
virtual esp_err_t add_peer(uint8_t *mac, int channel);
|
||||
virtual esp_err_t remove_peer(uint8_t *mac);
|
||||
virtual esp_err_t send_message(uint8_t *mac, uint8_t *data, size_t datalen);
|
||||
|
||||
private:
|
||||
std::list<esp_now_peer_info_t> peers;
|
||||
};
|
||||
|
||||
esp_err_t ESPNowW32::add_peer(uint8_t *mac, int channel) {
|
||||
peers.push_back(esp_now_peer_info_t{});
|
||||
esp_now_peer_info_t &peer = peers.back();
|
||||
std::memcpy(peer.peer_addr, mac, sizeof(peer.peer_addr));
|
||||
peer.channel = channel;
|
||||
peer.ifidx = WIFI_IF_STA;
|
||||
auto success = esp_now_add_peer(&peers.back());
|
||||
if (success != ESP_OK)
|
||||
remove_peer(mac);
|
||||
return success;
|
||||
}
|
||||
esp_err_t ESPNowW32::remove_peer(uint8_t *mac) {
|
||||
// find peer in peers
|
||||
for (auto it = peers.begin(); it != peers.end();) {
|
||||
if (0 == memcmp(mac, it->peer_addr, sizeof(it->peer_addr))) {
|
||||
it = peers.erase(it); // remove it
|
||||
}
|
||||
}
|
||||
return esp_now_del_peer(mac);
|
||||
}
|
||||
esp_err_t ESPNowW32::send_message(uint8_t *mac, uint8_t *data, size_t datalen) {
|
||||
return esp_now_send(mac, data, datalen);
|
||||
}
|
||||
ESPNowW32 espnow = ESPNowW32();
|
||||
IESPNowW &ESPNow = espnow;
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <esp_now.h>
|
||||
|
||||
namespace espnow {
|
||||
class IESPNowW {
|
||||
public:
|
||||
virtual int add_peer(uint8_t *mac, int channel = 0) = 0;
|
||||
virtual int remove_peer(uint8_t *mac) = 0;
|
||||
virtual int send_message(uint8_t *mac, uint8_t *data, size_t datalen) = 0;
|
||||
virtual int init() { return esp_now_init(); }
|
||||
int reg_send_cb(esp_now_send_cb_t cb) {
|
||||
return esp_now_register_send_cb(cb);
|
||||
}
|
||||
int reg_recv_cb(esp_now_recv_cb_t cb) {
|
||||
return esp_now_register_recv_cb(cb);
|
||||
}
|
||||
int unreg_send_cb() { return esp_now_unregister_send_cb(); }
|
||||
int unreg_recv_cb() { return esp_now_unregister_recv_cb(); }
|
||||
};
|
||||
|
||||
extern IESPNowW &ESPNow;
|
||||
} // namespace
|
Reference in New Issue
Block a user