Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
1c306e031b | |||
74c6279f50 |
@ -4,7 +4,7 @@ Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi.
|
|||||||
|
|
||||||
## Features
|
## 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.
|
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).
|
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.
|
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
|
## Attention
|
||||||
|
|
||||||
1. ESP-NOW network name must be set same of all another ESP-NOW devices in network.
|
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.
|
2. If encryption is used, the key must be set same of all another ESP-NOW devices in network.
|
||||||
3. WiFi router must be set on channel 1.
|
3. Upload the "data" folder (with web interface) into the filesystem before flashing.
|
||||||
|
4. WiFi router must be set on channel 1.
|
||||||
|
|
||||||
## Tested on
|
## Tested on
|
||||||
|
|
||||||
|
12
src/main.cpp
12
src/main.cpp
@ -34,7 +34,7 @@ void setupWebServer(void);
|
|||||||
|
|
||||||
void connectToMqtt(void);
|
void connectToMqtt(void);
|
||||||
|
|
||||||
const String firmware{"1.22"};
|
const String firmware{"1.24"};
|
||||||
|
|
||||||
String espnowNetName{"DEFAULT"};
|
String espnowNetName{"DEFAULT"};
|
||||||
|
|
||||||
@ -90,6 +90,7 @@ void setup()
|
|||||||
WiFi.setAutoReconnect(true);
|
WiFi.setAutoReconnect(true);
|
||||||
|
|
||||||
myNet.begin(espnowNetName.c_str(), 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.setOnBroadcastReceivingCallback(onEspnowMessage);
|
||||||
myNet.setOnUnicastReceivingCallback(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["unique_id"] = myNet.macToString(sender) + "-" + unit;
|
||||||
jsonConfig["device_class"] = getValueName(json["class"].as<ha_switch_device_class_t>());
|
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." + json["template"].as<String>() + " }}";
|
||||||
jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set";
|
jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set";
|
||||||
jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
|
jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
|
||||||
jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
|
jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
|
||||||
@ -252,19 +253,16 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
|
|||||||
jsonConfig["name"] = json["name"];
|
jsonConfig["name"] = json["name"];
|
||||||
jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
|
jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
|
||||||
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." + json["template"].as<String>() + " }}";
|
||||||
jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
|
jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
|
||||||
jsonConfig["force_update"] = "true";
|
jsonConfig["force_update"] = "true";
|
||||||
jsonConfig["qos"] = 2;
|
jsonConfig["qos"] = 2;
|
||||||
jsonConfig["retain"] = "true";
|
jsonConfig["retain"] = "true";
|
||||||
if (type == HACT_BINARY_SENSOR)
|
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(json["class"].as<ha_binary_sensor_device_class_t>());
|
||||||
jsonConfig["device_class"] = getValueName(deviceClass);
|
|
||||||
jsonConfig["payload_on"] = json["payload_on"];
|
jsonConfig["payload_on"] = json["payload_on"];
|
||||||
jsonConfig["payload_off"] = json["payload_off"];
|
jsonConfig["payload_off"] = json["payload_off"];
|
||||||
if (deviceClass == HABSDC_BATTERY)
|
|
||||||
jsonConfig["value_template"] = "{{ value_json.battery }}";
|
|
||||||
}
|
}
|
||||||
char buffer[2048]{0};
|
char buffer[2048]{0};
|
||||||
serializeJsonPretty(jsonConfig, buffer);
|
serializeJsonPretty(jsonConfig, buffer);
|
||||||
|
Reference in New Issue
Block a user