From 82f7e3c12870adb8106d454ab26aab650793b7a2 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Mon, 9 Jan 2023 12:33:46 +0300 Subject: [PATCH] Version 1.1 Fixed some bugs. --- doc/README.md | 2 +- src/main.cpp | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/doc/README.md b/doc/README.md index 6f82a44..031443c 100644 --- a/doc/README.md +++ b/doc/README.md @@ -18,7 +18,7 @@ Communication protocol used in the firmware (only necessary "cuts" from the orig Module returns 55 AA 00 08 00 01 00 08 (Confirmation message) Module power off -2. Sending the battery status. Pressing the button. Not used in the firmware. +2. Sending the battery status. Pressing the button. Module power is on Module sends 55 AA 00 01 00 00 00 (Initial message) diff --git a/src/main.cpp b/src/main.cpp index e778db0..f8b3d19 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ void sendSensorConfigMessage(void); void sendBatteryConfigMessage(void); void sendAttributesMessage(void); -const String firmware{"1.0"}; +const String firmware{"1.1"}; String espnowNetName{"DEFAULT"}; @@ -22,6 +22,9 @@ String deviceSensorName{"ESP-NOW window sensor"}; uint8_t deviceSensorClass{HABSDC_WINDOW}; String deviceBatteryName{"ESP-NOW window sensor battery"}; +String sensorStatus{""}; +String batteryStatus{""}; + char receivedBytes[128]{0}; uint8_t counter{0}; uint8_t messageLenght{0}; @@ -49,6 +52,9 @@ void setup() loadConfig(); + json["state"] = sensorStatus; + json["battery"] = batteryStatus; + myNet.begin(espnowNetName.c_str()); myNet.setOnConfirmReceivingCallback(onConfirmReceiving); @@ -104,6 +110,7 @@ void loop() Serial.flush(); Serial.end(); dataReceived = false; + WiFi.mode(WIFI_AP); WiFi.softAP(("ESP-NOW Window " + myNet.getNodeMac()).c_str(), "12345678", 1, 0); setupWebServer(); ArduinoOTA.begin(); @@ -113,22 +120,42 @@ void loop() if (receivedBytes[7] == 0x01) { if (receivedBytes[17] == 0x02) + { json["battery"] = "HIGH"; + batteryStatus = "HIGH"; + } if (receivedBytes[17] == 0x01) + { json["battery"] = "MID"; + batteryStatus = "MID"; + } if (receivedBytes[17] == 0x00) + { json["battery"] = "LOW"; + batteryStatus = "LOW"; + } dataReceived = false; - Serial.write(confirmationMessage, sizeof(confirmationMessage)); - Serial.flush(); + saveConfig(); + serializeJsonPretty(json, buffer); + memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message)); + memcpy(temp, &outgoingData, sizeof(esp_now_payload_data_t)); + myNet.sendBroadcastMessage(temp); + semaphore = true; } if (receivedBytes[7] == 0x02) { if (receivedBytes[17] == 0x01) + { json["state"] = "OPEN"; + sensorStatus = "OPEN"; + } if (receivedBytes[17] == 0x00) + { json["state"] = "CLOSED"; + sensorStatus = "CLOSED"; + } 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)); @@ -147,7 +174,6 @@ void onConfirmReceiving(const uint8_t *target, const bool status) { Serial.write(confirmationMessage, sizeof(confirmationMessage)); Serial.flush(); - ESP.deepSleep(0); } } @@ -157,23 +183,27 @@ void loadConfig() saveConfig(); File file = SPIFFS.open("/config.json", "r"); String jsonFile = file.readString(); - StaticJsonDocument<512> json; + StaticJsonDocument<1024> json; deserializeJson(json, jsonFile); espnowNetName = json["espnowNetName"].as(); deviceSensorName = json["deviceSensorName"].as(); deviceBatteryName = json["deviceBatteryName"].as(); deviceSensorClass = json["deviceSensorClass"]; + sensorStatus = json["sensorStatus"].as(); + batteryStatus = json["batteryStatus"].as(); file.close(); } void saveConfig() { - StaticJsonDocument<512> json; + StaticJsonDocument<1024> json; json["firmware"] = firmware; json["espnowNetName"] = espnowNetName; json["deviceSensorName"] = deviceSensorName; json["deviceBatteryName"] = deviceBatteryName; json["deviceSensorClass"] = deviceSensorClass; + json["sensorStatus"] = sensorStatus; + json["batteryStatus"] = batteryStatus; json["system"] = "empty"; File file = SPIFFS.open("/config.json", "w"); serializeJsonPretty(json, file);