Main code refactoring

This commit is contained in:
Alexey Zholtikov 2023-03-08 16:17:22 +03:00
parent 1c08d32e20
commit 72698ddaaf
2 changed files with 20 additions and 38 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

@ -29,9 +29,7 @@ const String firmware{"1.3"};
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};
@ -65,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();
}
@ -128,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));
@ -136,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)