Version 1.2
Fixed some minor bugs. Added window/door sensor support.
This commit is contained in:
		| @@ -34,7 +34,7 @@ Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi. | |||||||
| 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) | 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) | 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) | 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) (coming soon) | 5. [ESP-NOW Water Leakage Sensor](https://github.com/aZholtikov/ESP-NOW-Water-Leakage-Sensor) (coming soon) | ||||||
|  |  | ||||||
| ## To Do | ## To Do | ||||||
|   | |||||||
							
								
								
									
										39
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								src/main.cpp
									
									
									
									
									
								
							| @@ -34,7 +34,7 @@ void setupWebServer(void); | |||||||
|  |  | ||||||
| void connectToMqtt(void); | void connectToMqtt(void); | ||||||
|  |  | ||||||
| const String firmware{"1.1"}; | const String firmware{"1.2"}; | ||||||
|  |  | ||||||
| String espnowNetName{"DEFAULT"}; | String espnowNetName{"DEFAULT"}; | ||||||
|  |  | ||||||
| @@ -165,12 +165,12 @@ void onEspnowMessage(const char *data, const uint8_t *sender) | |||||||
|             StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json; |             StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json; | ||||||
|             deserializeJson(json, configData.message); |             deserializeJson(json, configData.message); | ||||||
|             uint8_t unit = json["unit"].as<uint8_t>(); |             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; |             StaticJsonDocument<2048> jsonConfig; | ||||||
|             jsonConfig["platform"] = "mqtt"; |             jsonConfig["platform"] = "mqtt"; | ||||||
|             jsonConfig["name"] = json["name"]; |             jsonConfig["name"] = json["name"]; | ||||||
|             jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit; |             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["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state"; | ||||||
|             jsonConfig["value_template"] = "{{ value_json.state }}"; |             jsonConfig["value_template"] = "{{ value_json.state }}"; | ||||||
|             jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set"; |             jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set"; | ||||||
| @@ -183,7 +183,7 @@ void onEspnowMessage(const char *data, const uint8_t *sender) | |||||||
|             jsonConfig["retain"] = "true"; |             jsonConfig["retain"] = "true"; | ||||||
|             char buffer[2048]{0}; |             char buffer[2048]{0}; | ||||||
|             serializeJsonPretty(jsonConfig, buffer); |             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) |         if (incomingData.deviceType == ENDT_LED) | ||||||
|         { |         { | ||||||
| @@ -192,7 +192,7 @@ void onEspnowMessage(const char *data, const uint8_t *sender) | |||||||
|             StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json; |             StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json; | ||||||
|             deserializeJson(json, configData.message); |             deserializeJson(json, configData.message); | ||||||
|             uint8_t unit = json["unit"].as<uint8_t>(); |             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"]; |             esp_now_led_type_t ledClass = json["class"]; | ||||||
|             StaticJsonDocument<2048> jsonConfig; |             StaticJsonDocument<2048> jsonConfig; | ||||||
|             jsonConfig["platform"] = "mqtt"; |             jsonConfig["platform"] = "mqtt"; | ||||||
| @@ -225,7 +225,34 @@ void onEspnowMessage(const char *data, const uint8_t *sender) | |||||||
|             jsonConfig["retain"] = "true"; |             jsonConfig["retain"] = "true"; | ||||||
|             char buffer[2048]{0}; |             char buffer[2048]{0}; | ||||||
|             serializeJsonPretty(jsonConfig, buffer); |             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["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) |     if (incomingData.payloadsType == ENPT_FORWARD) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user