Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
fda640e713 | |||
648a19775e | |||
b500582cb8 | |||
![]() |
ed9cba4bff | ||
00576355ff | |||
17db726612 | |||
6fffb45489 | |||
3347c4d097 | |||
ed42021c9b | |||
02b8326b80 | |||
0795911f7a | |||
dc9f08e82c | |||
4d8146e64b | |||
39a13599c3 | |||
bcda1c9ea5 | |||
7f51f05eb8 |
11
README.md
11
README.md
@ -1,10 +1,10 @@
|
||||
# Light/led strip controller for ESP8266
|
||||
# ESP-NOW light/led strip controller for ESP8266
|
||||
|
||||
ESP-NOW based light/led strip controller for ESP8266. Alternate firmware for Tuya/SmartLife WiFi light/led strip controllers.
|
||||
|
||||
## Features
|
||||
|
||||
1. After turn on (or after rebooting) creates an access point named "ESP-NOW Light 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 light 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 light/led strip configuration to Home Assistan via MQTT discovery as a light.
|
||||
@ -25,5 +25,8 @@ See [here](https://github.com/aZholtikov/ESP-NOW-Light-Led-Strip/tree/main/hardw
|
||||
|
||||
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 light/led strip controllers, 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 light/led strip controllers, the WiFi module must be replaced with an ESP8266 compatible module (if necessary).
|
||||
|
||||
Any feedback via [e-mail](mailto:github@zh.com.ru) would be appreciated. Or... [Buy me a coffee](https://paypal.me/aZholtikov).
|
||||
|
@ -10,7 +10,7 @@ function createXmlHttpObject() {
|
||||
|
||||
function load() {
|
||||
if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {
|
||||
xmlHttp.open('PUT', '/config.json', true);
|
||||
xmlHttp.open('GET', '/config', true);
|
||||
xmlHttp.send(null);
|
||||
xmlHttp.onload = function () {
|
||||
jsonResponse = JSON.parse(xmlHttp.responseText);
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
<body onload="load();">
|
||||
<form class="box">
|
||||
<h1>ESP-NOW Light </h1>
|
||||
<h1>ESP-NOW Light</h1>
|
||||
<div class="wrapper">
|
||||
<p class="text">Firmware:</p>
|
||||
<p class="text" id="version"></p>
|
||||
|
@ -2,20 +2,26 @@
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
framework = arduino
|
||||
build_flags = -D PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK305
|
||||
board_build.filesystem = littlefs
|
||||
board_build.ldscript = eagle.flash.4m1m.ld
|
||||
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
|
||||
https://github.com/aZholtikov/Async-Web-Server
|
||||
https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
[env:ESP-12E-OTA]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
framework = arduino
|
||||
build_flags = -D PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK305
|
||||
board_build.filesystem = littlefs
|
||||
board_build.ldscript = eagle.flash.4m1m.ld
|
||||
upload_port = 192.168.4.1
|
||||
upload_protocol = espota
|
||||
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
|
||||
https://github.com/aZholtikov/Async-Web-Server
|
||||
https://github.com/bblanchon/ArduinoJson
|
385
src/main.cpp
385
src/main.cpp
@ -1,16 +1,20 @@
|
||||
#include "ArduinoJson.h"
|
||||
#include "ArduinoOTA.h"
|
||||
#include "ESPAsyncWebServer.h"
|
||||
#include "ESPAsyncWebServer.h" // https://github.com/aZholtikov/Async-Web-Server
|
||||
#include "LittleFS.h"
|
||||
#include "EEPROM.h"
|
||||
#include "Ticker.h"
|
||||
#include "ZHNetwork.h"
|
||||
#include "ZHConfig.h"
|
||||
|
||||
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);
|
||||
void loadStatus(void);
|
||||
void saveStatus(void);
|
||||
void setupWebServer(void);
|
||||
|
||||
void sendAttributesMessage(void);
|
||||
@ -18,24 +22,33 @@ void sendKeepAliveMessage(void);
|
||||
void sendConfigMessage(void);
|
||||
void sendStatusMessage(void);
|
||||
|
||||
String getValue(String data, char separator, byte index);
|
||||
String getValue(String data, char separator, uint8_t index);
|
||||
|
||||
void changeLedState(void);
|
||||
|
||||
const String firmware{"1.11"};
|
||||
typedef struct
|
||||
{
|
||||
uint16_t id{0};
|
||||
char message[200]{0};
|
||||
} espnow_message_t;
|
||||
|
||||
String espnowNetName{"DEFAULT"};
|
||||
struct deviceConfig
|
||||
{
|
||||
String espnowNetName{"DEFAULT"};
|
||||
String deviceName = "ESP-NOW light " + String(ESP.getChipId(), HEX);
|
||||
uint8_t ledType{ENLT_NONE};
|
||||
uint8_t coldWhitePin{0};
|
||||
uint8_t warmWhitePin{0};
|
||||
uint8_t redPin{0};
|
||||
uint8_t greenPin{0};
|
||||
uint8_t bluePin{0};
|
||||
} config;
|
||||
|
||||
String deviceName = "ESP-NOW light " + String(ESP.getChipId(), HEX);
|
||||
std::vector<espnow_message_t> espnowMessage;
|
||||
|
||||
const String firmware{"1.21"};
|
||||
|
||||
uint8_t ledType{ENLT_NONE};
|
||||
bool ledStatus{false};
|
||||
uint8_t coldWhitePin{0};
|
||||
uint8_t warmWhitePin{0};
|
||||
uint8_t redPin{0};
|
||||
uint8_t greenPin{0};
|
||||
uint8_t bluePin{0};
|
||||
|
||||
uint8_t brightness{255};
|
||||
uint16_t temperature{255};
|
||||
uint8_t red{255};
|
||||
@ -46,9 +59,6 @@ bool wasMqttAvailable{false};
|
||||
|
||||
uint8_t gatewayMAC[6]{0};
|
||||
|
||||
const String payloadOn{"ON"};
|
||||
const String payloadOff{"OFF"};
|
||||
|
||||
ZHNetwork myNet;
|
||||
AsyncWebServer webServer(80);
|
||||
|
||||
@ -62,47 +72,40 @@ 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()
|
||||
{
|
||||
analogWriteRange(255);
|
||||
|
||||
SPIFFS.begin();
|
||||
LittleFS.begin();
|
||||
|
||||
loadConfig();
|
||||
loadStatus();
|
||||
|
||||
if (coldWhitePin)
|
||||
pinMode(coldWhitePin, OUTPUT);
|
||||
if (warmWhitePin)
|
||||
pinMode(warmWhitePin, OUTPUT);
|
||||
if (redPin)
|
||||
pinMode(redPin, OUTPUT);
|
||||
if (greenPin)
|
||||
pinMode(greenPin, OUTPUT);
|
||||
if (bluePin)
|
||||
pinMode(bluePin, OUTPUT);
|
||||
if (config.coldWhitePin)
|
||||
pinMode(config.coldWhitePin, OUTPUT);
|
||||
if (config.warmWhitePin)
|
||||
pinMode(config.warmWhitePin, OUTPUT);
|
||||
if (config.redPin)
|
||||
pinMode(config.redPin, OUTPUT);
|
||||
if (config.greenPin)
|
||||
pinMode(config.greenPin, OUTPUT);
|
||||
if (config.bluePin)
|
||||
pinMode(config.bluePin, OUTPUT);
|
||||
|
||||
changeLedState();
|
||||
|
||||
WiFi.setSleepMode(WIFI_NONE_SLEEP);
|
||||
myNet.begin(espnowNetName.c_str());
|
||||
myNet.begin(config.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);
|
||||
@ -133,42 +136,46 @@ void loop()
|
||||
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;
|
||||
memcpy(&incomingData, data, sizeof(esp_now_payload_data_t));
|
||||
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;
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||
deserializeJson(json, incomingData.message);
|
||||
bool temp = json["MQTT"] == "online" ? true : false;
|
||||
if (wasMqttAvailable != temp)
|
||||
{
|
||||
wasMqttAvailable = temp;
|
||||
if (temp)
|
||||
{
|
||||
sendConfigMessage();
|
||||
sendAttributesMessage();
|
||||
sendStatusMessage();
|
||||
}
|
||||
}
|
||||
gatewayAvailabilityCheckTimer.once(15, gatewayAvailabilityCheckTimerCallback);
|
||||
}
|
||||
}
|
||||
|
||||
void onUnicastReceiving(const char *data, const byte *sender)
|
||||
void onUnicastReceiving(const char *data, const uint8_t *sender)
|
||||
{
|
||||
esp_now_payload_data_t incomingData;
|
||||
memcpy(&incomingData, data, sizeof(esp_now_payload_data_t));
|
||||
if (incomingData.deviceType != ENDT_GATEWAY || myNet.macToString(gatewayMAC) != myNet.macToString(sender))
|
||||
return;
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||
if (incomingData.payloadsType == ENPT_SET)
|
||||
{
|
||||
deserializeJson(json, incomingData.message);
|
||||
if (json["set"])
|
||||
ledStatus = json["set"] == payloadOn ? true : false;
|
||||
ledStatus = json["set"] == "ON" ? true : false;
|
||||
if (json["brightness"])
|
||||
brightness = json["brightness"];
|
||||
if (json["temperature"])
|
||||
@ -192,113 +199,138 @@ void onUnicastReceiving(const char *data, const byte *sender)
|
||||
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;
|
||||
attributesMessageResendTimer.detach();
|
||||
}
|
||||
if (keepAliveMessageResendTimerSemaphore)
|
||||
{
|
||||
keepAliveMessageResendTimerSemaphore = false;
|
||||
keepAliveMessageResendTimer.detach();
|
||||
}
|
||||
if (configMessageResendTimerSemaphore)
|
||||
{
|
||||
configMessageResendTimerSemaphore = false;
|
||||
configMessageResendTimer.detach();
|
||||
}
|
||||
if (statusMessageResendTimerSemaphore)
|
||||
{
|
||||
statusMessageResendTimerSemaphore = false;
|
||||
statusMessageResendTimer.detach();
|
||||
if (status)
|
||||
espnowMessage.erase(espnowMessage.begin() + i);
|
||||
else
|
||||
{
|
||||
message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
|
||||
espnowMessage.at(i) = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void loadConfig()
|
||||
{
|
||||
if (!SPIFFS.exists("/config.json"))
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
EEPROM.begin(4096);
|
||||
if (EEPROM.read(4095) == 254)
|
||||
{
|
||||
EEPROM.get(0, config);
|
||||
EEPROM.end();
|
||||
}
|
||||
else
|
||||
{
|
||||
EEPROM.end();
|
||||
saveConfig();
|
||||
File file = SPIFFS.open("/config.json", "r");
|
||||
}
|
||||
delay(50);
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
|
||||
void saveConfig()
|
||||
{
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
EEPROM.begin(4096);
|
||||
EEPROM.write(4095, 254);
|
||||
EEPROM.put(0, config);
|
||||
EEPROM.end();
|
||||
delay(50);
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
|
||||
void loadStatus()
|
||||
{
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
if (!LittleFS.exists("/status.json"))
|
||||
saveStatus();
|
||||
File file = LittleFS.open("/status.json", "r");
|
||||
String jsonFile = file.readString();
|
||||
StaticJsonDocument<512> json;
|
||||
DynamicJsonDocument json(192); // To calculate the buffer size uses https://arduinojson.org/v6/assistant.
|
||||
deserializeJson(json, jsonFile);
|
||||
espnowNetName = json["espnowNetName"].as<String>();
|
||||
deviceName = json["deviceName"].as<String>();
|
||||
ledType = json["ledType"];
|
||||
ledStatus = json["ledStatus"];
|
||||
coldWhitePin = json["coldWhitePin"];
|
||||
warmWhitePin = json["warmWhitePin"];
|
||||
redPin = json["redPin"];
|
||||
greenPin = json["greenPin"];
|
||||
bluePin = json["bluePin"];
|
||||
ledStatus = json["status"];
|
||||
brightness = json["brightness"];
|
||||
temperature = json["temperature"];
|
||||
red = json["red"];
|
||||
green = json["green"];
|
||||
blue = json["blue"];
|
||||
file.close();
|
||||
delay(50);
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
|
||||
void saveConfig()
|
||||
void saveStatus()
|
||||
{
|
||||
StaticJsonDocument<512> json;
|
||||
json["firmware"] = firmware;
|
||||
json["espnowNetName"] = espnowNetName;
|
||||
json["deviceName"] = deviceName;
|
||||
json["ledType"] = ledType;
|
||||
json["ledStatus"] = ledStatus;
|
||||
json["coldWhitePin"] = coldWhitePin;
|
||||
json["warmWhitePin"] = warmWhitePin;
|
||||
json["redPin"] = redPin;
|
||||
json["greenPin"] = greenPin;
|
||||
json["bluePin"] = bluePin;
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
DynamicJsonDocument json(128); // To calculate the buffer size uses https://arduinojson.org/v6/assistant.
|
||||
json["status"] = ledStatus;
|
||||
json["brightness"] = brightness;
|
||||
json["temperature"] = temperature;
|
||||
json["red"] = red;
|
||||
json["green"] = green;
|
||||
json["blue"] = blue;
|
||||
json["system"] = "empty";
|
||||
File file = SPIFFS.open("/config.json", "w");
|
||||
File file = LittleFS.open("/status.json", "w");
|
||||
serializeJsonPretty(json, file);
|
||||
file.close();
|
||||
delay(50);
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
|
||||
void setupWebServer()
|
||||
{
|
||||
webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(SPIFFS, "/index.htm"); });
|
||||
{ request->send(LittleFS, "/index.htm"); });
|
||||
|
||||
webServer.on("/function.js", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(LittleFS, "/function.js"); });
|
||||
|
||||
webServer.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(LittleFS, "/style.css"); });
|
||||
|
||||
webServer.on("/setting", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
ledType = request->getParam("ledType")->value().toInt();
|
||||
coldWhitePin = request->getParam("coldWhitePin")->value().toInt();
|
||||
warmWhitePin = request->getParam("warmWhitePin")->value().toInt();
|
||||
redPin = request->getParam("redPin")->value().toInt();
|
||||
greenPin = request->getParam("greenPin")->value().toInt();
|
||||
bluePin = request->getParam("bluePin")->value().toInt();
|
||||
deviceName = request->getParam("deviceName")->value();
|
||||
espnowNetName = request->getParam("espnowNetName")->value();
|
||||
config.ledType = request->getParam("ledType")->value().toInt();
|
||||
config.coldWhitePin = request->getParam("coldWhitePin")->value().toInt();
|
||||
config.warmWhitePin = request->getParam("warmWhitePin")->value().toInt();
|
||||
config.redPin = request->getParam("redPin")->value().toInt();
|
||||
config.greenPin = request->getParam("greenPin")->value().toInt();
|
||||
config.bluePin = request->getParam("bluePin")->value().toInt();
|
||||
config.deviceName = request->getParam("deviceName")->value();
|
||||
config.espnowNetName = request->getParam("espnowNetName")->value();
|
||||
request->send(200);
|
||||
saveConfig(); });
|
||||
|
||||
webServer.on("/config", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
String configJson;
|
||||
DynamicJsonDocument json(384); // To calculate the buffer size uses https://arduinojson.org/v6/assistant.
|
||||
json["firmware"] = firmware;
|
||||
json["espnowNetName"] = config.espnowNetName;
|
||||
json["deviceName"] = config.deviceName;
|
||||
json["ledType"] = config.ledType;
|
||||
json["coldWhitePin"] = config.coldWhitePin;
|
||||
json["warmWhitePin"] = config.warmWhitePin;
|
||||
json["redPin"] = config.redPin;
|
||||
json["greenPin"] = config.greenPin;
|
||||
json["bluePin"] = config.bluePin;
|
||||
serializeJsonPretty(json, configJson);
|
||||
request->send(200, "application/json", configJson); });
|
||||
|
||||
webServer.on("/restart", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
request->send(200);
|
||||
ESP.restart(); });
|
||||
|
||||
webServer.onNotFound([](AsyncWebServerRequest *request)
|
||||
{
|
||||
if (SPIFFS.exists(request->url()))
|
||||
request->send(SPIFFS, request->url());
|
||||
else
|
||||
{
|
||||
request->send(404, "text/plain", "File Not Found");
|
||||
} });
|
||||
{ request->send(404, "text/plain", "File Not Found"); });
|
||||
|
||||
webServer.begin();
|
||||
}
|
||||
@ -313,22 +345,19 @@ void sendAttributesMessage()
|
||||
uint32_t hours = mins / 60;
|
||||
uint32_t days = hours / 24;
|
||||
esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_ATTRIBUTES};
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
espnow_message_t message;
|
||||
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||
json["Type"] = "ESP-NOW light";
|
||||
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 +366,11 @@ void sendKeepAliveMessage()
|
||||
return;
|
||||
keepAliveMessageTimerSemaphore = false;
|
||||
esp_now_payload_data_t outgoingData{ENDT_LED, 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,22 +378,17 @@ void sendConfigMessage()
|
||||
if (!isGatewayAvailable)
|
||||
return;
|
||||
esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_CONFIG};
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
json["name"] = deviceName;
|
||||
json["unit"] = 1;
|
||||
json["type"] = HACT_LIGHT;
|
||||
json["class"] = ledType;
|
||||
json["payload_on"] = payloadOn;
|
||||
json["payload_off"] = payloadOff;
|
||||
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);
|
||||
espnow_message_t message;
|
||||
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||
json[MCMT_DEVICE_NAME] = config.deviceName;
|
||||
json[MCMT_DEVICE_UNIT] = 1;
|
||||
json[MCMT_COMPONENT_TYPE] = HACT_LIGHT;
|
||||
json[MCMT_DEVICE_CLASS] = config.ledType;
|
||||
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(1, sendConfigMessage);
|
||||
espnowMessage.push_back(message);
|
||||
}
|
||||
|
||||
void sendStatusMessage()
|
||||
@ -374,28 +397,25 @@ void sendStatusMessage()
|
||||
return;
|
||||
statusMessageTimerSemaphore = false;
|
||||
esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_STATE};
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
json["state"] = ledStatus ? payloadOn : payloadOff;
|
||||
espnow_message_t message;
|
||||
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||
json["state"] = ledStatus ? "ON" : "OFF";
|
||||
json["brightness"] = brightness;
|
||||
json["temperature"] = temperature;
|
||||
json["rgb"] = String(red) + "," + String(green) + "," + String(blue);
|
||||
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);
|
||||
|
||||
statusMessageResendTimerSemaphore = true;
|
||||
statusMessageResendTimer.once(1, sendStatusMessage);
|
||||
espnowMessage.push_back(message);
|
||||
}
|
||||
|
||||
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 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)
|
||||
{
|
||||
found++;
|
||||
@ -411,67 +431,68 @@ void changeLedState(void)
|
||||
{
|
||||
if (red == 255 && green == 255 && blue == 255)
|
||||
{
|
||||
if (ledType == ENLT_W || ledType == ENLT_RGBW)
|
||||
analogWrite(coldWhitePin, brightness);
|
||||
if (ledType == ENLT_WW || ledType == ENLT_RGBWW)
|
||||
if (config.ledType == ENLT_W || config.ledType == ENLT_RGBW)
|
||||
analogWrite(config.coldWhitePin, brightness);
|
||||
if (config.ledType == ENLT_WW || config.ledType == ENLT_RGBWW)
|
||||
{
|
||||
analogWrite(coldWhitePin, map(brightness, 0, 255, 0, map(temperature, 500, 153, 0, 255)));
|
||||
analogWrite(warmWhitePin, map(brightness, 0, 255, 0, map(temperature, 153, 500, 0, 255)));
|
||||
analogWrite(config.coldWhitePin, map(brightness, 0, 255, 0, map(temperature, 500, 153, 0, 255)));
|
||||
analogWrite(config.warmWhitePin, map(brightness, 0, 255, 0, map(temperature, 153, 500, 0, 255)));
|
||||
}
|
||||
if (ledType == ENLT_RGB)
|
||||
if (config.ledType == ENLT_RGB)
|
||||
{
|
||||
analogWrite(redPin, map(red, 0, 255, 0, brightness));
|
||||
analogWrite(greenPin, map(green, 0, 255, 0, brightness));
|
||||
analogWrite(bluePin, map(blue, 0, 255, 0, brightness));
|
||||
analogWrite(config.redPin, map(red, 0, 255, 0, brightness));
|
||||
analogWrite(config.greenPin, map(green, 0, 255, 0, brightness));
|
||||
analogWrite(config.bluePin, map(blue, 0, 255, 0, brightness));
|
||||
}
|
||||
if (ledType == ENLT_RGBW || ledType == ENLT_RGBWW)
|
||||
if (config.ledType == ENLT_RGBW || config.ledType == ENLT_RGBWW)
|
||||
{
|
||||
digitalWrite(redPin, LOW);
|
||||
digitalWrite(greenPin, LOW);
|
||||
digitalWrite(bluePin, LOW);
|
||||
digitalWrite(config.redPin, LOW);
|
||||
digitalWrite(config.greenPin, LOW);
|
||||
digitalWrite(config.bluePin, LOW);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ledType == ENLT_W)
|
||||
analogWrite(coldWhitePin, brightness);
|
||||
if (ledType == ENLT_WW)
|
||||
if (config.ledType == ENLT_W)
|
||||
analogWrite(config.coldWhitePin, brightness);
|
||||
if (config.ledType == ENLT_WW)
|
||||
{
|
||||
analogWrite(coldWhitePin, map(brightness, 0, 255, 0, map(temperature, 500, 153, 0, 255)));
|
||||
analogWrite(warmWhitePin, map(brightness, 0, 255, 0, map(temperature, 153, 500, 0, 255)));
|
||||
analogWrite(config.coldWhitePin, map(brightness, 0, 255, 0, map(temperature, 500, 153, 0, 255)));
|
||||
analogWrite(config.warmWhitePin, map(brightness, 0, 255, 0, map(temperature, 153, 500, 0, 255)));
|
||||
}
|
||||
if (ledType == ENLT_RGBW || ledType == ENLT_RGBWW)
|
||||
digitalWrite(coldWhitePin, LOW);
|
||||
if (ledType == ENLT_RGBWW)
|
||||
digitalWrite(warmWhitePin, LOW);
|
||||
if (ledType == ENLT_RGB || ledType == ENLT_RGBW || ledType == ENLT_RGBWW)
|
||||
if (config.ledType == ENLT_RGBW || config.ledType == ENLT_RGBWW)
|
||||
digitalWrite(config.coldWhitePin, LOW);
|
||||
if (config.ledType == ENLT_RGBWW)
|
||||
digitalWrite(config.warmWhitePin, LOW);
|
||||
if (config.ledType == ENLT_RGB || config.ledType == ENLT_RGBW || config.ledType == ENLT_RGBWW)
|
||||
{
|
||||
analogWrite(redPin, map(red, 0, 255, 0, brightness));
|
||||
analogWrite(greenPin, map(green, 0, 255, 0, brightness));
|
||||
analogWrite(bluePin, map(blue, 0, 255, 0, brightness));
|
||||
analogWrite(config.redPin, map(red, 0, 255, 0, brightness));
|
||||
analogWrite(config.greenPin, map(green, 0, 255, 0, brightness));
|
||||
analogWrite(config.bluePin, map(blue, 0, 255, 0, brightness));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ledType == ENLT_W || ledType == ENLT_WW || ledType == ENLT_RGBW || ledType == ENLT_RGBWW)
|
||||
digitalWrite(coldWhitePin, LOW);
|
||||
if (ledType == ENLT_WW || ledType == ENLT_RGBWW)
|
||||
digitalWrite(warmWhitePin, LOW);
|
||||
if (ledType == ENLT_RGB || ledType == ENLT_RGBW || ledType == ENLT_RGBWW)
|
||||
if (config.ledType == ENLT_W || config.ledType == ENLT_WW || config.ledType == ENLT_RGBW || config.ledType == ENLT_RGBWW)
|
||||
digitalWrite(config.coldWhitePin, LOW);
|
||||
if (config.ledType == ENLT_WW || config.ledType == ENLT_RGBWW)
|
||||
digitalWrite(config.warmWhitePin, LOW);
|
||||
if (config.ledType == ENLT_RGB || config.ledType == ENLT_RGBW || config.ledType == ENLT_RGBWW)
|
||||
{
|
||||
digitalWrite(redPin, LOW);
|
||||
digitalWrite(greenPin, LOW);
|
||||
digitalWrite(bluePin, LOW);
|
||||
digitalWrite(config.redPin, LOW);
|
||||
digitalWrite(config.greenPin, LOW);
|
||||
digitalWrite(config.bluePin, LOW);
|
||||
}
|
||||
}
|
||||
saveConfig();
|
||||
saveStatus();
|
||||
}
|
||||
|
||||
void gatewayAvailabilityCheckTimerCallback()
|
||||
{
|
||||
isGatewayAvailable = false;
|
||||
memset(gatewayMAC, 0, 6);
|
||||
memset(&gatewayMAC, 0, 6);
|
||||
espnowMessage.clear();
|
||||
}
|
||||
|
||||
void apModeHideTimerCallback()
|
||||
|
Loading…
x
Reference in New Issue
Block a user