Version 1.12

Main code refactoring.
This commit is contained in:
Alexey Zholtikov 2023-01-14 10:57:25 +03:00
parent dfabdd080a
commit 7f51f05eb8

View File

@ -7,7 +7,7 @@
void onBroadcastReceiving(const char *data, const uint8_t *sender); void onBroadcastReceiving(const char *data, const uint8_t *sender);
void onUnicastReceiving(const char *data, const uint8_t *sender); void onUnicastReceiving(const char *data, const uint8_t *sender);
void onConfirmReceiving(const uint8_t *target, const bool status); void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status);
void loadConfig(void); void loadConfig(void);
void saveConfig(void); void saveConfig(void);
@ -18,11 +18,19 @@ void sendKeepAliveMessage(void);
void sendConfigMessage(void); void sendConfigMessage(void);
void sendStatusMessage(void); void sendStatusMessage(void);
String getValue(String data, char separator, byte index); String getValue(String data, char separator, uint8_t index);
void changeLedState(void); void changeLedState(void);
const String firmware{"1.11"}; typedef struct
{
uint16_t id{0};
char message[200]{0};
} espnow_message_t;
std::vector<espnow_message_t> espnowMessage;
const String firmware{"1.12"};
String espnowNetName{"DEFAULT"}; String espnowNetName{"DEFAULT"};
@ -62,23 +70,14 @@ void apModeHideTimerCallback(void);
Ticker attributesMessageTimer; Ticker attributesMessageTimer;
bool attributesMessageTimerSemaphore{true}; bool attributesMessageTimerSemaphore{true};
void attributesMessageTimerCallback(void); void attributesMessageTimerCallback(void);
Ticker attributesMessageResendTimer;
bool attributesMessageResendTimerSemaphore{false};
Ticker keepAliveMessageTimer; Ticker keepAliveMessageTimer;
bool keepAliveMessageTimerSemaphore{true}; bool keepAliveMessageTimerSemaphore{true};
void keepAliveMessageTimerCallback(void); void keepAliveMessageTimerCallback(void);
Ticker keepAliveMessageResendTimer;
bool keepAliveMessageResendTimerSemaphore{false};
Ticker configMessageResendTimer;
bool configMessageResendTimerSemaphore{false};
Ticker statusMessageTimer; Ticker statusMessageTimer;
bool statusMessageTimerSemaphore{true}; bool statusMessageTimerSemaphore{true};
void statusMessageTimerCallback(void); void statusMessageTimerCallback(void);
Ticker statusMessageResendTimer;
bool statusMessageResendTimerSemaphore{false};
void setup() void setup()
{ {
@ -133,14 +132,14 @@ void loop()
ArduinoOTA.handle(); ArduinoOTA.handle();
} }
void onBroadcastReceiving(const char *data, const byte *sender) void onBroadcastReceiving(const char *data, const uint8_t *sender)
{ {
esp_now_payload_data_t incomingData; esp_now_payload_data_t incomingData;
memcpy(&incomingData, data, sizeof(esp_now_payload_data_t)); memcpy(&incomingData, data, sizeof(esp_now_payload_data_t));
if (incomingData.deviceType != ENDT_GATEWAY) if (incomingData.deviceType != ENDT_GATEWAY)
return; return;
if (myNet.macToString(gatewayMAC) != myNet.macToString(sender) && incomingData.payloadsType == ENPT_KEEP_ALIVE) if (myNet.macToString(gatewayMAC) != myNet.macToString(sender) && incomingData.payloadsType == ENPT_KEEP_ALIVE)
memcpy(gatewayMAC, sender, 6); memcpy(&gatewayMAC, sender, 6);
if (myNet.macToString(gatewayMAC) == myNet.macToString(sender) && incomingData.payloadsType == ENPT_KEEP_ALIVE) if (myNet.macToString(gatewayMAC) == myNet.macToString(sender) && incomingData.payloadsType == ENPT_KEEP_ALIVE)
{ {
isGatewayAvailable = true; isGatewayAvailable = true;
@ -157,7 +156,7 @@ void onBroadcastReceiving(const char *data, const byte *sender)
} }
} }
void onUnicastReceiving(const char *data, const byte *sender) void onUnicastReceiving(const char *data, const uint8_t *sender)
{ {
esp_now_payload_data_t incomingData; esp_now_payload_data_t incomingData;
memcpy(&incomingData, data, sizeof(esp_now_payload_data_t)); memcpy(&incomingData, data, sizeof(esp_now_payload_data_t));
@ -192,30 +191,19 @@ void onUnicastReceiving(const char *data, const byte *sender)
ESP.restart(); ESP.restart();
} }
void onConfirmReceiving(const uint8_t *target, const bool status) void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status)
{ {
if (status) for (uint16_t i{0}; i < espnowMessage.size(); ++i)
{ {
if (attributesMessageResendTimerSemaphore) espnow_message_t message = espnowMessage[i];
{ if (message.id == id)
attributesMessageResendTimerSemaphore = false; if (status)
attributesMessageResendTimer.detach(); espnowMessage.erase(espnowMessage.begin() + i);
} else
if (keepAliveMessageResendTimerSemaphore) {
{ message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
keepAliveMessageResendTimerSemaphore = false; espnowMessage.at(i) = message;
keepAliveMessageResendTimer.detach(); }
}
if (configMessageResendTimerSemaphore)
{
configMessageResendTimerSemaphore = false;
configMessageResendTimer.detach();
}
if (statusMessageResendTimerSemaphore)
{
statusMessageResendTimerSemaphore = false;
statusMessageResendTimer.detach();
}
} }
} }
@ -313,6 +301,7 @@ void sendAttributesMessage()
uint32_t hours = mins / 60; uint32_t hours = mins / 60;
uint32_t days = hours / 24; uint32_t days = hours / 24;
esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_ATTRIBUTES}; esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_ATTRIBUTES};
espnow_message_t message;
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json; StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
json["Type"] = "ESP-NOW light"; json["Type"] = "ESP-NOW light";
json["MCU"] = "ESP8266"; json["MCU"] = "ESP8266";
@ -320,15 +309,11 @@ void sendAttributesMessage()
json["Firmware"] = firmware; json["Firmware"] = firmware;
json["Library"] = myNet.getFirmwareVersion(); json["Library"] = myNet.getFirmwareVersion();
json["Uptime"] = "Days:" + String(days) + " Hours:" + String(hours - (days * 24)) + " Mins:" + String(mins - (hours * 60)); json["Uptime"] = "Days:" + String(days) + " Hours:" + String(hours - (days * 24)) + " Mins:" + String(mins - (hours * 60));
char buffer[sizeof(esp_now_payload_data_t::message)]{0}; serializeJsonPretty(json, outgoingData.message);
serializeJsonPretty(json, buffer); memcpy(&message.message, &outgoingData, sizeof(esp_now_payload_data_t));
memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message)); message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
char temp[sizeof(esp_now_payload_data_t)]{0};
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
myNet.sendUnicastMessage(temp, gatewayMAC, true);
attributesMessageResendTimerSemaphore = true; espnowMessage.push_back(message);
attributesMessageResendTimer.once(1, sendAttributesMessage);
} }
void sendKeepAliveMessage() void sendKeepAliveMessage()
@ -337,12 +322,11 @@ void sendKeepAliveMessage()
return; return;
keepAliveMessageTimerSemaphore = false; keepAliveMessageTimerSemaphore = false;
esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_KEEP_ALIVE}; esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_KEEP_ALIVE};
char temp[sizeof(esp_now_payload_data_t)]{0}; espnow_message_t message;
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t)); memcpy(&message.message, &outgoingData, sizeof(esp_now_payload_data_t));
myNet.sendUnicastMessage(temp, gatewayMAC, true); message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
keepAliveMessageResendTimerSemaphore = true; espnowMessage.push_back(message);
keepAliveMessageResendTimer.once(1, sendKeepAliveMessage);
} }
void sendConfigMessage() void sendConfigMessage()
@ -350,6 +334,7 @@ void sendConfigMessage()
if (!isGatewayAvailable) if (!isGatewayAvailable)
return; return;
esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_CONFIG}; esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_CONFIG};
espnow_message_t message;
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json; StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
json["name"] = deviceName; json["name"] = deviceName;
json["unit"] = 1; json["unit"] = 1;
@ -357,15 +342,11 @@ void sendConfigMessage()
json["class"] = ledType; json["class"] = ledType;
json["payload_on"] = payloadOn; json["payload_on"] = payloadOn;
json["payload_off"] = payloadOff; json["payload_off"] = payloadOff;
char buffer[sizeof(esp_now_payload_data_t::message)]{0}; serializeJsonPretty(json, outgoingData.message);
serializeJsonPretty(json, buffer); memcpy(&message.message, &outgoingData, sizeof(esp_now_payload_data_t));
memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message)); message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
char temp[sizeof(esp_now_payload_data_t)]{0};
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
myNet.sendUnicastMessage(temp, gatewayMAC, true);
configMessageResendTimerSemaphore = true; espnowMessage.push_back(message);
configMessageResendTimer.once(1, sendConfigMessage);
} }
void sendStatusMessage() void sendStatusMessage()
@ -374,28 +355,25 @@ void sendStatusMessage()
return; return;
statusMessageTimerSemaphore = false; statusMessageTimerSemaphore = false;
esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_STATE}; esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_STATE};
espnow_message_t message;
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json; StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
json["state"] = ledStatus ? payloadOn : payloadOff; json["state"] = ledStatus ? payloadOn : payloadOff;
json["brightness"] = brightness; json["brightness"] = brightness;
json["temperature"] = temperature; json["temperature"] = temperature;
json["rgb"] = String(red) + "," + String(green) + "," + String(blue); json["rgb"] = String(red) + "," + String(green) + "," + String(blue);
char buffer[sizeof(esp_now_payload_data_t::message)]{0}; serializeJsonPretty(json, outgoingData.message);
serializeJsonPretty(json, buffer); memcpy(&message.message, &outgoingData, sizeof(esp_now_payload_data_t));
memcpy(&outgoingData.message, &buffer, sizeof(esp_now_payload_data_t::message)); message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
char temp[sizeof(esp_now_payload_data_t)]{0};
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
myNet.sendUnicastMessage(temp, gatewayMAC, true);
statusMessageResendTimerSemaphore = true; espnowMessage.push_back(message);
statusMessageResendTimer.once(1, sendStatusMessage);
} }
String getValue(String data, char separator, byte index) String getValue(String data, char separator, uint8_t index)
{ {
byte found{0}; uint8_t found{0};
int strIndex[]{0, -1}; int strIndex[]{0, -1};
int maxIndex = data.length() - 1; int maxIndex = data.length() - 1;
for (byte i{0}; i <= maxIndex && found <= index; i++) for (uint8_t i{0}; i <= maxIndex && found <= index; i++)
if (data.charAt(i) == separator || i == maxIndex) if (data.charAt(i) == separator || i == maxIndex)
{ {
found++; found++;
@ -471,7 +449,8 @@ void changeLedState(void)
void gatewayAvailabilityCheckTimerCallback() void gatewayAvailabilityCheckTimerCallback()
{ {
isGatewayAvailable = false; isGatewayAvailable = false;
memset(gatewayMAC, 0, 6); memset(&gatewayMAC, 0, 6);
espnowMessage.clear();
} }
void apModeHideTimerCallback() void apModeHideTimerCallback()