Version 1.0
Initial version.
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.pio
|
||||
.vscode
|
||||
.DS_Store
|
32
README.md
@ -1,2 +1,32 @@
|
||||
# ESP-NOW-Water-Leakage-Sensor
|
||||
# ESP-NOW water leakage sensor for ESP8266
|
||||
|
||||
ESP-NOW based water leakage sensor for ESP8266. Alternate firmware for Tuya/SmartLife WiFi water leakage sensors.
|
||||
|
||||
## Features
|
||||
|
||||
1. 2 possiility operating modes - NORMAL and LITE.
|
||||
2. Average response time of 1.8 second (NORMAL), 0.4 second (LITE) (depends on the MCU of the sensor).
|
||||
3. Triggered in 2 cases: contact closure, contact drying (only if the contacts were previously closed).
|
||||
4. When triggered transmits system information and sensor status (ALARM/DRY) at NORMAL mode and always ALARM at LITE mode.
|
||||
5. In setup/update mode creates an access point named "ESP-NOW Water XXXXXXXXXXXX" with password "12345678" (IP 192.168.4.1) at NORMAL mode only.
|
||||
6. Automatically adds sensor configuration to Home Assistan via MQTT discovery as a binary_sensor.
|
||||
7. Possibility firmware update over OTA (if is allows the size of the flash memory) at NORMAL mode only.
|
||||
8. Web interface for settings at NORMAL mode only.
|
||||
|
||||
## Notes
|
||||
|
||||
1. ESP-NOW mesh network based on the library [ZHNetwork](https://github.com/aZholtikov/ZHNetwork).
|
||||
2. For enter to setup/update mode press the button for > 2 seconds. The LED blinks fast. Access point will be available during 120 seconds before the module is powered off.
|
||||
|
||||
## Tested on
|
||||
|
||||
See [here](https://github.com/aZholtikov/ESP-NOW-Water-Leakage-Sensor/tree/main/hardware).
|
||||
|
||||
## Attention
|
||||
|
||||
1. A gateway is required. For details see [ESP-NOW Gateway](https://github.com/aZholtikov/ESP-NOW-Gateway).
|
||||
2. ESP-NOW network name must be set same of all another ESP-NOW devices in network.
|
||||
3. Upload the "data" folder (with web interface) into the filesystem before flashing for NORMAL mode only.
|
||||
4. For using this firmware on Tuya/SmartLife WiFi water leakage sensors, the WiFi module must be replaced with an ESP8266 compatible module (if necessary).
|
||||
5. Highly recommended connect an external power supply during setup/upgrade.
|
||||
6. 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-Water-Leakage-Sensor/tree/main/doc) for NORMAL mode only.
|
||||
|
60
data/function.js
Executable file
@ -0,0 +1,60 @@
|
||||
var xmlHttp = createXmlHttpObject();
|
||||
function createXmlHttpObject() {
|
||||
if (window.XMLHttpRequest) {
|
||||
xmlHttp = new XMLHttpRequest();
|
||||
} else {
|
||||
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
|
||||
}
|
||||
return xmlHttp;
|
||||
}
|
||||
|
||||
function load() {
|
||||
if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {
|
||||
xmlHttp.open('PUT', '/config.json', true);
|
||||
xmlHttp.send(null);
|
||||
xmlHttp.onload = function () {
|
||||
jsonResponse = JSON.parse(xmlHttp.responseText);
|
||||
loadBlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function loadBlock() {
|
||||
newData = JSON.parse(xmlHttp.responseText);
|
||||
data = document.getElementsByTagName('body')[0].innerHTML;
|
||||
var newString;
|
||||
for (var key in newData) {
|
||||
newString = data.replace(new RegExp('{{' + key + '}}', 'g'), newData[key]);
|
||||
data = newString;
|
||||
}
|
||||
document.getElementsByTagName('body')[0].innerHTML = newString;
|
||||
setFirmvareValue('version', 'firmware');
|
||||
handleServerResponse();
|
||||
}
|
||||
|
||||
function getValue(id) {
|
||||
var value = document.getElementById(id).value;
|
||||
return value;
|
||||
}
|
||||
|
||||
function sendRequest(submit, server) {
|
||||
request = new XMLHttpRequest();
|
||||
request.open("GET", server, true);
|
||||
request.send();
|
||||
}
|
||||
|
||||
function saveSetting(submit) {
|
||||
server = "/setting?deviceName=" + getValue('deviceName')
|
||||
+ "&espnowNetName=" + getValue('espnowNetName');
|
||||
sendRequest(submit, server);
|
||||
alert("Please restart device for changes apply.");
|
||||
}
|
||||
|
||||
function restart(submit) {
|
||||
server = "/restart";
|
||||
sendRequest(submit, server);
|
||||
}
|
||||
|
||||
function setFirmvareValue(id, value) {
|
||||
document.getElementById(id).innerHTML = document.getElementById(value).value;
|
||||
}
|
39
data/index.htm
Normal file
@ -0,0 +1,39 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=0.9">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<script type="text/javascript" src="function.js"></script>
|
||||
<title>ESP-NOW Water Leakage Sensor</title>
|
||||
</head>
|
||||
|
||||
<body onload="load();">
|
||||
<form class="box">
|
||||
<h1>ESP-NOW Water Leakage Sensor </h1>
|
||||
<div class="wrapper">
|
||||
<p class="text">Firmware:</p>
|
||||
<p class="text" id="version"></p>
|
||||
<input id="firmware" value="{{firmware}}" hidden />
|
||||
</div>
|
||||
|
||||
<div class="wrapper">
|
||||
<p class="text">Sensor name:</p>
|
||||
<input id="deviceName" value="{{deviceName}}" placeholder="Name" autocomplete="off" label
|
||||
title="ESP-NOW device name (up to 150 characters)" />
|
||||
</div>
|
||||
|
||||
<div class="wrapper">
|
||||
<p class="text">ESP-NOW network name:</p>
|
||||
<input id="espnowNetName" value="{{espnowNetName}}" placeholder="Name" autocomplete="off" label
|
||||
title="ESP-NOW network name (1 to 20 characters)" />
|
||||
</div>
|
||||
|
||||
<div class="wrapper">
|
||||
<input class="btn" type="submit" value="Save" onclick="saveSetting(this);">
|
||||
<input class="btn" type="submit" value="Restart" onclick="restart(this);">
|
||||
</div>
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
69
data/style.css
Normal file
@ -0,0 +1,69 @@
|
||||
body {
|
||||
font-family: "Gill Sans", sans-serif;
|
||||
background: rgb(255, 255, 255);
|
||||
}
|
||||
|
||||
.box {
|
||||
width: 400px;
|
||||
padding: 20px 20px;
|
||||
margin: 20px auto;
|
||||
background: #e0f5fb;
|
||||
box-shadow: 4px 4px 30px rgba(0, 0, 0, 0.2);
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: rgb(65, 125, 238);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.text {
|
||||
font-weight: 600;
|
||||
flex-shrink: 0;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.wrapper {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: baseline;
|
||||
}
|
||||
|
||||
input {
|
||||
width: 48%;
|
||||
min-height: 30px;
|
||||
border-radius: 5px;
|
||||
border: none;
|
||||
margin-bottom: 10px;
|
||||
padding: 0 10px;
|
||||
color: rgb(0, 0, 0);
|
||||
background: #a3e0f1;
|
||||
transition: .5s;
|
||||
}
|
||||
|
||||
input:hover {
|
||||
background: white;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn {
|
||||
width: 48%;
|
||||
background: rgb(65, 125, 238);
|
||||
color: white;
|
||||
transition: .5s;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
background: rgb(65, 125, 238);
|
||||
opacity: 0.5;
|
||||
transform: translatey(-3px);
|
||||
}
|
||||
|
||||
#deviceName,
|
||||
#espnowNetName {
|
||||
width: 73%;
|
||||
}
|
||||
|
||||
.wrapper.wrapper--end {
|
||||
align-items: baseline;
|
||||
}
|
36
doc/README.md
Normal file
@ -0,0 +1,36 @@
|
||||
# Communication protocol
|
||||
|
||||
The protocol can be read with 2 USB-TTL converters. RX of the first one is connected to the RX of the WiFi module to read the MCU data. Connect the RX of the second to the TX of the WiFi module to read the module's data. Don't forget to connect all GND (of both converters and module).
|
||||
|
||||
Communication protocol used in the firmware (only necessary "cuts" from the original protocol):
|
||||
|
||||
```text
|
||||
1. Normal mode. Sensor triggering.
|
||||
|
||||
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)
|
||||
MCU sends 55 AA 00 05 00 05 01 04 00 01 00 0F (Leak status. 00 0F - leak, 01 10 - dry)
|
||||
Module returns 55 AA 00 05 00 01 00 05 (Confirmation message)
|
||||
MCU sends 55 AA 00 05 00 05 03 04 00 01 02 13 (Battery status. 02 13 - high, 01 12 - medium, 00 11 - low)
|
||||
Module returns 55 AA 00 05 00 01 00 05 (Confirmation message)
|
||||
Module power off
|
||||
|
||||
2. Update mode. Pressing the button for > 2 seconds - the LED blinks fast.
|
||||
|
||||
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)
|
||||
MCU sends 55 AA 00 04 00 00 01 00 04 (Message for switching to setting mode)
|
||||
Module returns 55 AA 00 04 00 00 03 (Confirmation message)
|
||||
|
||||
Update mode has started. Will be available during 120 seconds until the module is powered off.
|
||||
After updating and rebooting the module will return to normal mode - the LED will go off.
|
||||
Highly recommended connect an external power supply during update.
|
||||
|
||||
Battery level sending is not used in the firmware because it is not needed.
|
||||
```
|
BIN
doc/Serial Communication Protocol v20220706.pdf
Normal file
BIN
doc/Serial Port Protocol Water Leakage Sensor.pdf
Normal file
BIN
doc/Serial Port Protocol Wi-Fi Common Solution v20210412.pdf
Normal file
BIN
hardware/Model_WIFI_S2_Type_1/TYWE3S.pdf
Normal file
BIN
hardware/Model_WIFI_S2_Type_1/inside1.jpeg
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
hardware/Model_WIFI_S2_Type_1/inside2.jpeg
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
hardware/Model_WIFI_S2_Type_1/main.jpeg
Normal file
After Width: | Height: | Size: 2.0 MiB |
BIN
hardware/Model_WIFI_S2_Type_2/CB3S.pdf
Normal file
BIN
hardware/Model_WIFI_S2_Type_2/ESP-12E.pdf
Normal file
BIN
hardware/Model_WIFI_S2_Type_2/inside1.jpeg
Normal file
After Width: | Height: | Size: 2.0 MiB |
BIN
hardware/Model_WIFI_S2_Type_2/inside2.jpeg
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
hardware/Model_WIFI_S2_Type_2/inside3.jpeg
Normal file
After Width: | Height: | Size: 2.8 MiB |
BIN
hardware/Model_WIFI_S2_Type_2/inside4.jpeg
Normal file
After Width: | Height: | Size: 2.6 MiB |
BIN
hardware/Model_WIFI_S2_Type_2/main.jpeg
Normal file
After Width: | Height: | Size: 2.0 MiB |
5
hardware/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Tested on
|
||||
|
||||
1. Model WIFI S2 Type 1. Built on Tuya WiFi module TYWE3S (ESP8266 chip). Analogue of ESP-01. Does not require replacement. Total triggering time about 2 sec (NORMAL), 0.4 sec (LITE). [Photo](https://github.com/aZholtikov/ESP-NOW-Water-Leakage-Sensor/tree/main/hardware/Model_WIFI_S2_Type_1).
|
||||
|
||||
2. Model WIFI S2 Type 2. Built on Tuya WiFi module CB3S (BK7231N chip). Replacement required. Performed replacement with ESP-12E. Total triggering time about 1.5 sec (NORMAL), 0.3 sec (LITE). [Photo](https://github.com/aZholtikov/ESP-NOW-Water-Leakage-Sensor/tree/main/hardware/Model_WIFI_S2_Type_2). A pull-up to GND of GPIO15 is required (i used a 0805 10K SMD resistor on top of the module pins).
|
31
platformio.ini
Normal file
@ -0,0 +1,31 @@
|
||||
[env:TYWE3S]
|
||||
platform = espressif8266
|
||||
board = esp01_1m
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
https://github.com/aZholtikov/ZHNetwork
|
||||
https://github.com/aZholtikov/ZHConfig
|
||||
bblanchon/ArduinoJson@^6.19.4
|
||||
me-no-dev/ESP Async WebServer@^1.2.3
|
||||
|
||||
[env:ESP-12E]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
https://github.com/aZholtikov/ZHNetwork
|
||||
https://github.com/aZholtikov/ZHConfig
|
||||
bblanchon/ArduinoJson@^6.19.4
|
||||
me-no-dev/ESP Async WebServer@^1.2.3
|
||||
|
||||
[env:ESP-12E-OTA]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
framework = arduino
|
||||
upload_port = 192.168.4.1
|
||||
upload_protocol = espota
|
||||
lib_deps =
|
||||
https://github.com/aZholtikov/ZHNetwork
|
||||
https://github.com/aZholtikov/ZHConfig
|
||||
bblanchon/ArduinoJson@^6.19.4
|
||||
me-no-dev/ESP Async WebServer@^1.2.3
|
259
src/main.cpp
Normal file
@ -0,0 +1,259 @@
|
||||
#include "ArduinoJson.h"
|
||||
#include "ArduinoOTA.h"
|
||||
#include "ESPAsyncWebServer.h"
|
||||
#include "ZHNetwork.h"
|
||||
#include "ZHConfig.h"
|
||||
|
||||
#define NORMAL
|
||||
// #define LITE
|
||||
|
||||
void onConfirmReceiving(const uint8_t *target, const bool status);
|
||||
|
||||
void loadConfig(void);
|
||||
void saveConfig(void);
|
||||
void setupWebServer(void);
|
||||
|
||||
void sendConfigMessage(void);
|
||||
void sendAttributesMessage(void);
|
||||
|
||||
const String firmware{"1.0"};
|
||||
|
||||
String espnowNetName{"DEFAULT"};
|
||||
|
||||
String deviceName{"ESP-NOW water sensor"};
|
||||
|
||||
char receivedBytes[128]{0};
|
||||
uint8_t counter{0};
|
||||
uint8_t messageLenght{0};
|
||||
bool dataReceiving{false};
|
||||
bool dataReceived{false};
|
||||
bool semaphore{false};
|
||||
uint8_t count{0};
|
||||
|
||||
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_STATE};
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
char buffer[sizeof(esp_now_payload_data_t::message)]{0};
|
||||
char temp[sizeof(esp_now_payload_data_t)]{0};
|
||||
const char initialMessage[] = {0x55, 0xAA, 0x00, 0x01, 0x00, 0x00, 0x00};
|
||||
const char connectedMessage[] = {0x55, 0xAA, 0x00, 0x02, 0x00, 0x01, 0x04, 0x06};
|
||||
const char settingMessage[] = {0x55, 0xAA, 0x00, 0x04, 0x00, 0x00, 0x03};
|
||||
const char confirmationMessage[] = {0x55, 0xAA, 0x00, 0x05, 0x00, 0x01, 0x00, 0x05};
|
||||
|
||||
ZHNetwork myNet;
|
||||
AsyncWebServer webServer(80);
|
||||
|
||||
void setup()
|
||||
{
|
||||
#if defined(NORMAL)
|
||||
Serial.begin(9600);
|
||||
|
||||
SPIFFS.begin();
|
||||
|
||||
loadConfig();
|
||||
|
||||
myNet.begin(espnowNetName.c_str());
|
||||
|
||||
myNet.setOnConfirmReceivingCallback(onConfirmReceiving);
|
||||
|
||||
sendConfigMessage();
|
||||
sendAttributesMessage();
|
||||
|
||||
Serial.write(initialMessage, sizeof(initialMessage));
|
||||
Serial.flush();
|
||||
#endif
|
||||
#if defined(LITE)
|
||||
myNet.begin(espnowNetName.c_str());
|
||||
|
||||
myNet.setOnConfirmReceivingCallback(onConfirmReceiving);
|
||||
|
||||
sendConfigMessage();
|
||||
sendAttributesMessage();
|
||||
|
||||
json["state"] = "ALARM";
|
||||
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);
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
#if defined(NORMAL)
|
||||
if (Serial.available() > 0 && !dataReceived)
|
||||
{
|
||||
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)
|
||||
{
|
||||
Serial.write(connectedMessage, sizeof(connectedMessage));
|
||||
Serial.flush();
|
||||
dataReceived = false;
|
||||
}
|
||||
if (receivedBytes[3] == 0x02)
|
||||
dataReceived = false;
|
||||
if (receivedBytes[3] == 0x04)
|
||||
{
|
||||
Serial.write(settingMessage, sizeof(settingMessage));
|
||||
Serial.flush();
|
||||
Serial.end();
|
||||
dataReceived = false;
|
||||
WiFi.softAP(("ESP-NOW Water " + myNet.getNodeMac()).c_str(), "12345678", 1, 0);
|
||||
setupWebServer();
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
if (receivedBytes[3] == 0x05)
|
||||
{
|
||||
if (receivedBytes[6] == 0x03)
|
||||
{
|
||||
Serial.write(confirmationMessage, sizeof(confirmationMessage));
|
||||
Serial.flush();
|
||||
dataReceived = false;
|
||||
}
|
||||
if (receivedBytes[6] == 0x01)
|
||||
{
|
||||
if (receivedBytes[10] == 0x00)
|
||||
json["state"] = "ALARM";
|
||||
if (receivedBytes[10] == 0x01)
|
||||
json["state"] = "DRY";
|
||||
dataReceived = false;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
myNet.maintenance();
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
|
||||
void onConfirmReceiving(const uint8_t *target, const bool status)
|
||||
{
|
||||
#if defined(NORMAL)
|
||||
if (semaphore)
|
||||
{
|
||||
Serial.write(confirmationMessage, sizeof(confirmationMessage));
|
||||
Serial.flush();
|
||||
ESP.deepSleep(0);
|
||||
}
|
||||
#endif
|
||||
#if defined(LITE)
|
||||
++count;
|
||||
if (count == 3)
|
||||
ESP.deepSleep(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void loadConfig()
|
||||
{
|
||||
if (!SPIFFS.exists("/config.json"))
|
||||
saveConfig();
|
||||
File file = SPIFFS.open("/config.json", "r");
|
||||
String jsonFile = file.readString();
|
||||
StaticJsonDocument<512> json;
|
||||
deserializeJson(json, jsonFile);
|
||||
espnowNetName = json["espnowNetName"].as<String>();
|
||||
deviceName = json["deviceName"].as<String>();
|
||||
file.close();
|
||||
}
|
||||
|
||||
void saveConfig()
|
||||
{
|
||||
StaticJsonDocument<512> json;
|
||||
json["firmware"] = firmware;
|
||||
json["espnowNetName"] = espnowNetName;
|
||||
json["deviceName"] = deviceName;
|
||||
json["system"] = "empty";
|
||||
File file = SPIFFS.open("/config.json", "w");
|
||||
serializeJsonPretty(json, file);
|
||||
file.close();
|
||||
}
|
||||
|
||||
void setupWebServer()
|
||||
{
|
||||
webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(SPIFFS, "/index.htm"); });
|
||||
|
||||
webServer.on("/setting", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
deviceName = request->getParam("deviceName")->value();
|
||||
espnowNetName = request->getParam("espnowNetName")->value();
|
||||
request->send(200);
|
||||
saveConfig(); });
|
||||
|
||||
webServer.on("/restart", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
request->send(200);
|
||||
ESP.restart(); });
|
||||
|
||||
webServer.onNotFound([](AsyncWebServerRequest *request)
|
||||
{
|
||||
if (SPIFFS.exists(request->url()))
|
||||
request->send(SPIFFS, request->url());
|
||||
else
|
||||
{
|
||||
request->send(404, "text/plain", "File Not Found");
|
||||
} });
|
||||
|
||||
webServer.begin();
|
||||
}
|
||||
|
||||
void sendConfigMessage()
|
||||
{
|
||||
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_CONFIG};
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
json["name"] = deviceName;
|
||||
json["unit"] = 1;
|
||||
json["type"] = HACT_BINARY_SENSOR;
|
||||
json["class"] = HABSDC_MOISTURE;
|
||||
json["payload_on"] = "ALARM";
|
||||
json["payload_off"] = "DRY";
|
||||
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};
|
||||
memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
myNet.sendBroadcastMessage(temp);
|
||||
}
|
||||
|
||||
void sendAttributesMessage()
|
||||
{
|
||||
esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_ATTRIBUTES};
|
||||
StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
|
||||
json["Type"] = "ESP-NOW Water Sensor";
|
||||
json["MCU"] = "ESP8266";
|
||||
json["MAC"] = myNet.getNodeMac();
|
||||
json["Firmware"] = firmware;
|
||||
json["Library"] = myNet.getFirmwareVersion();
|
||||
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};
|
||||
memcpy(temp, &outgoingData, sizeof(esp_now_payload_data_t));
|
||||
myNet.sendBroadcastMessage(temp);
|
||||
}
|