6 Commits
v1.1 ... v1.23

Author SHA1 Message Date
74c6279f50 Version 1.23
Added value template to config message.
2023-01-15 11:58:40 +03:00
8c3b51304e Version 1.22
Web interface minor redesign.
Main code minor changes.
2023-01-12 12:24:53 +03:00
7b60f9efb3 Readme update 2023-01-08 10:49:50 +03:00
06487167f4 Version 1.21
Fixed one minor bug.
2023-01-08 10:14:51 +03:00
219cf17855 Version 1.2
Fixed some minor bugs.
Added window/door sensor support.
2023-01-06 13:09:21 +03:00
3aca3b7bbd Minor changes 2023-01-05 12:34:42 +03:00
5 changed files with 96 additions and 30 deletions

View File

@ -16,7 +16,7 @@ Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi.
1. ESP-NOW mesh network based on the library [ZHNetwork](https://github.com/aZholtikov/ZHNetwork).
2. Regardless of the status of connections to WiFi or MQTT the device perform ESP-NOW node function.
3. For restart the device (without using the Web interface and only if MQTT connection established) send an "restart" command to the device's root topic (example - "homeassistant/gateway/70039F44BEF7").
3. For restart the device (without using the Web interface and only if MQTT connection established) send an "restart" command to the device's root topic (example - "homeassistant/espnow_gateway/70039F44BEF7").
## Attention
@ -26,16 +26,16 @@ Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi.
## Tested on
1. NodeMCU 1.0 (ESP-12E Module). Unstable work.
2. AZ-Delivery ESP-32 Dev Kit C V4. Stable work.
1. NodeMCU 1.0 (ESP-12E Module). ESP-NOW + WiFi mode. Unstable work.
2. AZ-Delivery ESP-32 Dev Kit C V4. ESP-NOW + WiFi mode. Stable work.
## Supported devices
1. [RF - Gateway](https://github.com/aZholtikov/RF-Gateway) (coming soon)
1. [RF Gateway](https://github.com/aZholtikov/RF-Gateway) (coming soon)
2. [ESP-NOW Switch](https://github.com/aZholtikov/ESP-NOW-Switch)
3. [ESP-NOW Light/Led Strip](https://github.com/aZholtikov/ESP-NOW-Light-Led-Strip)
4. [ESP-NOW Window/Door Sensor](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor) (coming soon)
5. [ESP-NOW Water Leakage Sensor](https://github.com/aZholtikov/ESP-NOW-Water-Leakage-Sensor) (coming soon)
4. [ESP-NOW Window/Door Sensor](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor)
5. [ESP-NOW Water Leakage Sensor](https://github.com/aZholtikov/ESP-NOW-Water-Leakage-Sensor)
## To Do

View File

@ -31,20 +31,20 @@
<p class="text">WiFi settings</p>
<div class="wrapper">
<input id="ssid" value="{{ssid}}" placeholder="SSID" label title="WiFi network name" />
<input class="text-inp" id="ssid" value="{{ssid}}" placeholder="SSID" label title="WiFi network name" />
<input id="password" value="{{password}}" onfocus="this.type='text'" type="password" placeholder="Password"
autocomplete="off" label title="WiFi password" />
</div>
<p class="text">MQTT settings</p>
<div class="wrapper">
<input id="mqttHostName" value="{{mqttHostName}}" placeholder="URL or IP" label
<input class="text-inp" id="mqttHostName" value="{{mqttHostName}}" placeholder="URL or IP" label
title="MQTT server URL or IP" />
<input id="mqttHostPort" value="{{mqttHostPort}}" placeholder="Port" label title="MQTT server port" />
</div>
<div class="wrapper">
<input id="mqttUserLogin" value="{{mqttUserLogin}}" placeholder="Login" label
<input class="text-inp" id="mqttUserLogin" value="{{mqttUserLogin}}" placeholder="Login" label
title="MQTT server user login" />
<input id="mqttUserPassword" value="{{mqttUserPassword}}" onfocus="this.type='text'" type="password"
placeholder="Password" autocomplete="off" label title="MQTT server user password" />

View File

@ -1,3 +1,7 @@
p{
margin: 0 0;
}
body {
font-family: "Gill Sans", sans-serif;
background: rgb(255, 255, 255);
@ -21,6 +25,21 @@ h1 {
font-weight: 600;
flex-shrink: 0;
margin-right: 10px;
margin-left: 10px;
margin: 10px 0;
}
.text-inp {
width: 48%;
min-height: 30px;
border-radius: 5px;
border: none;
margin-bottom: 10px;
padding: 0 10px;
color: rgb(0, 0, 0);
background: #a3e0f1;
transition: .5s;
margin-left: 0;
}
.wrapper {
@ -39,6 +58,7 @@ input {
color: rgb(0, 0, 0);
background: #a3e0f1;
transition: .5s;
margin-left: 10px;
}
input:hover {
@ -51,6 +71,8 @@ input:hover {
background: rgb(65, 125, 238);
color: white;
transition: .5s;
margin-left: 0;
margin-top: 8px;
}
.btn:hover {
@ -65,6 +87,10 @@ input:hover {
width: 100%;
}
#espnowNetName {
margin-bottom: 10px;
}
.wrapper.wrapper--end {
align-items: baseline;
}

View File

@ -1,4 +1,4 @@
[env:esp8266]
[env:ESP8266]
platform = espressif8266
board = nodemcuv2
framework = arduino
@ -9,7 +9,7 @@ lib_deps =
me-no-dev/ESP Async WebServer@^1.2.3
marvinroger/AsyncMqttClient@^0.9.0
[env:esp8266-ota]
[env:ESP8266-OTA]
platform = espressif8266
board = nodemcuv2
framework = arduino
@ -22,7 +22,7 @@ lib_deps =
me-no-dev/ESP Async WebServer@^1.2.3
marvinroger/AsyncMqttClient@^0.9.0
[env:esp32]
[env:ESP32]
platform = espressif32
board = az-delivery-devkit-v4
framework = arduino
@ -34,11 +34,11 @@ lib_deps =
marvinroger/AsyncMqttClient@^0.9.0
luc-github/ESP32SSDP@^1.2.0
[env:esp32-ota]
[env:ESP32-OTA]
platform = espressif32
board = az-delivery-devkit-v4
framework = arduino
upload_port = 192.168.1.143
upload_port = 192.168.1.110
upload_protocol = espota
lib_deps =
https://github.com/aZholtikov/ZHNetwork

View File

@ -34,11 +34,16 @@ void setupWebServer(void);
void connectToMqtt(void);
const String firmware{"1.1"};
const String firmware{"1.23"};
String espnowNetName{"DEFAULT"};
String deviceName{"ESP-NOW gateway"};
#if defined(ESP8266)
String deviceName = "ESP-NOW gateway " + String(ESP.getChipId(), HEX);
#endif
#if defined(ESP32)
String deviceName = "ESP-NOW gateway " + String(ESP.getEfuseMac(), HEX);
#endif
String ssid{"SSID"};
String password{"PASSWORD"};
@ -72,6 +77,7 @@ void setup()
loadConfig();
WiFi.onEvent(onWifiEvent);
#if defined(ESP8266)
WiFi.setSleepMode(WIFI_NONE_SLEEP);
#endif
@ -88,7 +94,13 @@ void setup()
myNet.setOnBroadcastReceivingCallback(onEspnowMessage);
myNet.setOnUnicastReceivingCallback(onEspnowMessage);
WiFi.softAP(("ESP-NOW Gateway " + myNet.getNodeMac()).c_str(), "12345678");
#if defined(ESP8266)
WiFi.softAP(("ESP-NOW gateway " + String(ESP.getChipId(), HEX)).c_str(), "12345678");
#endif
#if defined(ESP32)
WiFi.softAP(("ESP-NOW gateway " + String(ESP.getEfuseMac(), HEX)).c_str(), "12345678");
#endif
uint8_t scan = WiFi.scanNetworks(false, false, 1);
String name;
int32_t rssi;
@ -165,25 +177,25 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
deserializeJson(json, configData.message);
uint8_t unit = json["unit"].as<uint8_t>();
String type = json["type"];
ha_component_type_t type = json["type"].as<ha_component_type_t>();
StaticJsonDocument<2048> jsonConfig;
jsonConfig["platform"] = "mqtt";
jsonConfig["name"] = json["name"];
jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
jsonConfig["device_class"] = json["class"];
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";
jsonConfig["payload_on"] = json["reverse"] == "true" ? "OFF" : "ON";
jsonConfig["payload_off"] = json["reverse"] == "true" ? "ON" : "OFF";
jsonConfig["payload_on"] = json["payload_on"];
jsonConfig["payload_off"] = json["payload_off"];
jsonConfig["optimistic"] = "false";
jsonConfig["qos"] = 2;
jsonConfig["retain"] = "true";
char buffer[2048]{0};
serializeJsonPretty(jsonConfig, buffer);
mqttClient.publish((topicPrefix + "/" + type + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), 2, true, buffer);
mqttClient.publish((topicPrefix + "/" + getValueName(type) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), 2, true, buffer);
}
if (incomingData.deviceType == ENDT_LED)
{
@ -192,7 +204,7 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
deserializeJson(json, configData.message);
uint8_t unit = json["unit"].as<uint8_t>();
String type = json["type"];
ha_component_type_t type = json["type"].as<ha_component_type_t>();
esp_now_led_type_t ledClass = json["class"];
StaticJsonDocument<2048> jsonConfig;
jsonConfig["platform"] = "mqtt";
@ -218,14 +230,42 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
}
jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
jsonConfig["payload_on"] = "ON";
jsonConfig["payload_off"] = "OFF";
jsonConfig["payload_on"] = json["payload_on"];
jsonConfig["payload_off"] = json["payload_off"];
jsonConfig["optimistic"] = "false";
jsonConfig["qos"] = 2;
jsonConfig["retain"] = "true";
char buffer[2048]{0};
serializeJsonPretty(jsonConfig, buffer);
mqttClient.publish((topicPrefix + "/" + type + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), 2, true, buffer);
mqttClient.publish((topicPrefix + "/" + getValueName(type) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), 2, true, buffer);
}
if (incomingData.deviceType == ENDT_SENSOR)
{
esp_now_payload_data_t configData;
memcpy(&configData.message, &incomingData.message, sizeof(esp_now_payload_data_t::message));
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
deserializeJson(json, configData.message);
uint8_t unit = json["unit"].as<uint8_t>();
ha_component_type_t type = json["type"].as<ha_component_type_t>();
StaticJsonDocument<2048> jsonConfig;
jsonConfig["platform"] = "mqtt";
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." + 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_BINARY_SENSOR)
{
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"];
}
char buffer[2048]{0};
serializeJsonPretty(jsonConfig, buffer);
mqttClient.publish((topicPrefix + "/" + getValueName(type) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), 2, true, buffer);
}
}
if (incomingData.payloadsType == ENPT_FORWARD)
@ -295,7 +335,7 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties
if (String(topic) == topicPrefix + "/espnow_switch/" + mac + "/set" || String(topic) == topicPrefix + "/espnow_led/" + mac + "/set")
{
flag = true;
json["set"] = message == "ON" ? "ON" : "OFF";
json["set"] = message;
}
if (String(topic) == topicPrefix + "/espnow_led/" + mac + "/brightness")
{
@ -356,7 +396,7 @@ void sendAttributesMessage()
uint32_t hours = mins / 60;
uint32_t days = hours / 24;
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
json["Type"] = "ESP-NOW Gateway";
json["Type"] = "ESP-NOW gateway";
#if defined(ESP8266)
json["MCU"] = "ESP8266";
#endif
@ -449,7 +489,7 @@ void setupWebServer()
ssdpDescription += xmlNode("friendlyName", deviceName);
ssdpDescription += xmlNode("presentationURL", "/");
ssdpDescription += xmlNode("serialNumber", "0000000" + String(random(1000)));
ssdpDescription += xmlNode("modelName", "ESP-NOW Gateway");
ssdpDescription += xmlNode("modelName", "ESP-NOW gateway");
ssdpDescription += xmlNode("modelNumber", firmware);
ssdpDescription += xmlNode("modelURL", "https://github.com/aZholtikov/ESP-NOW-Gateway");
ssdpDescription += xmlNode("manufacturer", "Alexey Zholtikov");