Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
619b3a6cdf | |||
ef625740d9 | |||
549d9cf916 |
@ -4,7 +4,7 @@ ESP-NOW based switch for ESP8266. Alternate firmware for Tuya/SmartLife WiFi swi
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
1. After turn on (or after rebooting) creates an access point named "ESP-NOW Switch XXXXXXXXXXXX" with password "12345678" (IP 192.168.4.1). Access point will be shown during 5 minutes. The rest of the time access point is a hidden.
|
1. After turn on (or after rebooting) creates an access point named "ESP-NOW switch XXXXXXXXXXXX" with password "12345678" (IP 192.168.4.1). Access point will be shown during 5 minutes. The rest of the time access point is a hidden.
|
||||||
2. Periodically transmission of system information (every 60 seconds), availability status (every 10 seconds) and state status (every 300 seconds) to the gateway.
|
2. Periodically transmission of system information (every 60 seconds), availability status (every 10 seconds) and state status (every 300 seconds) to the gateway.
|
||||||
3. Saves the last state when the power is turned off. Goes to the last state when the power is turned on.
|
3. Saves the last state when the power is turned off. Goes to the last state when the power is turned on.
|
||||||
4. Automatically adds switch configuration to Home Assistan via MQTT discovery as a switch.
|
4. Automatically adds switch configuration to Home Assistan via MQTT discovery as a switch.
|
||||||
@ -25,5 +25,6 @@ See [here](https://github.com/aZholtikov/ESP-NOW-Switch/tree/main/hardware).
|
|||||||
|
|
||||||
1. A gateway is required. For details see [ESP-NOW Gateway](https://github.com/aZholtikov/ESP-NOW-Gateway).
|
1. A gateway is required. For details see [ESP-NOW Gateway](https://github.com/aZholtikov/ESP-NOW-Gateway).
|
||||||
2. ESP-NOW network name must be set same of all another ESP-NOW devices in network.
|
2. ESP-NOW network name must be set same of all another ESP-NOW devices in network.
|
||||||
3. Upload the "data" folder (with web interface) into the filesystem before flashing.
|
3. If encryption is used, the key must be set same of all another ESP-NOW devices in network.
|
||||||
4. For using this firmware on Tuya/SmartLife WiFi switches, the WiFi module must be replaced with an ESP8266 compatible module (if necessary).
|
4. Upload the "data" folder (with web interface) into the filesystem before flashing.
|
||||||
|
5. For using this firmware on Tuya/SmartLife WiFi switches, the WiFi module must be replaced with an ESP8266 compatible module (if necessary).
|
||||||
|
109
src/main.cpp
109
src/main.cpp
@ -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);
|
||||||
@ -21,7 +21,15 @@ void sendKeepAliveMessage(void);
|
|||||||
void sendConfigMessage(void);
|
void sendConfigMessage(void);
|
||||||
void sendStatusMessage(void);
|
void sendStatusMessage(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.14"};
|
||||||
|
|
||||||
String espnowNetName{"DEFAULT"};
|
String espnowNetName{"DEFAULT"};
|
||||||
|
|
||||||
@ -59,23 +67,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()
|
||||||
{
|
{
|
||||||
@ -100,6 +99,7 @@ void setup()
|
|||||||
|
|
||||||
WiFi.setSleepMode(WIFI_NONE_SLEEP);
|
WiFi.setSleepMode(WIFI_NONE_SLEEP);
|
||||||
myNet.begin(espnowNetName.c_str());
|
myNet.begin(espnowNetName.c_str());
|
||||||
|
// myNet.setCryptKey("VERY_LONG_CRYPT_KEY"); // If encryption is used, the key must be set same of all another ESP-NOW devices in network.
|
||||||
|
|
||||||
myNet.setOnBroadcastReceivingCallback(onBroadcastReceiving);
|
myNet.setOnBroadcastReceivingCallback(onBroadcastReceiving);
|
||||||
myNet.setOnUnicastReceivingCallback(onUnicastReceiving);
|
myNet.setOnUnicastReceivingCallback(onUnicastReceiving);
|
||||||
@ -137,7 +137,7 @@ void onBroadcastReceiving(const char *data, const uint8_t *sender)
|
|||||||
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;
|
||||||
@ -182,30 +182,19 @@ void onUnicastReceiving(const char *data, const uint8_t *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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,6 +306,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_SWITCH, ENPT_ATTRIBUTES};
|
esp_now_payload_data_t outgoingData{ENDT_SWITCH, 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 switch";
|
json["Type"] = "ESP-NOW switch";
|
||||||
json["MCU"] = "ESP8266";
|
json["MCU"] = "ESP8266";
|
||||||
@ -324,15 +314,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()
|
||||||
@ -341,12 +327,11 @@ void sendKeepAliveMessage()
|
|||||||
return;
|
return;
|
||||||
keepAliveMessageTimerSemaphore = false;
|
keepAliveMessageTimerSemaphore = false;
|
||||||
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_KEEP_ALIVE};
|
esp_now_payload_data_t outgoingData{ENDT_SWITCH, 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()
|
||||||
@ -354,22 +339,20 @@ void sendConfigMessage()
|
|||||||
if (!isGatewayAvailable)
|
if (!isGatewayAvailable)
|
||||||
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;
|
||||||
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;
|
||||||
json["type"] = HACT_SWITCH;
|
json["type"] = HACT_SWITCH;
|
||||||
json["class"] = HASWDC_SWITCH;
|
json["class"] = HASWDC_SWITCH;
|
||||||
|
json["template"] = "state";
|
||||||
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()
|
||||||
@ -378,23 +361,21 @@ void sendStatusMessage()
|
|||||||
return;
|
return;
|
||||||
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;
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||||
json["state"] = relayStatus ? payloadOn : payloadOff;
|
json["state"] = relayStatus ? payloadOn : 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);
|
|
||||||
|
|
||||||
statusMessageResendTimerSemaphore = true;
|
espnowMessage.push_back(message);
|
||||||
statusMessageResendTimer.once(1, sendStatusMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void gatewayAvailabilityCheckTimerCallback()
|
void gatewayAvailabilityCheckTimerCallback()
|
||||||
{
|
{
|
||||||
isGatewayAvailable = false;
|
isGatewayAvailable = false;
|
||||||
memset(gatewayMAC, 0, 6);
|
memset(&gatewayMAC, 0, 6);
|
||||||
|
espnowMessage.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void apModeHideTimerCallback()
|
void apModeHideTimerCallback()
|
||||||
|
Reference in New Issue
Block a user