Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
90133a7d67 | |||
dd3cbb9633 | |||
0b64933ee7 | |||
1d8d8e3f7d | |||
ceb80d8bd7 | |||
53464acb22 | |||
af8cc16675 |
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
<body onload="load();">
|
<body onload="load();">
|
||||||
<form class="box">
|
<form class="box">
|
||||||
<h1>ESP-NOW Switch </h1>
|
<h1>ESP-NOW Switch</h1>
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
<p class="text">Firmware:</p>
|
<p class="text">Firmware:</p>
|
||||||
<p class="text" id="version"></p>
|
<p class="text" id="version"></p>
|
||||||
|
29
src/main.cpp
29
src/main.cpp
@ -35,7 +35,6 @@ typedef struct
|
|||||||
|
|
||||||
struct deviceConfig
|
struct deviceConfig
|
||||||
{
|
{
|
||||||
const String firmware{"1.41"};
|
|
||||||
String espnowNetName{"DEFAULT"};
|
String espnowNetName{"DEFAULT"};
|
||||||
uint8_t workMode{0};
|
uint8_t workMode{0};
|
||||||
String deviceName = "ESP-NOW switch " + String(ESP.getChipId(), HEX);
|
String deviceName = "ESP-NOW switch " + String(ESP.getChipId(), HEX);
|
||||||
@ -53,6 +52,8 @@ struct deviceConfig
|
|||||||
|
|
||||||
std::vector<espnow_message_t> espnowMessage;
|
std::vector<espnow_message_t> espnowMessage;
|
||||||
|
|
||||||
|
const String firmware{"1.42"};
|
||||||
|
|
||||||
bool relayStatus{false};
|
bool relayStatus{false};
|
||||||
|
|
||||||
bool wasMqttAvailable{false};
|
bool wasMqttAvailable{false};
|
||||||
@ -172,7 +173,7 @@ void onBroadcastReceiving(const char *data, const uint8_t *sender)
|
|||||||
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;
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||||
deserializeJson(json, incomingData.message);
|
deserializeJson(json, incomingData.message);
|
||||||
bool temp = json["MQTT"] == "online" ? true : false;
|
bool temp = json["MQTT"] == "online" ? true : false;
|
||||||
if (wasMqttAvailable != temp)
|
if (wasMqttAvailable != temp)
|
||||||
@ -201,7 +202,7 @@ void onUnicastReceiving(const char *data, const uint8_t *sender)
|
|||||||
memcpy(&incomingData, data, sizeof(esp_now_payload_data_t));
|
memcpy(&incomingData, data, sizeof(esp_now_payload_data_t));
|
||||||
if (incomingData.deviceType != ENDT_GATEWAY || myNet.macToString(gatewayMAC) != myNet.macToString(sender))
|
if (incomingData.deviceType != ENDT_GATEWAY || myNet.macToString(gatewayMAC) != myNet.macToString(sender))
|
||||||
return;
|
return;
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||||
if (incomingData.payloadsType == ENPT_SET)
|
if (incomingData.payloadsType == ENPT_SET)
|
||||||
{
|
{
|
||||||
deserializeJson(json, incomingData.message);
|
deserializeJson(json, incomingData.message);
|
||||||
@ -220,7 +221,7 @@ void onUnicastReceiving(const char *data, const uint8_t *sender)
|
|||||||
else
|
else
|
||||||
digitalWrite(config.ledPin, config.ledPinType ? relayStatus : !relayStatus);
|
digitalWrite(config.ledPin, config.ledPinType ? relayStatus : !relayStatus);
|
||||||
}
|
}
|
||||||
saveConfig();
|
saveStatus();
|
||||||
sendStatusMessage();
|
sendStatusMessage();
|
||||||
}
|
}
|
||||||
if (incomingData.payloadsType == ENPT_UPDATE)
|
if (incomingData.payloadsType == ENPT_UPDATE)
|
||||||
@ -287,9 +288,9 @@ void loadStatus(void)
|
|||||||
saveStatus();
|
saveStatus();
|
||||||
File file = LittleFS.open("/status.json", "r");
|
File file = LittleFS.open("/status.json", "r");
|
||||||
String jsonFile = file.readString();
|
String jsonFile = file.readString();
|
||||||
DynamicJsonDocument json(32);
|
DynamicJsonDocument json(64); // To calculate the buffer size uses https://arduinojson.org/v6/assistant.
|
||||||
deserializeJson(json, jsonFile);
|
deserializeJson(json, jsonFile);
|
||||||
relayStatus = json["relayStatus"];
|
relayStatus = json["status"];
|
||||||
file.close();
|
file.close();
|
||||||
delay(50);
|
delay(50);
|
||||||
ETS_GPIO_INTR_ENABLE();
|
ETS_GPIO_INTR_ENABLE();
|
||||||
@ -298,8 +299,8 @@ void loadStatus(void)
|
|||||||
void saveStatus(void)
|
void saveStatus(void)
|
||||||
{
|
{
|
||||||
ETS_GPIO_INTR_DISABLE();
|
ETS_GPIO_INTR_DISABLE();
|
||||||
DynamicJsonDocument json(32);
|
DynamicJsonDocument json(48); // To calculate the buffer size uses https://arduinojson.org/v6/assistant.
|
||||||
json["relayStatus"] = relayStatus;
|
json["status"] = relayStatus;
|
||||||
json["system"] = "empty";
|
json["system"] = "empty";
|
||||||
File file = LittleFS.open("/status.json", "w");
|
File file = LittleFS.open("/status.json", "w");
|
||||||
serializeJsonPretty(json, file);
|
serializeJsonPretty(json, file);
|
||||||
@ -340,8 +341,8 @@ void setupWebServer()
|
|||||||
webServer.on("/config", HTTP_GET, [](AsyncWebServerRequest *request)
|
webServer.on("/config", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
String configJson;
|
String configJson;
|
||||||
DynamicJsonDocument json(512);
|
DynamicJsonDocument json(384); // To calculate the buffer size uses https://arduinojson.org/v6/assistant.
|
||||||
json["firmware"] = config.firmware;
|
json["firmware"] = firmware;
|
||||||
json["espnowNetName"] = config.espnowNetName;
|
json["espnowNetName"] = config.espnowNetName;
|
||||||
json["deviceName"] = config.deviceName;
|
json["deviceName"] = config.deviceName;
|
||||||
json["relayPin"] = config.relayPin;
|
json["relayPin"] = config.relayPin;
|
||||||
@ -407,7 +408,7 @@ void sendAttributesMessage(const uint8_t type)
|
|||||||
uint32_t days = hours / 24;
|
uint32_t days = hours / 24;
|
||||||
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_ATTRIBUTES};
|
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_ATTRIBUTES};
|
||||||
espnow_message_t message;
|
espnow_message_t message;
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||||
if (type == ENST_NONE)
|
if (type == ENST_NONE)
|
||||||
json["Type"] = "ESP-NOW switch";
|
json["Type"] = "ESP-NOW switch";
|
||||||
else
|
else
|
||||||
@ -417,7 +418,7 @@ void sendAttributesMessage(const uint8_t type)
|
|||||||
}
|
}
|
||||||
json["MCU"] = "ESP8266";
|
json["MCU"] = "ESP8266";
|
||||||
json["MAC"] = myNet.getNodeMac();
|
json["MAC"] = myNet.getNodeMac();
|
||||||
json["Firmware"] = config.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));
|
||||||
serializeJsonPretty(json, outgoingData.message);
|
serializeJsonPretty(json, outgoingData.message);
|
||||||
@ -446,7 +447,7 @@ void sendConfigMessage(const uint8_t type)
|
|||||||
return;
|
return;
|
||||||
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_CONFIG};
|
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_CONFIG};
|
||||||
espnow_message_t message;
|
espnow_message_t message;
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||||
if (type == ENST_NONE)
|
if (type == ENST_NONE)
|
||||||
{
|
{
|
||||||
json[MCMT_DEVICE_NAME] = config.deviceName;
|
json[MCMT_DEVICE_NAME] = config.deviceName;
|
||||||
@ -498,7 +499,7 @@ void sendStatusMessage(const uint8_t type)
|
|||||||
statusMessageTimerSemaphore = false;
|
statusMessageTimerSemaphore = false;
|
||||||
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_STATE};
|
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_STATE};
|
||||||
espnow_message_t message;
|
espnow_message_t message;
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||||
if (type == ENST_NONE)
|
if (type == ENST_NONE)
|
||||||
json["state"] = relayStatus ? "ON" : "OFF";
|
json["state"] = relayStatus ? "ON" : "OFF";
|
||||||
if (type == ENST_DS18B20)
|
if (type == ENST_DS18B20)
|
||||||
|
Reference in New Issue
Block a user