Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
619b3a6cdf | |||
ef625740d9 | |||
549d9cf916 | |||
82e736dc8b | |||
439722511f |
@ -4,7 +4,7 @@ ESP-NOW based switch for ESP8266. Alternate firmware for Tuya/SmartLife WiFi swi
|
||||
|
||||
## 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.
|
||||
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.
|
||||
@ -19,11 +19,12 @@ ESP-NOW based switch for ESP8266. Alternate firmware for Tuya/SmartLife WiFi swi
|
||||
|
||||
## Tested on
|
||||
|
||||
Coming soon.
|
||||
See [here](https://github.com/aZholtikov/ESP-NOW-Switch/tree/main/hardware).
|
||||
|
||||
## Attention
|
||||
|
||||
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.
|
||||
3. Upload the "data" folder (with web interface) into the filesystem before flashing.
|
||||
4. For using this firmware on Tuya/SmartLife WiFi switches, the WiFi module must be replaced with an ESP8266 compatible module (if necessary).
|
||||
3. If encryption is used, the key must be set same of all another ESP-NOW devices in network.
|
||||
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).
|
||||
|
@ -1,3 +1,7 @@
|
||||
p{
|
||||
margin: 0 0;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: "Gill Sans", sans-serif;
|
||||
background: rgb(255, 255, 255);
|
||||
@ -21,6 +25,8 @@ h1 {
|
||||
font-weight: 600;
|
||||
flex-shrink: 0;
|
||||
margin-right: 10px;
|
||||
margin-left: 10px;
|
||||
margin: 10px 0;
|
||||
}
|
||||
|
||||
.text-select {
|
||||
@ -42,6 +48,7 @@ input {
|
||||
border-radius: 5px;
|
||||
border: none;
|
||||
margin-bottom: 10px;
|
||||
margin-left: 10px;
|
||||
padding: 0 10px;
|
||||
color: rgb(0, 0, 0);
|
||||
background: #a3e0f1;
|
||||
@ -76,6 +83,8 @@ select:hover {
|
||||
background: rgb(65, 125, 238);
|
||||
color: white;
|
||||
transition: .5s;
|
||||
margin-left: 0;
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
@ -89,6 +98,10 @@ select:hover {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
#espnowNetName {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.wrapper.wrapper--end {
|
||||
align-items: baseline;
|
||||
}
|
BIN
hardware/MOES_1CH_10A/Pins.jpeg
Normal file
BIN
hardware/MOES_1CH_10A/Pins.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.5 MiB |
BIN
hardware/MOES_1CH_10A/TYWE2S.pdf
Normal file
BIN
hardware/MOES_1CH_10A/TYWE2S.pdf
Normal file
Binary file not shown.
BIN
hardware/MOES_1CH_10A/WB2S.pdf
Normal file
BIN
hardware/MOES_1CH_10A/WB2S.pdf
Normal file
Binary file not shown.
BIN
hardware/MOES_1CH_10A/inside1.jpeg
Normal file
BIN
hardware/MOES_1CH_10A/inside1.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.7 MiB |
BIN
hardware/MOES_1CH_10A/inside2.jpeg
Normal file
BIN
hardware/MOES_1CH_10A/inside2.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.5 MiB |
BIN
hardware/MOES_1CH_10A/inside3.jpeg
Normal file
BIN
hardware/MOES_1CH_10A/inside3.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.5 MiB |
BIN
hardware/MOES_1CH_10A/main.jpeg
Normal file
BIN
hardware/MOES_1CH_10A/main.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 MiB |
12
hardware/README.md
Normal file
12
hardware/README.md
Normal file
@ -0,0 +1,12 @@
|
||||
# Tested on
|
||||
|
||||
1. MOES 1CH 10A. Built on Tuya WiFi module WA2 (WB2S) (BK7231T chip). Replacement required. Performed replacement with ESP-02S (TYWE2S). [Photo](https://github.com/aZholtikov/ESP-NOW-Switch/tree/main/hardware/MOES_1CH_10A).
|
||||
|
||||
```text
|
||||
Relay GPIO GPIO04 HIGH
|
||||
Led GPIO GPIO12 LOW
|
||||
Button GPIO GPIO14 RISING
|
||||
```
|
||||
|
||||
2. AUBESS 1CH 16A (Coming soon)
|
||||
3. LIGHT E27 SOCKET (Coming soon)
|
@ -1,4 +1,4 @@
|
||||
[env:esp8266]
|
||||
[env:ESP-12E]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
framework = arduino
|
||||
@ -9,7 +9,7 @@ lib_deps =
|
||||
bblanchon/ArduinoJson@^6.19.4
|
||||
me-no-dev/ESP Async WebServer@^1.2.3
|
||||
|
||||
[env:esp8266-ota]
|
||||
[env:ESP-12E-OTA]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
framework = arduino
|
||||
@ -22,7 +22,7 @@ lib_deps =
|
||||
bblanchon/ArduinoJson@^6.19.4
|
||||
me-no-dev/ESP Async WebServer@^1.2.3
|
||||
|
||||
[env:esp8285]
|
||||
[env:ESP-02S]
|
||||
platform = espressif8266
|
||||
board = esp8285
|
||||
framework = arduino
|
||||
@ -32,16 +32,3 @@ lib_deps =
|
||||
https://github.com/aZholtikov/ZHConfig
|
||||
bblanchon/ArduinoJson@^6.19.4
|
||||
me-no-dev/ESP Async WebServer@^1.2.3
|
||||
|
||||
[env:esp8285-ota]
|
||||
platform = espressif8266
|
||||
board = esp8285
|
||||
framework = arduino
|
||||
upload_port = 192.168.4.1
|
||||
upload_protocol = espota
|
||||
lib_extra_dirs =
|
||||
lib_deps =
|
||||
https://github.com/aZholtikov/ZHNetwork
|
||||
https://github.com/aZholtikov/ZHConfig
|
||||
bblanchon/ArduinoJson@^6.19.4
|
||||
me-no-dev/ESP Async WebServer@^1.2.3
|
||||
|
129
src/main.cpp
129
src/main.cpp
@ -7,7 +7,7 @@
|
||||
|
||||
void onBroadcastReceiving(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 saveConfig(void);
|
||||
@ -21,11 +21,19 @@ void sendKeepAliveMessage(void);
|
||||
void sendConfigMessage(void);
|
||||
void sendStatusMessage(void);
|
||||
|
||||
const String firmware{"1.1"};
|
||||
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 deviceName{"ESP-NOW switch"};
|
||||
String deviceName = "ESP-NOW switch " + String(ESP.getChipId(), HEX);
|
||||
|
||||
bool relayStatus{false};
|
||||
uint8_t relayPin{0};
|
||||
@ -41,6 +49,9 @@ bool wasMqttAvailable{false};
|
||||
|
||||
uint8_t gatewayMAC[6]{0};
|
||||
|
||||
const String payloadOn{"ON"};
|
||||
const String payloadOff{"OFF"};
|
||||
|
||||
ZHNetwork myNet;
|
||||
AsyncWebServer webServer(80);
|
||||
|
||||
@ -56,23 +67,14 @@ void apModeHideTimerCallback(void);
|
||||
Ticker attributesMessageTimer;
|
||||
bool attributesMessageTimerSemaphore{true};
|
||||
void attributesMessageTimerCallback(void);
|
||||
Ticker attributesMessageResendTimer;
|
||||
bool attributesMessageResendTimerSemaphore{false};
|
||||
|
||||
Ticker keepAliveMessageTimer;
|
||||
bool keepAliveMessageTimerSemaphore{true};
|
||||
void keepAliveMessageTimerCallback(void);
|
||||
Ticker keepAliveMessageResendTimer;
|
||||
bool keepAliveMessageResendTimerSemaphore{false};
|
||||
|
||||
Ticker configMessageResendTimer;
|
||||
bool configMessageResendTimerSemaphore{false};
|
||||
|
||||
Ticker statusMessageTimer;
|
||||
bool statusMessageTimerSemaphore{true};
|
||||
void statusMessageTimerCallback(void);
|
||||
Ticker statusMessageResendTimer;
|
||||
bool statusMessageResendTimerSemaphore{false};
|
||||
|
||||
void setup()
|
||||
{
|
||||
@ -97,13 +99,14 @@ void setup()
|
||||
|
||||
WiFi.setSleepMode(WIFI_NONE_SLEEP);
|
||||
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.setOnUnicastReceivingCallback(onUnicastReceiving);
|
||||
myNet.setOnConfirmReceivingCallback(onConfirmReceiving);
|
||||
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
WiFi.softAP(("ESP-NOW Switch " + myNet.getNodeMac()).c_str(), "12345678", 1, 0);
|
||||
WiFi.softAP(("ESP-NOW switch " + String(ESP.getChipId(), HEX)).c_str(), "12345678", 1, 0);
|
||||
apModeHideTimer.once(300, apModeHideTimerCallback);
|
||||
|
||||
setupWebServer();
|
||||
@ -134,7 +137,7 @@ void onBroadcastReceiving(const char *data, const uint8_t *sender)
|
||||
if (incomingData.deviceType != ENDT_GATEWAY)
|
||||
return;
|
||||
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)
|
||||
{
|
||||
isGatewayAvailable = true;
|
||||
@ -161,7 +164,7 @@ void onUnicastReceiving(const char *data, const uint8_t *sender)
|
||||
if (incomingData.payloadsType == ENPT_SET)
|
||||
{
|
||||
deserializeJson(json, incomingData.message);
|
||||
relayStatus = json["set"] == "ON" ? true : false;
|
||||
relayStatus = json["set"] == payloadOn ? true : false;
|
||||
if (relayPin)
|
||||
digitalWrite(relayPin, relayPinType ? relayStatus : !relayStatus);
|
||||
if (ledPin)
|
||||
@ -171,36 +174,26 @@ void onUnicastReceiving(const char *data, const uint8_t *sender)
|
||||
}
|
||||
if (incomingData.payloadsType == ENPT_UPDATE)
|
||||
{
|
||||
WiFi.softAP(("ESP-NOW Switch " + myNet.getNodeMac()).c_str(), "12345678", 1, 0);
|
||||
WiFi.softAP(("ESP-NOW switch " + String(ESP.getChipId(), HEX)).c_str(), "12345678", 1, 0);
|
||||
webServer.begin();
|
||||
apModeHideTimer.once(300, apModeHideTimerCallback);
|
||||
}
|
||||
if (incomingData.payloadsType == ENPT_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)
|
||||
{
|
||||
for (uint16_t i{0}; i < espnowMessage.size(); ++i)
|
||||
{
|
||||
espnow_message_t message = espnowMessage[i];
|
||||
if (message.id == id)
|
||||
if (status)
|
||||
espnowMessage.erase(espnowMessage.begin() + i);
|
||||
else
|
||||
{
|
||||
if (attributesMessageResendTimerSemaphore)
|
||||
{
|
||||
attributesMessageResendTimerSemaphore = false;
|
||||
attributesMessageResendTimer.detach();
|
||||
}
|
||||
if (keepAliveMessageResendTimerSemaphore)
|
||||
{
|
||||
keepAliveMessageResendTimerSemaphore = false;
|
||||
keepAliveMessageResendTimer.detach();
|
||||
}
|
||||
if (configMessageResendTimerSemaphore)
|
||||
{
|
||||
configMessageResendTimerSemaphore = false;
|
||||
configMessageResendTimer.detach();
|
||||
}
|
||||
if (statusMessageResendTimerSemaphore)
|
||||
{
|
||||
statusMessageResendTimerSemaphore = false;
|
||||
statusMessageResendTimer.detach();
|
||||
message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
|
||||
espnowMessage.at(i) = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -288,7 +281,7 @@ void setupWebServer()
|
||||
void IRAM_ATTR buttonInterrupt()
|
||||
{
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
buttonInterruptTimer.once_ms(50, switchingRelay); // For prevent contact chatter.
|
||||
buttonInterruptTimer.once_ms(100, switchingRelay); // For prevent contact chatter.
|
||||
}
|
||||
|
||||
void switchingRelay()
|
||||
@ -313,22 +306,19 @@ void sendAttributesMessage()
|
||||
uint32_t hours = mins / 60;
|
||||
uint32_t days = hours / 24;
|
||||
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_ATTRIBUTES};
|
||||
espnow_message_t message;
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
json["Type"] = "ESP-NOW Switch";
|
||||
json["Type"] = "ESP-NOW switch";
|
||||
json["MCU"] = "ESP8266";
|
||||
json["MAC"] = myNet.getNodeMac();
|
||||
json["Firmware"] = firmware;
|
||||
json["Library"] = myNet.getFirmwareVersion();
|
||||
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, buffer);
|
||||
memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message));
|
||||
char temp[sizeof(esp_now_payload_data_t)]{0};
|
||||
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
myNet.sendUnicastMessage(temp, gatewayMAC, true);
|
||||
serializeJsonPretty(json, outgoingData.message);
|
||||
memcpy(&message.message, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
|
||||
|
||||
attributesMessageResendTimerSemaphore = true;
|
||||
attributesMessageResendTimer.once(1, sendAttributesMessage);
|
||||
espnowMessage.push_back(message);
|
||||
}
|
||||
|
||||
void sendKeepAliveMessage()
|
||||
@ -337,12 +327,11 @@ void sendKeepAliveMessage()
|
||||
return;
|
||||
keepAliveMessageTimerSemaphore = false;
|
||||
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_KEEP_ALIVE};
|
||||
char temp[sizeof(esp_now_payload_data_t)]{0};
|
||||
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
myNet.sendUnicastMessage(temp, gatewayMAC, true);
|
||||
espnow_message_t message;
|
||||
memcpy(&message.message, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
|
||||
|
||||
keepAliveMessageResendTimerSemaphore = true;
|
||||
keepAliveMessageResendTimer.once(1, sendKeepAliveMessage);
|
||||
espnowMessage.push_back(message);
|
||||
}
|
||||
|
||||
void sendConfigMessage()
|
||||
@ -350,21 +339,20 @@ void sendConfigMessage()
|
||||
if (!isGatewayAvailable)
|
||||
return;
|
||||
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_CONFIG};
|
||||
espnow_message_t message;
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
json["name"] = deviceName;
|
||||
json["unit"] = 1;
|
||||
json["type"] = HACT_SWITCH;
|
||||
json["class"] = HASWDC_SWITCH;
|
||||
json["reverse"] = "false";
|
||||
char buffer[sizeof(esp_now_payload_data_t::message)]{0};
|
||||
serializeJsonPretty(json, buffer);
|
||||
memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message));
|
||||
char temp[sizeof(esp_now_payload_data_t)]{0};
|
||||
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
myNet.sendUnicastMessage(temp, gatewayMAC, true);
|
||||
json["template"] = "state";
|
||||
json["payload_on"] = payloadOn;
|
||||
json["payload_off"] = payloadOff;
|
||||
serializeJsonPretty(json, outgoingData.message);
|
||||
memcpy(&message.message, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
|
||||
|
||||
configMessageResendTimerSemaphore = true;
|
||||
configMessageResendTimer.once(5, sendConfigMessage);
|
||||
espnowMessage.push_back(message);
|
||||
}
|
||||
|
||||
void sendStatusMessage()
|
||||
@ -373,28 +361,27 @@ void sendStatusMessage()
|
||||
return;
|
||||
statusMessageTimerSemaphore = false;
|
||||
esp_now_payload_data_t outgoingData{ENDT_SWITCH, ENPT_STATE};
|
||||
espnow_message_t message;
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
json["state"] = relayStatus ? "ON" : "OFF";
|
||||
char buffer[sizeof(esp_now_payload_data_t::message)]{0};
|
||||
serializeJsonPretty(json, buffer);
|
||||
memcpy(&outgoingData.message, &buffer, sizeof(esp_now_payload_data_t::message));
|
||||
char temp[sizeof(esp_now_payload_data_t)]{0};
|
||||
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
myNet.sendUnicastMessage(temp, gatewayMAC, true);
|
||||
json["state"] = relayStatus ? payloadOn : payloadOff;
|
||||
serializeJsonPretty(json, outgoingData.message);
|
||||
memcpy(&message.message, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
|
||||
|
||||
statusMessageResendTimerSemaphore = true;
|
||||
statusMessageResendTimer.once(1, sendStatusMessage);
|
||||
espnowMessage.push_back(message);
|
||||
}
|
||||
|
||||
void gatewayAvailabilityCheckTimerCallback()
|
||||
{
|
||||
isGatewayAvailable = false;
|
||||
memset(gatewayMAC, 0, 6);
|
||||
memset(&gatewayMAC, 0, 6);
|
||||
espnowMessage.clear();
|
||||
}
|
||||
|
||||
void apModeHideTimerCallback()
|
||||
{
|
||||
WiFi.softAP(("ESP-NOW Switch " + myNet.getNodeMac()).c_str(), "12345678", 1, 1);
|
||||
WiFi.softAP(("ESP-NOW switch " + String(ESP.getChipId(), HEX)).c_str(), "12345678", 1, 1);
|
||||
webServer.end();
|
||||
}
|
||||
|
||||
void attributesMessageTimerCallback()
|
||||
|
Reference in New Issue
Block a user