|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
#include "ArduinoJson.h"
|
|
|
|
|
#include "ArduinoOTA.h"
|
|
|
|
|
#include "ESPAsyncWebServer.h"
|
|
|
|
|
#include "ESPAsyncWebServer.h" // https://github.com/aZholtikov/Async-Web-Server
|
|
|
|
|
#include "LittleFS.h"
|
|
|
|
|
#include "Ticker.h"
|
|
|
|
|
#include "ZHNetwork.h"
|
|
|
|
|
#include "ZHConfig.h"
|
|
|
|
@ -30,20 +31,20 @@ typedef struct
|
|
|
|
|
|
|
|
|
|
std::vector<espnow_message_t> espnowMessage;
|
|
|
|
|
|
|
|
|
|
const String firmware{"1.13"};
|
|
|
|
|
const String firmware{"1.2"};
|
|
|
|
|
|
|
|
|
|
String espnowNetName{"DEFAULT"};
|
|
|
|
|
|
|
|
|
|
String deviceName = "ESP-NOW light " + String(ESP.getChipId(), HEX);
|
|
|
|
|
|
|
|
|
|
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};
|
|
|
|
|
|
|
|
|
|
bool ledStatus{false};
|
|
|
|
|
uint8_t brightness{255};
|
|
|
|
|
uint16_t temperature{255};
|
|
|
|
|
uint8_t red{255};
|
|
|
|
@ -54,9 +55,6 @@ bool wasMqttAvailable{false};
|
|
|
|
|
|
|
|
|
|
uint8_t gatewayMAC[6]{0};
|
|
|
|
|
|
|
|
|
|
const String payloadOn{"ON"};
|
|
|
|
|
const String payloadOff{"OFF"};
|
|
|
|
|
|
|
|
|
|
ZHNetwork myNet;
|
|
|
|
|
AsyncWebServer webServer(80);
|
|
|
|
|
|
|
|
|
@ -83,7 +81,7 @@ void setup()
|
|
|
|
|
{
|
|
|
|
|
analogWriteRange(255);
|
|
|
|
|
|
|
|
|
|
SPIFFS.begin();
|
|
|
|
|
LittleFS.begin();
|
|
|
|
|
|
|
|
|
|
loadConfig();
|
|
|
|
|
|
|
|
|
@ -151,7 +149,11 @@ void onBroadcastReceiving(const char *data, const uint8_t *sender)
|
|
|
|
|
{
|
|
|
|
|
wasMqttAvailable = temp;
|
|
|
|
|
if (temp)
|
|
|
|
|
{
|
|
|
|
|
sendConfigMessage();
|
|
|
|
|
sendAttributesMessage();
|
|
|
|
|
sendStatusMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
gatewayAvailabilityCheckTimer.once(15, gatewayAvailabilityCheckTimerCallback);
|
|
|
|
|
}
|
|
|
|
@ -168,7 +170,7 @@ void onUnicastReceiving(const char *data, const uint8_t *sender)
|
|
|
|
|
{
|
|
|
|
|
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"])
|
|
|
|
@ -198,6 +200,7 @@ void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool sta
|
|
|
|
|
{
|
|
|
|
|
espnow_message_t message = espnowMessage[i];
|
|
|
|
|
if (message.id == id)
|
|
|
|
|
{
|
|
|
|
|
if (status)
|
|
|
|
|
espnowMessage.erase(espnowMessage.begin() + i);
|
|
|
|
|
else
|
|
|
|
@ -205,14 +208,16 @@ void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool sta
|
|
|
|
|
message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
|
|
|
|
|
espnowMessage.at(i) = message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loadConfig()
|
|
|
|
|
{
|
|
|
|
|
if (!SPIFFS.exists("/config.json"))
|
|
|
|
|
ETS_GPIO_INTR_DISABLE();
|
|
|
|
|
if (!LittleFS.exists("/config.json"))
|
|
|
|
|
saveConfig();
|
|
|
|
|
File file = SPIFFS.open("/config.json", "r");
|
|
|
|
|
File file = LittleFS.open("/config.json", "r");
|
|
|
|
|
String jsonFile = file.readString();
|
|
|
|
|
StaticJsonDocument<512> json;
|
|
|
|
|
deserializeJson(json, jsonFile);
|
|
|
|
@ -231,10 +236,13 @@ void loadConfig()
|
|
|
|
|
green = json["green"];
|
|
|
|
|
blue = json["blue"];
|
|
|
|
|
file.close();
|
|
|
|
|
delay(50);
|
|
|
|
|
ETS_GPIO_INTR_ENABLE();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void saveConfig()
|
|
|
|
|
{
|
|
|
|
|
ETS_GPIO_INTR_DISABLE();
|
|
|
|
|
StaticJsonDocument<512> json;
|
|
|
|
|
json["firmware"] = firmware;
|
|
|
|
|
json["espnowNetName"] = espnowNetName;
|
|
|
|
@ -252,15 +260,17 @@ void saveConfig()
|
|
|
|
|
json["green"] = green;
|
|
|
|
|
json["blue"] = blue;
|
|
|
|
|
json["system"] = "empty";
|
|
|
|
|
File file = SPIFFS.open("/config.json", "w");
|
|
|
|
|
File file = LittleFS.open("/config.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("/setting", HTTP_GET, [](AsyncWebServerRequest *request)
|
|
|
|
|
{
|
|
|
|
@ -282,8 +292,8 @@ void setupWebServer()
|
|
|
|
|
|
|
|
|
|
webServer.onNotFound([](AsyncWebServerRequest *request)
|
|
|
|
|
{
|
|
|
|
|
if (SPIFFS.exists(request->url()))
|
|
|
|
|
request->send(SPIFFS, request->url());
|
|
|
|
|
if (LittleFS.exists(request->url()))
|
|
|
|
|
request->send(LittleFS, request->url());
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
request->send(404, "text/plain", "File Not Found");
|
|
|
|
@ -337,12 +347,10 @@ void sendConfigMessage()
|
|
|
|
|
esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_CONFIG};
|
|
|
|
|
espnow_message_t message;
|
|
|
|
|
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;
|
|
|
|
|
json[MCMT_DEVICE_NAME] = deviceName;
|
|
|
|
|
json[MCMT_DEVICE_UNIT] = 1;
|
|
|
|
|
json[MCMT_COMPONENT_TYPE] = HACT_LIGHT;
|
|
|
|
|
json[MCMT_DEVICE_CLASS] = ledType;
|
|
|
|
|
serializeJsonPretty(json, outgoingData.message);
|
|
|
|
|
memcpy(&message.message, &outgoingData, sizeof(esp_now_payload_data_t));
|
|
|
|
|
message.id = myNet.sendUnicastMessage(message.message, gatewayMAC, true);
|
|
|
|
@ -358,7 +366,7 @@ void sendStatusMessage()
|
|
|
|
|
esp_now_payload_data_t outgoingData{ENDT_LED, ENPT_STATE};
|
|
|
|
|
espnow_message_t message;
|
|
|
|
|
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
|
|
|
|
json["state"] = ledStatus ? payloadOn : payloadOff;
|
|
|
|
|
json["state"] = ledStatus ? "ON" : "OFF";
|
|
|
|
|
json["brightness"] = brightness;
|
|
|
|
|
json["temperature"] = temperature;
|
|
|
|
|
json["rgb"] = String(red) + "," + String(green) + "," + String(blue);
|
|
|
|
|