Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
82f7e3c128 | |||
752e8caf53 | |||
373be8ac60 | |||
a18d4366ac |
@ -4,7 +4,7 @@ ESP-NOW based window/door sensor for ESP8266. Alternate firmware for Tuya/SmartL
|
||||
|
||||
## Features
|
||||
|
||||
1. When triggered transmits system information, battery status and sensor status.
|
||||
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).
|
||||
4. Automatically adds sensor and battery configuration to Home Assistan via MQTT discovery as a binary_sensors (2 different binary_sensor).
|
||||
|
@ -12,20 +12,20 @@ Communication protocol used in the firmware (only necessary "cuts" from the orig
|
||||
MCU returns 55 AA 00 01 00 ............ (MCU system information)
|
||||
Module sends 55 AA 00 02 00 01 04 06 (Network connection established)
|
||||
MCU returns 55 AA 00 02 00 00 01 (Confirmation message)
|
||||
The 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)
|
||||
The 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 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)
|
||||
MCU returns 55 AA 00 01 00 00 ............ (MCU system information)
|
||||
Module sends 55 AA 00 02 00 01 04 06 (Network connection established)
|
||||
MCU returns 55 AA 00 02 00 00 01 (Confirmation message)
|
||||
The 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 power off
|
||||
|
||||
@ -36,7 +36,7 @@ Communication protocol used in the firmware (only necessary "cuts" from the orig
|
||||
MCU returns 55 AA 00 01 00 00 ............ (MCU system information)
|
||||
Module sends 55 AA 00 02 00 01 04 06 (Network connection established)
|
||||
MCU returns 55 AA 00 02 00 00 01 (Confirmation message)
|
||||
The 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)
|
||||
|
||||
Update mode has started. Will be available during 120 seconds until the module is powered off.
|
||||
|
45
src/main.cpp
45
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,9 +22,12 @@ 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};
|
||||
byte counter{0};
|
||||
byte messageLenght{0};
|
||||
uint8_t counter{0};
|
||||
uint8_t messageLenght{0};
|
||||
bool dataReceiving{false};
|
||||
bool dataReceived{false};
|
||||
bool semaphore{false};
|
||||
@ -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));
|
||||
@ -156,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<String>();
|
||||
deviceSensorName = json["deviceSensorName"].as<String>();
|
||||
deviceBatteryName = json["deviceBatteryName"].as<String>();
|
||||
deviceSensorClass = json["deviceSensorClass"];
|
||||
sensorStatus = json["sensorStatus"].as<String>();
|
||||
batteryStatus = json["batteryStatus"].as<String>();
|
||||
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);
|
||||
|
Reference in New Issue
Block a user