Compare commits

...

4 Commits
v1.3 ... main

Author SHA1 Message Date
a1d389f225 Version 1.31
Main code refactoring.
Fixed one bug.
2023-03-15 15:49:56 +03:00
72698ddaaf Main code refactoring 2023-03-08 16:17:22 +03:00
1c08d32e20 Minor changes 2023-03-07 11:59:02 +03:00
e310ef6573 Fixed one bug 2023-03-06 00:38:23 +03:00
4 changed files with 27 additions and 44 deletions

View File

@ -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).
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).
Any feedback via [e-mail](mailto:github@zh.com.ru) would be appreciated. Or... [Buy me a coffee](https://paypal.me/aZholtikov).

View File

@ -10,7 +10,7 @@
<body onload="load();">
<form class="box">
<h1>ESP-NOW Window Sensor </h1>
<h1>ESP-NOW Window Sensor</h1>
<div class="wrapper">
<p class="text">Firmware:</p>
<p class="text" id="version"></p>

View File

@ -1,4 +1,4 @@
# 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).
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. 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).

View File

@ -20,17 +20,16 @@ void sendAttributesMessage(void);
struct deviceConfig
{
const String firmware{"1.3"};
String espnowNetName{"DEFAULT"};
String deviceName = "ESP-NOW window " + String(ESP.getChipId(), HEX);
uint8_t deviceClass{HABSDC_WINDOW};
} config;
const String firmware{"1.31"};
char receivedBytes[128]{0};
uint8_t counter{0};
uint8_t messageLenght{0};
bool dataReceiving{false};
bool dataReceived{false};
bool isDataReceived{false};
bool semaphore{false};
const char initialMessage[] = {0x55, 0xAA, 0x00, 0x01, 0x00, 0x00, 0x00};
@ -64,57 +63,27 @@ void setup()
void loop()
{
if (Serial.available() > 0 && !dataReceived)
if (isDataReceived)
{
char receivedByte[1];
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) // MCU system information.
if (receivedBytes[0] == 0x55 && receivedBytes[3] == 0x01) // MCU system information.
{
Serial.write(connectedMessage, sizeof(connectedMessage));
Serial.flush();
dataReceived = false;
}
if (receivedBytes[3] == 0x02) // MCU confirmation message.
dataReceived = false;
if (receivedBytes[3] == 0x03) // Message for switching to setting mode.
if (receivedBytes[0] == 0x55 && receivedBytes[3] == 0x03) // Message for switching to setting mode.
{
Serial.write(settingMessage, sizeof(settingMessage));
Serial.flush();
Serial.end();
dataReceived = false;
WiFi.mode(WIFI_AP);
WiFi.softAP(("ESP-NOW window " + String(ESP.getChipId(), HEX)).c_str(), "12345678", 1, 0);
setupWebServer();
ArduinoOTA.begin();
}
if (receivedBytes[3] == 0x08) // Sensor status message.
if (receivedBytes[0] == 0x55 && receivedBytes[3] == 0x08) // Sensor status message.
{
if (receivedBytes[7] == 0x01) // Battery status.
{
dataReceived = false;
Serial.write(confirmationMessage, sizeof(confirmationMessage));
Serial.flush();
}
@ -127,7 +96,6 @@ void loop()
if (receivedBytes[17] == 0x00)
json["state"] = "CLOSED";
json["battery"] = round((double(system_get_vdd33()) / 1000) * 100) / 100;
dataReceived = false;
serializeJsonPretty(json, outgoingData.message);
char temp[sizeof(esp_now_payload_data_t)]{0};
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
@ -135,11 +103,24 @@ void loop()
semaphore = true;
}
}
isDataReceived = false;
counter = 0;
memset(&receivedBytes, 0, 128);
}
myNet.maintenance();
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)
{
if (semaphore)
@ -201,7 +182,7 @@ void setupWebServer()
{
String configJson;
DynamicJsonDocument json(256); // To calculate the buffer size uses https://arduinojson.org/v6/assistant.
json["firmware"] = config.firmware;
json["firmware"] = firmware;
json["espnowNetName"] = config.espnowNetName;
json["deviceName"] = config.deviceName;
json["deviceClass"] = config.deviceClass;
@ -258,7 +239,7 @@ void sendAttributesMessage()
json["Type"] = "ESP-NOW window sensor";
json["MCU"] = "ESP8266";
json["MAC"] = myNet.getNodeMac();
json["Firmware"] = config.firmware;
json["Firmware"] = firmware;
json["Library"] = myNet.getFirmwareVersion();
serializeJsonPretty(json, outgoingData.message);
char temp[sizeof(esp_now_payload_data_t)]{0};