diff --git a/README.md b/README.md index ce03a42..32caad2 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ESP-NOW based window/door sensor for ESP8266. Alternate firmware for Tuya/SmartL 1. When triggered transmits system information, battery status (HIGH/MID/LOW) and sensor status (OPEN/CLOSED). 2. Average response time of 1 second (depends on the MCU of the sensor). -3. In setup/update mode creates an access point named "ESP-NOW Window XXXXXXXXXXXX" with password "12345678" (IP 192.168.4.1). +3. In setup/update mode creates an access point named "ESP-NOW window XXXXXXXXXXXX" with password "12345678" (IP 192.168.4.1). 4. Automatically adds sensor and battery configuration to Home Assistan via MQTT discovery as a binary_sensors (2 different binary_sensor). 5. Possibility firmware update over OTA (if is allows the size of the flash memory). 6. Web interface for settings. @@ -24,7 +24,8 @@ See [here](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor/tree/main/ha 1. A gateway is required. For details see [ESP-NOW Gateway](https://github.com/aZholtikov/ESP-NOW-Gateway). 2. ESP-NOW network name 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. For using this firmware on Tuya/SmartLife WiFi window/door sensors, the WiFi module must be replaced with an ESP8266 compatible module (if necessary). -5. Highly recommended connect an external power supply during setup/upgrade. -6. Because this sensor is battery operated, it has an additional controller (MCU) that controls the power of the WiFi module (Module) and transmits data to it for transmission to the network. The communication is done via UART at 9600 speed. Make sure that the protocol is correct before flashing. Details [here](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor/tree/main/doc). +3. If encryption is used, the key must be set same of all another ESP-NOW devices in network. +4. Upload the "data" folder (with web interface) into the filesystem before flashing. +5. For using this firmware on Tuya/SmartLife WiFi window/door sensors, the WiFi module must be replaced with an ESP8266 compatible module (if necessary). +6. Highly recommended connect an external power supply during setup/upgrade. +7. Because this sensor is battery operated, it has an additional controller (MCU) that controls the power of the WiFi module (Module) and transmits data to it for transmission to the network. The communication is done via UART at 9600 speed. Make sure that the protocol is correct before flashing. Details [here](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor/tree/main/doc). diff --git a/data/function.js b/data/function.js index edd2130..1758539 100755 --- a/data/function.js +++ b/data/function.js @@ -29,7 +29,7 @@ function loadBlock() { } document.getElementsByTagName('body')[0].innerHTML = newString; setFirmvareValue('version', 'firmware'); - setGpioValue('deviceSensorClassSelect', 'deviceSensorClass'); + setGpioValue('deviceClassSelect', 'deviceClass'); handleServerResponse(); } @@ -51,10 +51,9 @@ function sendRequest(submit, server) { } function saveSetting(submit) { - server = "/setting?deviceSensorName=" + getValue('deviceSensorName') - + "&deviceBatteryName=" + getValue('deviceBatteryName') + server = "/setting?deviceName=" + getValue('deviceName') + "&espnowNetName=" + getValue('espnowNetName') - + "&deviceSensorClass=" + getSelectValue('deviceSensorClassSelect'); + + "&deviceClass=" + getSelectValue('deviceClassSelect'); sendRequest(submit, server); alert("Please restart device for changes apply."); } diff --git a/data/index.htm b/data/index.htm index 90f9727..d9c2d5e 100644 --- a/data/index.htm +++ b/data/index.htm @@ -5,12 +5,12 @@ - ESP-NOW Window/Door Sensor + ESP-NOW Window Sensor
-

ESP-NOW Window/Door Sensor

+

ESP-NOW Window Sensor

Firmware:

@@ -18,14 +18,8 @@
-

Sensor name:

- -
- -
-

Battery name:

- Device name:

+
@@ -37,8 +31,8 @@

Sensor type:

- -

+

diff --git a/data/style.css b/data/style.css index b4e4f72..bff8611 100644 --- a/data/style.css +++ b/data/style.css @@ -1,3 +1,7 @@ +p{ + margin: 0 0; +} + body { font-family: "Gill Sans", sans-serif; background: rgb(255, 255, 255); @@ -21,13 +25,15 @@ h1 { font-weight: 600; flex-shrink: 0; margin-right: 10px; + margin-left: 10px; + margin: 10px 0; } .text-select { width: 35%; font-weight: 600; flex-shrink: 0; - margin-right: 10px; + margin-right: 10px; } .wrapper { @@ -42,6 +48,7 @@ input { border-radius: 5px; border: none; margin-bottom: 10px; + margin-left: 10px; padding: 0 10px; color: rgb(0, 0, 0); background: #a3e0f1; @@ -76,6 +83,8 @@ select:hover { background: rgb(65, 125, 238); color: white; transition: .5s; + margin-left: 0; + margin-top: 8px; } .btn:hover { @@ -84,10 +93,13 @@ select:hover { transform: translatey(-3px); } -#deviceSensorName, -#deviceBatteryName, +#deviceName, #espnowNetName { - width: 73%; + width: 100%; +} + +#espnowNetName { + margin-bottom: 15px; } .wrapper.wrapper--end { diff --git a/src/main.cpp b/src/main.cpp index f8b3d19..e0eb7e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ #include "ZHNetwork.h" #include "ZHConfig.h" -void onConfirmReceiving(const uint8_t *target, const bool status); +void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status); void loadConfig(void); void saveConfig(void); @@ -14,13 +14,12 @@ void sendSensorConfigMessage(void); void sendBatteryConfigMessage(void); void sendAttributesMessage(void); -const String firmware{"1.1"}; +const String firmware{"1.2"}; String espnowNetName{"DEFAULT"}; -String deviceSensorName{"ESP-NOW window sensor"}; -uint8_t deviceSensorClass{HABSDC_WINDOW}; -String deviceBatteryName{"ESP-NOW window sensor battery"}; +String deviceName = "ESP-NOW window " + String(ESP.getChipId(), HEX); +uint8_t deviceClass{HABSDC_WINDOW}; String sensorStatus{""}; String batteryStatus{""}; @@ -34,7 +33,6 @@ bool semaphore{false}; esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_STATE}; StaticJsonDocument json; -char buffer[sizeof(esp_now_payload_data_t::message)]{0}; char temp[sizeof(esp_now_payload_data_t)]{0}; const char initialMessage[] = {0x55, 0xAA, 0x00, 0x01, 0x00, 0x00, 0x00}; const char connectedMessage[] = {0x55, 0xAA, 0x00, 0x02, 0x00, 0x01, 0x04, 0x06}; @@ -56,6 +54,7 @@ void setup() json["battery"] = batteryStatus; myNet.begin(espnowNetName.c_str()); + // 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.setOnConfirmReceivingCallback(onConfirmReceiving); @@ -111,7 +110,7 @@ void loop() Serial.end(); dataReceived = false; WiFi.mode(WIFI_AP); - WiFi.softAP(("ESP-NOW Window " + myNet.getNodeMac()).c_str(), "12345678", 1, 0); + WiFi.softAP(("ESP-NOW window " + String(ESP.getChipId(), HEX)).c_str(), "12345678", 1, 0); setupWebServer(); ArduinoOTA.begin(); } @@ -136,9 +135,8 @@ void loop() } dataReceived = false; saveConfig(); - serializeJsonPretty(json, buffer); - memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message)); - memcpy(temp, &outgoingData, sizeof(esp_now_payload_data_t)); + serializeJsonPretty(json, outgoingData.message); + memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t)); myNet.sendBroadcastMessage(temp); semaphore = true; } @@ -156,9 +154,8 @@ void loop() } dataReceived = false; saveConfig(); - serializeJsonPretty(json, buffer); - memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message)); - memcpy(temp, &outgoingData, sizeof(esp_now_payload_data_t)); + serializeJsonPretty(json, outgoingData.message); + memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t)); myNet.sendBroadcastMessage(temp); semaphore = true; } @@ -168,7 +165,7 @@ void loop() ArduinoOTA.handle(); } -void onConfirmReceiving(const uint8_t *target, const bool status) +void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status) { if (semaphore) { @@ -186,9 +183,8 @@ void loadConfig() StaticJsonDocument<1024> json; deserializeJson(json, jsonFile); espnowNetName = json["espnowNetName"].as(); - deviceSensorName = json["deviceSensorName"].as(); - deviceBatteryName = json["deviceBatteryName"].as(); - deviceSensorClass = json["deviceSensorClass"]; + deviceName = json["deviceName"].as(); + deviceClass = json["deviceClass"]; sensorStatus = json["sensorStatus"].as(); batteryStatus = json["batteryStatus"].as(); file.close(); @@ -199,9 +195,8 @@ void saveConfig() StaticJsonDocument<1024> json; json["firmware"] = firmware; json["espnowNetName"] = espnowNetName; - json["deviceSensorName"] = deviceSensorName; - json["deviceBatteryName"] = deviceBatteryName; - json["deviceSensorClass"] = deviceSensorClass; + json["deviceName"] = deviceName; + json["deviceClass"] = deviceClass; json["sensorStatus"] = sensorStatus; json["batteryStatus"] = batteryStatus; json["system"] = "empty"; @@ -217,9 +212,8 @@ void setupWebServer() webServer.on("/setting", HTTP_GET, [](AsyncWebServerRequest *request) { - deviceSensorName = request->getParam("deviceSensorName")->value(); - deviceBatteryName = request->getParam("deviceBatteryName")->value(); - deviceSensorClass = request->getParam("deviceSensorClass")->value().toInt(); + deviceName = request->getParam("deviceName")->value(); + deviceClass = request->getParam("deviceClass")->value().toInt(); espnowNetName = request->getParam("espnowNetName")->value(); request->send(200); saveConfig(); }); @@ -245,10 +239,11 @@ void sendSensorConfigMessage() { esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_CONFIG}; StaticJsonDocument json; - json["name"] = deviceSensorName; + json["name"] = deviceName; json["unit"] = 1; json["type"] = HACT_BINARY_SENSOR; - json["class"] = deviceSensorClass; + json["class"] = deviceClass; + json["template"] = "state"; json["payload_on"] = "OPEN"; json["payload_off"] = "CLOSED"; char buffer[sizeof(esp_now_payload_data_t::message)]{0}; @@ -263,10 +258,11 @@ void sendBatteryConfigMessage() { esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_CONFIG}; StaticJsonDocument json; - json["name"] = deviceBatteryName; + json["name"] = deviceName + " battery"; json["unit"] = 2; json["type"] = HACT_BINARY_SENSOR; json["class"] = HABSDC_BATTERY; + json["template"] = "battery"; json["payload_on"] = "MID"; json["payload_off"] = "HIGH"; char buffer[sizeof(esp_now_payload_data_t::message)]{0}; @@ -281,7 +277,7 @@ void sendAttributesMessage() { esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_ATTRIBUTES}; StaticJsonDocument json; - json["Type"] = "ESP-NOW Window Sensor"; + json["Type"] = "ESP-NOW window sensor"; json["MCU"] = "ESP8266"; json["MAC"] = myNet.getNodeMac(); json["Firmware"] = firmware;