From 9a86f806e4fab5e3457f58ce9a620cd946266fa7 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Wed, 4 Jan 2023 14:02:05 +0300 Subject: [PATCH] Version 1.1 To Do item 1 performed. --- README.md | 11 ++++++----- platformio.ini | 2 +- src/main.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c213bdd..1b505c1 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,9 @@ Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi. 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. -5. Possibility firmware update over OTA. -6. Web interface for settings. +5. Automatically adds supported ESP-NOW devices configurations to Home Assistan via MQTT discovery. +6. Possibility firmware update over OTA. +7. Web interface for settings. ## Notes @@ -31,14 +32,14 @@ Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi. ## Supported devices 1. [RF - Gateway](https://github.com/aZholtikov/RF-Gateway) (coming soon) -2. [ESP-NOW Switch](https://github.com/aZholtikov/ESP-NOW-Switch) (coming soon) -3. [ESP-NOW Led Light/Strip](https://github.com/aZholtikov/ESP-NOW-Led-Light-Strip) (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) ## To Do -- [ ] Automatically add ESP-NOW devices configurations to Home Assistan via MQTT discovery. +- [X] Automatically add ESP-NOW devices configurations to Home Assistan via MQTT discovery. - [ ] LAN connection support. - [ ] nRF24 device support (in current time uses "RF Gateway"). - [ ] BLE device support (for ESP32). diff --git a/platformio.ini b/platformio.ini index f48b1e3..f46437f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -38,7 +38,7 @@ lib_deps = platform = espressif32 board = az-delivery-devkit-v4 framework = arduino -upload_port = 192.168.1.144 +upload_port = 192.168.1.143 upload_protocol = espota lib_deps = https://github.com/aZholtikov/ZHNetwork diff --git a/src/main.cpp b/src/main.cpp index cae893b..06f156a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,7 +34,7 @@ void setupWebServer(void); void connectToMqtt(void); -const String firmware{"1.01"}; +const String firmware{"1.1"}; String espnowNetName{"DEFAULT"}; @@ -157,6 +157,7 @@ void onEspnowMessage(const char *data, const uint8_t *sender) if (incomingData.payloadsType == ENPT_STATE) mqttClient.publish((topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/" + getValueName(incomingData.payloadsType)).c_str(), 2, true, incomingData.message); if (incomingData.payloadsType == ENPT_CONFIG) + { if (incomingData.deviceType == ENDT_SWITCH) { esp_now_payload_data_t configData; @@ -165,7 +166,7 @@ void onEspnowMessage(const char *data, const uint8_t *sender) deserializeJson(json, configData.message); uint8_t unit = json["unit"].as(); String type = json["type"]; - StaticJsonDocument<1024> jsonConfig; + StaticJsonDocument<2048> jsonConfig; jsonConfig["platform"] = "mqtt"; jsonConfig["name"] = json["name"]; jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit; @@ -180,10 +181,53 @@ void onEspnowMessage(const char *data, const uint8_t *sender) jsonConfig["optimistic"] = "false"; jsonConfig["qos"] = 2; jsonConfig["retain"] = "true"; - char buffer[1024]{0}; + char buffer[2048]{0}; serializeJsonPretty(jsonConfig, buffer); mqttClient.publish((topicPrefix + "/" + type + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), 2, true, buffer); } + if (incomingData.deviceType == ENDT_LED) + { + esp_now_payload_data_t configData; + memcpy(&configData.message, &incomingData.message, sizeof(esp_now_payload_data_t::message)); + StaticJsonDocument json; + deserializeJson(json, configData.message); + uint8_t unit = json["unit"].as(); + String type = json["type"]; + esp_now_led_type_t ledClass = json["class"]; + 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["state_value_template"] = "{{ value_json.state }}"; + jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set"; + jsonConfig["brightness_state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state"; + jsonConfig["brightness_value_template"] = "{{ value_json.brightness }}"; + jsonConfig["brightness_command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/brightness"; + if (ledClass == ENLT_RGB || ledClass == ENLT_RGBW || ledClass == ENLT_RGBWW) + { + jsonConfig["rgb_state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state"; + jsonConfig["rgb_value_template"] = "{{ value_json.rgb | join(',') }}"; + jsonConfig["rgb_command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/rgb"; + } + if (ledClass == ENLT_WW || ledClass == ENLT_RGBWW) + { + jsonConfig["color_temp_state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state"; + jsonConfig["color_temp_value_template"] = "{{ value_json.temperature }}"; + jsonConfig["color_temp_command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/temperature"; + } + 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["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); + } + } if (incomingData.payloadsType == ENPT_FORWARD) { esp_now_payload_data_t forwardData; @@ -295,6 +339,7 @@ void sendKeepAliveMessage() outgoingData.payloadsType = ENPT_KEEP_ALIVE; StaticJsonDocument json; json["MQTT"] = mqttClient.connected() ? "online" : "offline"; + json["frequency"] = 10; // For compatibility with the previous version. Will be removed in future releases. char buffer[sizeof(esp_now_payload_data_t::message)]{0}; serializeJsonPretty(json, buffer); memcpy(&outgoingData.message, &buffer, sizeof(esp_now_payload_data_t::message));