Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
a1d389f225 | |||
72698ddaaf | |||
1c08d32e20 | |||
e310ef6573 | |||
4f14de7763 |
@ -4,10 +4,10 @@ ESP-NOW based window/door sensor for ESP8266. Alternate firmware for Tuya/SmartL
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
1. When triggered transmits system information, battery status (HIGH/MID/LOW) and sensor status (OPEN/CLOSED).
|
1. When triggered transmits system information, battery level charge and sensor status (OPEN/CLOSED).
|
||||||
2. Average response time of 1 second (depends on the MCU of the sensor).
|
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).
|
4. Automatically adds sensor and battery configuration to Home Assistan via MQTT discovery as a binary_sensor (for sensor) and sensor (for battery).
|
||||||
5. Possibility firmware update over OTA (if is allows the size of the flash memory).
|
5. Possibility firmware update over OTA (if is allows the size of the flash memory).
|
||||||
6. Web interface for settings.
|
6. Web interface for settings.
|
||||||
|
|
||||||
@ -29,3 +29,5 @@ See [here](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor/tree/main/ha
|
|||||||
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).
|
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.
|
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).
|
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).
|
||||||
|
|
||||||
|
Any feedback via [e-mail](mailto:github@zh.com.ru) would be appreciated. Or... [Buy me a coffee](https://paypal.me/aZholtikov).
|
||||||
|
@ -10,7 +10,7 @@ function createXmlHttpObject() {
|
|||||||
|
|
||||||
function load() {
|
function load() {
|
||||||
if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {
|
if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {
|
||||||
xmlHttp.open('PUT', '/config.json', true);
|
xmlHttp.open('GET', '/config', true);
|
||||||
xmlHttp.send(null);
|
xmlHttp.send(null);
|
||||||
xmlHttp.onload = function () {
|
xmlHttp.onload = function () {
|
||||||
jsonResponse = JSON.parse(xmlHttp.responseText);
|
jsonResponse = JSON.parse(xmlHttp.responseText);
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
<body onload="load();">
|
<body onload="load();">
|
||||||
<form class="box">
|
<form class="box">
|
||||||
<h1>ESP-NOW Window Sensor </h1>
|
<h1>ESP-NOW Window Sensor</h1>
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
<p class="text">Firmware:</p>
|
<p class="text">Firmware:</p>
|
||||||
<p class="text" id="version"></p>
|
<p class="text" id="version"></p>
|
||||||
|
@ -11,20 +11,20 @@ Communication protocol used in the firmware (only necessary "cuts" from the orig
|
|||||||
Module sends 55 AA 00 01 00 00 00 (Initial message)
|
Module sends 55 AA 00 01 00 00 00 (Initial message)
|
||||||
MCU returns 55 AA 00 01 00 ............ (MCU system information)
|
MCU returns 55 AA 00 01 00 ............ (MCU system information)
|
||||||
Module sends 55 AA 00 02 00 01 04 06 (Network connection established)
|
Module sends 55 AA 00 02 00 01 04 06 (Network connection established)
|
||||||
MCU returns 55 AA 00 02 00 00 01 (Confirmation message)
|
MCU returns 55 AA 00 02 00 00 01 (MCU confirmation message)
|
||||||
MCU sends 55 AA 00 08 00 0C 00 01 01 01 01 01 03 04 00 01 02 23 (Battery status. 02 23 - high, 01 22 - medium, 00 21 - low)
|
MCU sends 55 AA 00 08 00 0C 00 01 01 01 01 01 03 04 00 01 02 23 (Battery status. 02 23 - high, 01 22 - medium, 00 21 - low)
|
||||||
Module returns 55 AA 00 08 00 01 00 08 (Confirmation message)
|
Module returns 55 AA 00 08 00 01 00 08 (Confirmation message)
|
||||||
MCU sends 55 AA 00 08 00 0C 00 02 02 02 02 02 01 01 01 00 22 (Sensor position. 01 23 - open, 00 22 - closed)
|
MCU sends 55 AA 00 08 00 0C 00 02 02 02 02 02 01 01 01 00 22 (Sensor position. 01 23 - open, 00 22 - closed)
|
||||||
Module returns 55 AA 00 08 00 01 00 08 (Confirmation message)
|
Module returns 55 AA 00 08 00 01 00 08 (Confirmation message)
|
||||||
Module power off
|
Module power off
|
||||||
|
|
||||||
2. Sending the battery status. Pressing the button.
|
2. Sending the battery status. Pressing the button. Not used in the firmware.
|
||||||
|
|
||||||
Module power is on
|
Module power is on
|
||||||
Module sends 55 AA 00 01 00 00 00 (Initial message)
|
Module sends 55 AA 00 01 00 00 00 (Initial message)
|
||||||
MCU returns 55 AA 00 01 00 00 ............ (MCU system information)
|
MCU returns 55 AA 00 01 00 00 ............ (MCU system information)
|
||||||
Module sends 55 AA 00 02 00 01 04 06 (Network connection established)
|
Module sends 55 AA 00 02 00 01 04 06 (Network connection established)
|
||||||
MCU returns 55 AA 00 02 00 00 01 (Confirmation message)
|
MCU returns 55 AA 00 02 00 00 01 (MCU confirmation message)
|
||||||
MCU sends 55 AA 00 08 00 0C 00 01 01 01 01 01 03 04 00 01 02 23 (Battery status. 02 23 - high, 01 22 - medium, 00 21 - low)
|
MCU sends 55 AA 00 08 00 0C 00 01 01 01 01 01 03 04 00 01 02 23 (Battery status. 02 23 - high, 01 22 - medium, 00 21 - low)
|
||||||
Module returns 55 AA 00 08 00 01 00 08 (Confirmation message)
|
Module returns 55 AA 00 08 00 01 00 08 (Confirmation message)
|
||||||
Module power off
|
Module power off
|
||||||
@ -35,7 +35,7 @@ Communication protocol used in the firmware (only necessary "cuts" from the orig
|
|||||||
Module sends 55 AA 00 01 00 00 00 (Initial message)
|
Module sends 55 AA 00 01 00 00 00 (Initial message)
|
||||||
MCU returns 55 AA 00 01 00 00 ............ (MCU system information)
|
MCU returns 55 AA 00 01 00 00 ............ (MCU system information)
|
||||||
Module sends 55 AA 00 02 00 01 04 06 (Network connection established)
|
Module sends 55 AA 00 02 00 01 04 06 (Network connection established)
|
||||||
MCU returns 55 AA 00 02 00 00 01 (Confirmation message)
|
MCU returns 55 AA 00 02 00 00 01 (MCU confirmation message)
|
||||||
MCU sends 55 AA 00 03 00 00 02 (Message for switching to setting mode)
|
MCU sends 55 AA 00 03 00 00 02 (Message for switching to setting mode)
|
||||||
Module returns 55 AA 00 03 00 00 02 (Confirmation message)
|
Module returns 55 AA 00 03 00 00 02 (Confirmation message)
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# Tested on
|
# Tested on
|
||||||
|
|
||||||
1. Model D06 Type 1. Built on Tuya WiFi module TYWE3S (ESP8266 chip). Analogue of ESP-01. Does not require replacement. Total triggering time about 1.5 sec. [Photo](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor/tree/main/hardware/Model_D06_Type_1).
|
1. MODEL D06 TYPE 1. Built on Tuya WiFi module TYWE3S (ESP8266 chip). Analogue of ESP-01. Does not require replacement. Total triggering time about 1.5 sec. [Photo](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor/tree/main/hardware/Model_D06_Type_1).
|
||||||
2. Model D06 Type 2. Built on Tuya WiFi module CBU (BK7231N chip). Replacement required. No physical equivalent (as of this writing). Performed replacement with ESP-M2 (ESP8285 chip) because it was in stock. It can be replaced with any ESP8266 compatible. Total response time about 0.5 sec. [Photo](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor/tree/main/hardware/Model_D06_Type_2).
|
2. MODEL D06 TYPE 2. Built on Tuya WiFi module CBU (BK7231N chip). Replacement required. No physical equivalent (as of this writing). Performed replacement with ESP-M2 (ESP8285 chip) because it was in stock. It can be replaced with any ESP8266 compatible. Total response time about 0.5 sec. [Photo](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor/tree/main/hardware/Model_D06_Type_2).
|
||||||
|
@ -1,19 +1,53 @@
|
|||||||
|
[env:ESP-12E]
|
||||||
|
platform = espressif8266
|
||||||
|
board = esp12e
|
||||||
|
framework = arduino
|
||||||
|
build_flags = -D PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK305
|
||||||
|
board_build.filesystem = littlefs
|
||||||
|
board_build.ldscript = eagle.flash.4m1m.ld
|
||||||
|
lib_deps =
|
||||||
|
https://github.com/aZholtikov/ZHNetwork
|
||||||
|
https://github.com/aZholtikov/ZHConfig
|
||||||
|
https://github.com/aZholtikov/Async-Web-Server
|
||||||
|
https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
|
[env:ESP-12E-OTA]
|
||||||
|
platform = espressif8266
|
||||||
|
board = esp12e
|
||||||
|
framework = arduino
|
||||||
|
build_flags = -D PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK305
|
||||||
|
board_build.filesystem = littlefs
|
||||||
|
board_build.ldscript = eagle.flash.4m1m.ld
|
||||||
|
upload_port = 192.168.4.1
|
||||||
|
upload_protocol = espota
|
||||||
|
lib_deps =
|
||||||
|
https://github.com/aZholtikov/ZHNetwork
|
||||||
|
https://github.com/aZholtikov/ZHConfig
|
||||||
|
https://github.com/aZholtikov/Async-Web-Server
|
||||||
|
https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
[env:TYWE3S]
|
[env:TYWE3S]
|
||||||
platform = espressif8266
|
platform = espressif8266
|
||||||
board = esp01_1m
|
board = esp01_1m
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
build_flags = -D PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK305
|
||||||
|
board_build.filesystem = littlefs
|
||||||
|
board_build.ldscript = eagle.flash.1m64.ld
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/aZholtikov/ZHNetwork
|
https://github.com/aZholtikov/ZHNetwork
|
||||||
https://github.com/aZholtikov/ZHConfig
|
https://github.com/aZholtikov/ZHConfig
|
||||||
bblanchon/ArduinoJson@^6.19.4
|
https://github.com/aZholtikov/Async-Web-Server
|
||||||
me-no-dev/ESP Async WebServer@^1.2.3
|
https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
[env:ESP-M2]
|
[env:ESP-M2]
|
||||||
platform = espressif8266
|
platform = espressif8266
|
||||||
board = esp8285
|
board = esp8285
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
build_flags = -D PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK305
|
||||||
|
board_build.filesystem = littlefs
|
||||||
|
board_build.ldscript = eagle.flash.1m64.ld
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/aZholtikov/ZHNetwork
|
https://github.com/aZholtikov/ZHNetwork
|
||||||
https://github.com/aZholtikov/ZHConfig
|
https://github.com/aZholtikov/ZHConfig
|
||||||
bblanchon/ArduinoJson@^6.19.4
|
https://github.com/aZholtikov/Async-Web-Server
|
||||||
me-no-dev/ESP Async WebServer@^1.2.3
|
https://github.com/bblanchon/ArduinoJson
|
||||||
|
249
src/main.cpp
249
src/main.cpp
@ -1,9 +1,13 @@
|
|||||||
#include "ArduinoJson.h"
|
#include "ArduinoJson.h"
|
||||||
#include "ArduinoOTA.h"
|
#include "ArduinoOTA.h"
|
||||||
#include "ESPAsyncWebServer.h"
|
#include "ESPAsyncWebServer.h" // https://github.com/aZholtikov/Async-Web-Server
|
||||||
|
#include "LittleFS.h"
|
||||||
|
#include "EEPROM.h"
|
||||||
#include "ZHNetwork.h"
|
#include "ZHNetwork.h"
|
||||||
#include "ZHConfig.h"
|
#include "ZHConfig.h"
|
||||||
|
|
||||||
|
ADC_MODE(ADC_VCC);
|
||||||
|
|
||||||
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status);
|
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status);
|
||||||
|
|
||||||
void loadConfig(void);
|
void loadConfig(void);
|
||||||
@ -14,26 +18,20 @@ void sendSensorConfigMessage(void);
|
|||||||
void sendBatteryConfigMessage(void);
|
void sendBatteryConfigMessage(void);
|
||||||
void sendAttributesMessage(void);
|
void sendAttributesMessage(void);
|
||||||
|
|
||||||
const String firmware{"1.2"};
|
struct deviceConfig
|
||||||
|
{
|
||||||
|
String espnowNetName{"DEFAULT"};
|
||||||
|
String deviceName = "ESP-NOW window " + String(ESP.getChipId(), HEX);
|
||||||
|
uint8_t deviceClass{HABSDC_WINDOW};
|
||||||
|
} config;
|
||||||
|
|
||||||
String espnowNetName{"DEFAULT"};
|
const String firmware{"1.31"};
|
||||||
|
|
||||||
String deviceName = "ESP-NOW window " + String(ESP.getChipId(), HEX);
|
|
||||||
uint8_t deviceClass{HABSDC_WINDOW};
|
|
||||||
|
|
||||||
String sensorStatus{""};
|
|
||||||
String batteryStatus{""};
|
|
||||||
|
|
||||||
char receivedBytes[128]{0};
|
char receivedBytes[128]{0};
|
||||||
uint8_t counter{0};
|
uint8_t counter{0};
|
||||||
uint8_t messageLenght{0};
|
bool isDataReceived{false};
|
||||||
bool dataReceiving{false};
|
|
||||||
bool dataReceived{false};
|
|
||||||
bool semaphore{false};
|
bool semaphore{false};
|
||||||
|
|
||||||
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_STATE};
|
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
|
||||||
char temp[sizeof(esp_now_payload_data_t)]{0};
|
|
||||||
const char initialMessage[] = {0x55, 0xAA, 0x00, 0x01, 0x00, 0x00, 0x00};
|
const char initialMessage[] = {0x55, 0xAA, 0x00, 0x01, 0x00, 0x00, 0x00};
|
||||||
const char connectedMessage[] = {0x55, 0xAA, 0x00, 0x02, 0x00, 0x01, 0x04, 0x06};
|
const char connectedMessage[] = {0x55, 0xAA, 0x00, 0x02, 0x00, 0x01, 0x04, 0x06};
|
||||||
const char settingMessage[] = {0x55, 0xAA, 0x00, 0x03, 0x00, 0x00, 0x02};
|
const char settingMessage[] = {0x55, 0xAA, 0x00, 0x03, 0x00, 0x00, 0x02};
|
||||||
@ -46,14 +44,11 @@ void setup()
|
|||||||
{
|
{
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
SPIFFS.begin();
|
LittleFS.begin();
|
||||||
|
|
||||||
loadConfig();
|
loadConfig();
|
||||||
|
|
||||||
json["state"] = sensorStatus;
|
myNet.begin(config.espnowNetName.c_str());
|
||||||
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.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);
|
myNet.setOnConfirmReceivingCallback(onConfirmReceiving);
|
||||||
@ -68,103 +63,64 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
if (Serial.available() > 0 && !dataReceived)
|
if (isDataReceived)
|
||||||
{
|
{
|
||||||
char receivedByte[1];
|
if (receivedBytes[0] == 0x55 && receivedBytes[3] == 0x01) // MCU system information.
|
||||||
Serial.readBytes(receivedByte, 1);
|
|
||||||
if (receivedByte[0] == 0x55)
|
|
||||||
{
|
|
||||||
dataReceiving = true;
|
|
||||||
receivedBytes[counter++] = receivedByte[0];
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (dataReceiving)
|
|
||||||
{
|
|
||||||
if (counter == 5)
|
|
||||||
messageLenght = 6 + int(receivedByte[0]);
|
|
||||||
if (counter == messageLenght)
|
|
||||||
{
|
|
||||||
receivedBytes[counter] = receivedByte[0];
|
|
||||||
counter = 0;
|
|
||||||
dataReceiving = false;
|
|
||||||
dataReceived = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
receivedBytes[counter++] = receivedByte[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (dataReceived)
|
|
||||||
{
|
|
||||||
if (receivedBytes[3] == 0x01)
|
|
||||||
{
|
{
|
||||||
Serial.write(connectedMessage, sizeof(connectedMessage));
|
Serial.write(connectedMessage, sizeof(connectedMessage));
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
dataReceived = false;
|
|
||||||
}
|
}
|
||||||
if (receivedBytes[3] == 0x02)
|
if (receivedBytes[0] == 0x55 && receivedBytes[3] == 0x03) // Message for switching to setting mode.
|
||||||
dataReceived = false;
|
|
||||||
if (receivedBytes[3] == 0x03)
|
|
||||||
{
|
{
|
||||||
Serial.write(settingMessage, sizeof(settingMessage));
|
Serial.write(settingMessage, sizeof(settingMessage));
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
Serial.end();
|
Serial.end();
|
||||||
dataReceived = false;
|
|
||||||
WiFi.mode(WIFI_AP);
|
WiFi.mode(WIFI_AP);
|
||||||
WiFi.softAP(("ESP-NOW window " + String(ESP.getChipId(), HEX)).c_str(), "12345678", 1, 0);
|
WiFi.softAP(("ESP-NOW window " + String(ESP.getChipId(), HEX)).c_str(), "12345678", 1, 0);
|
||||||
setupWebServer();
|
setupWebServer();
|
||||||
ArduinoOTA.begin();
|
ArduinoOTA.begin();
|
||||||
}
|
}
|
||||||
if (receivedBytes[3] == 0x08)
|
if (receivedBytes[0] == 0x55 && receivedBytes[3] == 0x08) // Sensor status message.
|
||||||
{
|
{
|
||||||
if (receivedBytes[7] == 0x01)
|
if (receivedBytes[7] == 0x01) // Battery status.
|
||||||
{
|
{
|
||||||
if (receivedBytes[17] == 0x02)
|
Serial.write(confirmationMessage, sizeof(confirmationMessage));
|
||||||
{
|
Serial.flush();
|
||||||
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;
|
|
||||||
saveConfig();
|
|
||||||
serializeJsonPretty(json, outgoingData.message);
|
|
||||||
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
|
||||||
myNet.sendBroadcastMessage(temp);
|
|
||||||
semaphore = true;
|
|
||||||
}
|
}
|
||||||
if (receivedBytes[7] == 0x02)
|
if (receivedBytes[7] == 0x02) // Sensor position.
|
||||||
{
|
{
|
||||||
|
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_STATE};
|
||||||
|
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||||
if (receivedBytes[17] == 0x01)
|
if (receivedBytes[17] == 0x01)
|
||||||
{
|
|
||||||
json["state"] = "OPEN";
|
json["state"] = "OPEN";
|
||||||
sensorStatus = "OPEN";
|
|
||||||
}
|
|
||||||
if (receivedBytes[17] == 0x00)
|
if (receivedBytes[17] == 0x00)
|
||||||
{
|
|
||||||
json["state"] = "CLOSED";
|
json["state"] = "CLOSED";
|
||||||
sensorStatus = "CLOSED";
|
json["battery"] = round((double(system_get_vdd33()) / 1000) * 100) / 100;
|
||||||
}
|
|
||||||
dataReceived = false;
|
|
||||||
saveConfig();
|
|
||||||
serializeJsonPretty(json, outgoingData.message);
|
serializeJsonPretty(json, outgoingData.message);
|
||||||
|
char temp[sizeof(esp_now_payload_data_t)]{0};
|
||||||
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||||
myNet.sendBroadcastMessage(temp);
|
myNet.sendBroadcastMessage(temp);
|
||||||
semaphore = true;
|
semaphore = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
isDataReceived = false;
|
||||||
|
counter = 0;
|
||||||
|
memset(&receivedBytes, 0, 128);
|
||||||
}
|
}
|
||||||
myNet.maintenance();
|
myNet.maintenance();
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void serialEvent()
|
||||||
|
{
|
||||||
|
while (Serial.available())
|
||||||
|
{
|
||||||
|
receivedBytes[counter++] = Serial.read();
|
||||||
|
delay(2);
|
||||||
|
}
|
||||||
|
isDataReceived = true;
|
||||||
|
}
|
||||||
|
|
||||||
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status)
|
void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool status)
|
||||||
{
|
{
|
||||||
if (semaphore)
|
if (semaphore)
|
||||||
@ -176,61 +132,69 @@ void onConfirmReceiving(const uint8_t *target, const uint16_t id, const bool sta
|
|||||||
|
|
||||||
void loadConfig()
|
void loadConfig()
|
||||||
{
|
{
|
||||||
if (!SPIFFS.exists("/config.json"))
|
ETS_GPIO_INTR_DISABLE();
|
||||||
|
EEPROM.begin(4096);
|
||||||
|
if (EEPROM.read(4095) == 254)
|
||||||
|
{
|
||||||
|
EEPROM.get(0, config);
|
||||||
|
EEPROM.end();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
EEPROM.end();
|
||||||
saveConfig();
|
saveConfig();
|
||||||
File file = SPIFFS.open("/config.json", "r");
|
}
|
||||||
String jsonFile = file.readString();
|
delay(50);
|
||||||
StaticJsonDocument<1024> json;
|
ETS_GPIO_INTR_ENABLE();
|
||||||
deserializeJson(json, jsonFile);
|
|
||||||
espnowNetName = json["espnowNetName"].as<String>();
|
|
||||||
deviceName = json["deviceName"].as<String>();
|
|
||||||
deviceClass = json["deviceClass"];
|
|
||||||
sensorStatus = json["sensorStatus"].as<String>();
|
|
||||||
batteryStatus = json["batteryStatus"].as<String>();
|
|
||||||
file.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveConfig()
|
void saveConfig()
|
||||||
{
|
{
|
||||||
StaticJsonDocument<1024> json;
|
ETS_GPIO_INTR_DISABLE();
|
||||||
json["firmware"] = firmware;
|
EEPROM.begin(4096);
|
||||||
json["espnowNetName"] = espnowNetName;
|
EEPROM.write(4095, 254);
|
||||||
json["deviceName"] = deviceName;
|
EEPROM.put(0, config);
|
||||||
json["deviceClass"] = deviceClass;
|
EEPROM.end();
|
||||||
json["sensorStatus"] = sensorStatus;
|
delay(50);
|
||||||
json["batteryStatus"] = batteryStatus;
|
ETS_GPIO_INTR_ENABLE();
|
||||||
json["system"] = "empty";
|
|
||||||
File file = SPIFFS.open("/config.json", "w");
|
|
||||||
serializeJsonPretty(json, file);
|
|
||||||
file.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupWebServer()
|
void setupWebServer()
|
||||||
{
|
{
|
||||||
webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||||
{ request->send(SPIFFS, "/index.htm"); });
|
{ request->send(LittleFS, "/index.htm"); });
|
||||||
|
|
||||||
|
webServer.on("/function.js", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||||
|
{ request->send(LittleFS, "/function.js"); });
|
||||||
|
|
||||||
|
webServer.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||||
|
{ request->send(LittleFS, "/style.css"); });
|
||||||
|
|
||||||
webServer.on("/setting", HTTP_GET, [](AsyncWebServerRequest *request)
|
webServer.on("/setting", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
deviceName = request->getParam("deviceName")->value();
|
config.deviceName = request->getParam("deviceName")->value();
|
||||||
deviceClass = request->getParam("deviceClass")->value().toInt();
|
config.deviceClass = request->getParam("deviceClass")->value().toInt();
|
||||||
espnowNetName = request->getParam("espnowNetName")->value();
|
config.espnowNetName = request->getParam("espnowNetName")->value();
|
||||||
request->send(200);
|
request->send(200);
|
||||||
saveConfig(); });
|
saveConfig(); });
|
||||||
|
|
||||||
webServer.on("/restart", HTTP_GET, [](AsyncWebServerRequest *request)
|
webServer.on("/config", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
request->send(200);
|
String configJson;
|
||||||
|
DynamicJsonDocument json(256); // To calculate the buffer size uses https://arduinojson.org/v6/assistant.
|
||||||
|
json["firmware"] = firmware;
|
||||||
|
json["espnowNetName"] = config.espnowNetName;
|
||||||
|
json["deviceName"] = config.deviceName;
|
||||||
|
json["deviceClass"] = config.deviceClass;
|
||||||
|
serializeJsonPretty(json, configJson);
|
||||||
|
request->send(200, "application/json", configJson); });
|
||||||
|
|
||||||
|
webServer.on("/restart", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||||
|
{request->send(200);
|
||||||
ESP.restart(); });
|
ESP.restart(); });
|
||||||
|
|
||||||
webServer.onNotFound([](AsyncWebServerRequest *request)
|
webServer.onNotFound([](AsyncWebServerRequest *request)
|
||||||
{
|
{ request->send(404, "text/plain", "File Not Found"); });
|
||||||
if (SPIFFS.exists(request->url()))
|
|
||||||
request->send(SPIFFS, request->url());
|
|
||||||
else
|
|
||||||
{
|
|
||||||
request->send(404, "text/plain", "File Not Found");
|
|
||||||
} });
|
|
||||||
|
|
||||||
webServer.begin();
|
webServer.begin();
|
||||||
}
|
}
|
||||||
@ -238,17 +202,15 @@ void setupWebServer()
|
|||||||
void sendSensorConfigMessage()
|
void sendSensorConfigMessage()
|
||||||
{
|
{
|
||||||
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_CONFIG};
|
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_CONFIG};
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||||
json["name"] = deviceName;
|
json[MCMT_DEVICE_NAME] = config.deviceName;
|
||||||
json["unit"] = 1;
|
json[MCMT_DEVICE_UNIT] = 1;
|
||||||
json["type"] = HACT_BINARY_SENSOR;
|
json[MCMT_COMPONENT_TYPE] = HACT_BINARY_SENSOR;
|
||||||
json["class"] = deviceClass;
|
json[MCMT_DEVICE_CLASS] = config.deviceClass;
|
||||||
json["template"] = "state";
|
json[MCMT_VALUE_TEMPLATE] = "state";
|
||||||
json["payload_on"] = "OPEN";
|
json[MCMT_PAYLOAD_ON] = "OPEN";
|
||||||
json["payload_off"] = "CLOSED";
|
json[MCMT_PAYLOAD_OFF] = "CLOSED";
|
||||||
char buffer[sizeof(esp_now_payload_data_t::message)]{0};
|
serializeJsonPretty(json, outgoingData.message);
|
||||||
serializeJsonPretty(json, buffer);
|
|
||||||
memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message));
|
|
||||||
char temp[sizeof(esp_now_payload_data_t)]{0};
|
char temp[sizeof(esp_now_payload_data_t)]{0};
|
||||||
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||||
myNet.sendBroadcastMessage(temp);
|
myNet.sendBroadcastMessage(temp);
|
||||||
@ -257,17 +219,14 @@ void sendSensorConfigMessage()
|
|||||||
void sendBatteryConfigMessage()
|
void sendBatteryConfigMessage()
|
||||||
{
|
{
|
||||||
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_CONFIG};
|
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_CONFIG};
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||||
json["name"] = deviceName + " battery";
|
json[MCMT_DEVICE_NAME] = config.deviceName + " battery";
|
||||||
json["unit"] = 2;
|
json[MCMT_DEVICE_UNIT] = 2;
|
||||||
json["type"] = HACT_BINARY_SENSOR;
|
json[MCMT_COMPONENT_TYPE] = HACT_SENSOR;
|
||||||
json["class"] = HABSDC_BATTERY;
|
json[MCMT_DEVICE_CLASS] = HASDC_VOLTAGE;
|
||||||
json["template"] = "battery";
|
json[MCMT_VALUE_TEMPLATE] = "battery";
|
||||||
json["payload_on"] = "MID";
|
json[MCMT_UNIT_OF_MEASUREMENT] = "V";
|
||||||
json["payload_off"] = "HIGH";
|
serializeJsonPretty(json, outgoingData.message);
|
||||||
char buffer[sizeof(esp_now_payload_data_t::message)]{0};
|
|
||||||
serializeJsonPretty(json, buffer);
|
|
||||||
memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message));
|
|
||||||
char temp[sizeof(esp_now_payload_data_t)]{0};
|
char temp[sizeof(esp_now_payload_data_t)]{0};
|
||||||
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||||
myNet.sendBroadcastMessage(temp);
|
myNet.sendBroadcastMessage(temp);
|
||||||
@ -276,16 +235,14 @@ void sendBatteryConfigMessage()
|
|||||||
void sendAttributesMessage()
|
void sendAttributesMessage()
|
||||||
{
|
{
|
||||||
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_ATTRIBUTES};
|
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_ATTRIBUTES};
|
||||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
|
||||||
json["Type"] = "ESP-NOW window sensor";
|
json["Type"] = "ESP-NOW window sensor";
|
||||||
json["MCU"] = "ESP8266";
|
json["MCU"] = "ESP8266";
|
||||||
json["MAC"] = myNet.getNodeMac();
|
json["MAC"] = myNet.getNodeMac();
|
||||||
json["Firmware"] = firmware;
|
json["Firmware"] = firmware;
|
||||||
json["Library"] = myNet.getFirmwareVersion();
|
json["Library"] = myNet.getFirmwareVersion();
|
||||||
char buffer[sizeof(esp_now_payload_data_t::message)]{0};
|
serializeJsonPretty(json, outgoingData.message);
|
||||||
serializeJsonPretty(json, buffer);
|
|
||||||
memcpy(outgoingData.message, buffer, sizeof(esp_now_payload_data_t::message));
|
|
||||||
char temp[sizeof(esp_now_payload_data_t)]{0};
|
char temp[sizeof(esp_now_payload_data_t)]{0};
|
||||||
memcpy(temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||||
myNet.sendBroadcastMessage(temp);
|
myNet.sendBroadcastMessage(temp);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user