Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
24ba0a09f4 | |||
eebaca38b7 | |||
8e9082f05e | |||
1c306e031b | |||
74c6279f50 |
@ -4,7 +4,7 @@ Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi.
|
||||
|
||||
## Features
|
||||
|
||||
1. Creates an access point named "ESP-NOW Gateway XXXXXXXXXXXX" with password "12345678" (IP 192.168.4.1).
|
||||
1. Creates an access point named "ESP-NOW gateway XXXXXXXXXXXX" with password "12345678" (IP 192.168.4.1).
|
||||
2. Possibility a device search through the Windows Network Environment via SSDP.
|
||||
3. Periodically transmission of system information to the MQTT broker (every 60 seconds) and availability status to the ESP-NOW network and to the MQTT broker (every 10 seconds).
|
||||
4. Automatically adds gateway configuration to Home Assistan via MQTT discovery as a binary_sensor.
|
||||
@ -21,8 +21,9 @@ Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi.
|
||||
## Attention
|
||||
|
||||
1. ESP-NOW network name must be set same of all another ESP-NOW devices in network.
|
||||
2. Upload the "data" folder (with web interface) into the filesystem before flashing.
|
||||
3. WiFi router must be set on channel 1.
|
||||
2. If encryption is used, the key 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. WiFi router must be set on channel 1.
|
||||
|
||||
## Tested on
|
||||
|
||||
|
@ -2,48 +2,52 @@
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
board_build.filesystem = littlefs
|
||||
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
|
||||
marvinroger/AsyncMqttClient@^0.9.0
|
||||
https://github.com/aZholtikov/Async-Web-Server
|
||||
https://github.com/bblanchon/ArduinoJson
|
||||
https://github.com/marvinroger/async-mqtt-client
|
||||
|
||||
[env:ESP8266-OTA]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
board_build.filesystem = littlefs
|
||||
upload_port = 192.168.1.113
|
||||
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
|
||||
marvinroger/AsyncMqttClient@^0.9.0
|
||||
https://github.com/aZholtikov/Async-Web-Server
|
||||
https://github.com/bblanchon/ArduinoJson
|
||||
https://github.com/marvinroger/async-mqtt-client
|
||||
|
||||
[env:ESP32]
|
||||
platform = espressif32
|
||||
board = az-delivery-devkit-v4
|
||||
framework = arduino
|
||||
board_build.filesystem = littlefs
|
||||
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
|
||||
marvinroger/AsyncMqttClient@^0.9.0
|
||||
luc-github/ESP32SSDP@^1.2.0
|
||||
https://github.com/aZholtikov/Async-Web-Server
|
||||
https://github.com/bblanchon/ArduinoJson
|
||||
https://github.com/marvinroger/async-mqtt-client
|
||||
https://github.com/luc-github/ESP32SSDP
|
||||
|
||||
[env:ESP32-OTA]
|
||||
platform = espressif32
|
||||
board = az-delivery-devkit-v4
|
||||
framework = arduino
|
||||
board_build.filesystem = littlefs
|
||||
upload_port = 192.168.1.110
|
||||
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
|
||||
marvinroger/AsyncMqttClient@^0.9.0
|
||||
luc-github/ESP32SSDP@^1.2.0
|
||||
https://github.com/aZholtikov/Async-Web-Server
|
||||
https://github.com/bblanchon/ArduinoJson
|
||||
https://github.com/marvinroger/async-mqtt-client
|
||||
https://github.com/luc-github/ESP32SSDP
|
||||
|
32
src/main.cpp
32
src/main.cpp
@ -1,7 +1,8 @@
|
||||
#include "ArduinoJson.h"
|
||||
#include "ArduinoOTA.h"
|
||||
#include "ESPAsyncWebServer.h"
|
||||
#include "ESPAsyncWebServer.h" // https://github.com/aZholtikov/Async-Web-Server
|
||||
#include "AsyncMQTTClient.h"
|
||||
#include "LittleFS.h"
|
||||
#include "Ticker.h"
|
||||
#include "ZHNetwork.h"
|
||||
#include "ZHConfig.h"
|
||||
@ -9,7 +10,6 @@
|
||||
#include "ESP8266SSDP.h"
|
||||
#endif
|
||||
#if defined(ESP32)
|
||||
#include "SPIFFS.h"
|
||||
#include "ESP32SSDP.h"
|
||||
#endif
|
||||
|
||||
@ -34,7 +34,7 @@ void setupWebServer(void);
|
||||
|
||||
void connectToMqtt(void);
|
||||
|
||||
const String firmware{"1.22"};
|
||||
const String firmware{"1.26"};
|
||||
|
||||
String espnowNetName{"DEFAULT"};
|
||||
|
||||
@ -72,7 +72,7 @@ void attributesMessageTimerCallback(void);
|
||||
|
||||
void setup()
|
||||
{
|
||||
SPIFFS.begin();
|
||||
LittleFS.begin();
|
||||
|
||||
loadConfig();
|
||||
|
||||
@ -90,6 +90,7 @@ void setup()
|
||||
WiFi.setAutoReconnect(true);
|
||||
|
||||
myNet.begin(espnowNetName.c_str(), true);
|
||||
// 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(onEspnowMessage);
|
||||
myNet.setOnUnicastReceivingCallback(onEspnowMessage);
|
||||
@ -184,7 +185,7 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
|
||||
jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
|
||||
jsonConfig["device_class"] = getValueName(json["class"].as<ha_switch_device_class_t>());
|
||||
jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state";
|
||||
jsonConfig["value_template"] = "{{ value_json.state }}";
|
||||
jsonConfig["value_template"] = "{{ value_json." + json["template"].as<String>() + " }}";
|
||||
jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set";
|
||||
jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
|
||||
jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
|
||||
@ -252,19 +253,18 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
|
||||
jsonConfig["name"] = json["name"];
|
||||
jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
|
||||
jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state";
|
||||
jsonConfig["value_template"] = "{{ value_json.state }}";
|
||||
jsonConfig["value_template"] = "{{ value_json." + json["template"].as<String>() + " }}";
|
||||
jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
|
||||
jsonConfig["force_update"] = "true";
|
||||
jsonConfig["qos"] = 2;
|
||||
jsonConfig["retain"] = "true";
|
||||
if (type == HACT_SENSOR)
|
||||
jsonConfig["device_class"] = getValueName(json["class"].as<ha_sensor_device_class_t>());
|
||||
if (type == HACT_BINARY_SENSOR)
|
||||
{
|
||||
ha_binary_sensor_device_class_t deviceClass = json["class"].as<ha_binary_sensor_device_class_t>();
|
||||
jsonConfig["device_class"] = getValueName(deviceClass);
|
||||
jsonConfig["device_class"] = getValueName(json["class"].as<ha_binary_sensor_device_class_t>());
|
||||
jsonConfig["payload_on"] = json["payload_on"];
|
||||
jsonConfig["payload_off"] = json["payload_off"];
|
||||
if (deviceClass == HABSDC_BATTERY)
|
||||
jsonConfig["value_template"] = "{{ value_json.battery }}";
|
||||
}
|
||||
char buffer[2048]{0};
|
||||
serializeJsonPretty(jsonConfig, buffer);
|
||||
@ -433,9 +433,9 @@ String getValue(String data, char separator, uint8_t index)
|
||||
|
||||
void loadConfig()
|
||||
{
|
||||
if (!SPIFFS.exists("/config.json"))
|
||||
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<1024> json;
|
||||
deserializeJson(json, jsonFile);
|
||||
@ -465,7 +465,7 @@ void saveConfig()
|
||||
json["mqttUserPassword"] = mqttUserPassword;
|
||||
json["topicPrefix"] = topicPrefix;
|
||||
json["system"] = "empty";
|
||||
File file = SPIFFS.open("/config.json", "w");
|
||||
File file = LittleFS.open("/config.json", "w");
|
||||
serializeJsonPretty(json, file);
|
||||
file.close();
|
||||
}
|
||||
@ -505,7 +505,7 @@ void setupWebServer()
|
||||
request->send(200, "text/xml", ssdpSend); });
|
||||
|
||||
webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(SPIFFS, "/index.htm"); });
|
||||
{ request->send(LittleFS, "/index.htm"); });
|
||||
|
||||
webServer.on("/setting", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
@ -528,8 +528,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");
|
||||
|
Reference in New Issue
Block a user