[HM-RD] Added leak/water detection sensor with ESP32 deep sleep / sensor wake (#1615)

Added leak/water detection sensor with ESP32 deep sleep / sensor wake
This commit is contained in:
Die Peter Pan authored and GitHub committed 2023-04-25 17:14:49 -05:00
1 parent e3e700449b
commit abeef870c7
9 files changed
+252 -21

No files matched your search

+11 -1
View File
@@ -298,12 +298,22 @@ static_assert(MQTT_SECURE_SELF_SIGNED_INDEX_DEFAULT < (sizeof(certs_array) / siz
* Deep-sleep for the ESP8266.
* Set the wake pin.
*/
#ifdef ESP8266_DEEP_SLEEP_IN_US
#if defined(DEEP_SLEEP_IN_US) && defined(ESP8266)
# ifndef ESP8266_DEEP_SLEEP_WAKE_PIN
# define ESP8266_DEEP_SLEEP_WAKE_PIN D0
# endif
#endif
/**
* Ext wake for Deep-sleep for the ESP32.
* Set the wake pin state.
*/
#ifdef ESP32_EXT0_WAKE_PIN
# ifndef ESP32_EXT0_WAKE_PIN_STATE
# define ESP32_EXT0_WAKE_PIN_STATE 1
# endif
#endif
/*------------------DEEP SLEEP parameters ------------------*/
//DEFAULT_LOW_POWER_MODE -1 to normal mode, low power mode can't be used on this build
//DEFAULT_LOW_POWER_MODE 0 to normal mode (no power consumption optimisations)
+80
View File
@@ -0,0 +1,80 @@
/*
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
Act as a wifi or ethernet gateway between your 433mhz/infrared IR/BLE signal and a MQTT broker
Send and receiving command by MQTT
This file sets parameters for the integration of C-37 YL-83 HM-RD water detection sensors
This file is part of OpenMQTTGateway.
OpenMQTTGateway is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenMQTTGateway is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "User_config.h"
#ifdef ZsensorC37_YL83_HMRD
//Time used to wait for an interval before resending temp and hum
unsigned long timeC37YL83HMRD = 0;
unsigned int persistedanalog = 0;
unsigned int persisteddigital = 0;
void setupZsensorC37_YL83_HMRD() {
pinMode(C37_YL83_HMRD_Digital_GPIO, INPUT);
Log.trace(F("C37_YL83_HMRD: digital configured pin: %d" CR), C37_YL83_HMRD_Digital_GPIO);
pinMode(C37_YL83_HMRD_Analog_GPIO, INPUT);
Log.trace(F("C37_YL83_HMRD: Analog configured pin: %d" CR), C37_YL83_HMRD_Analog_GPIO);
# ifdef C37_YL83_HMRD_Analog_RESOLUTION
analogReadResolution(C37_YL83_HMRD_Analog_RESOLUTION);
Log.trace(F("C37_YL83_HMRD: resolution: %d" CR), C37_YL83_HMRD_Analog_RESOLUTION);
# endif
}
void MeasureC37_YL83_HMRDWater() {
if (millis() > (timeC37YL83HMRD + C37_YL83_HMRD_INTERVAL_SEC)) { //retrieving value of water sensor every xUL
timeC37YL83HMRD = millis();
static int persistedanalog;
static int persisteddigital;
int sensorDigitalValue = digitalRead(C37_YL83_HMRD_Digital_GPIO); // Read the analog value from sensor
int sensorAnalogValue = analogRead(C37_YL83_HMRD_Analog_GPIO);
Log.trace(F("Creating C37_YL83_HMRD buffer" CR));
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject C37_YL83_HMRDdata = jsonBuffer.to<JsonObject>();
if (sensorDigitalValue != persisteddigital || C37_YL83_HMRD_ALWAYS) {
C37_YL83_HMRDdata["detected"] = (sensorDigitalValue == 1 ? "false" : "true");
} else {
Log.trace(F("Same digital don't send it" CR));
}
if (sensorAnalogValue != persistedanalog || C37_YL83_HMRD_ALWAYS) {
C37_YL83_HMRDdata["reading"] = sensorAnalogValue;
} else {
Log.trace(F("Same analog don't send it" CR));
}
if (C37_YL83_HMRDdata.size() > 0) {
pub(C37_YL83_HMRD_TOPIC, C37_YL83_HMRDdata);
delay(10);
# if defined(DEEP_SLEEP_IN_US) || defined(ESP32_EXT0_WAKE_PIN)
if (sensorDigitalValue == 1) //No water detected, all good we can sleep
ready_to_sleep = true;
# endif
}
persistedanalog = sensorAnalogValue;
persisteddigital = sensorDigitalValue;
}
}
#endif
+3 -3
View File
@@ -158,13 +158,13 @@ void MeasureDS1820Temp() {
}
pub((char*)(String(OW_TOPIC) + "/" + ds1820_addr[i]).c_str(), DS1820data);
delay(10);
# if defined(DEEP_SLEEP_IN_US) || defined(ESP32_EXT0_WAKE_PIN)
ready_to_sleep = true;
# endif
} else {
Log.trace(F("DS1820: Temperature for device %s didn't change, don't publish it." CR), (char*)ds1820_addr[i].c_str());
}
persisted_temp[i] = current_temp[i];
# ifdef ESP8266_DEEP_SLEEP_IN_US
ready_to_sleep = true;
# endif
}
}
}
+63
View File
@@ -0,0 +1,63 @@
/*
OpenMQTTGateway - ESP8266 or Arduino program for home automation
Act as a wifi or ethernet gateway between your RF/infrared IR signal and a MQTT broker
Send and receiving command by MQTT
This file sets parameters for the integration of C-37 YL-83 HM-RD water detection sensors
This file is part of OpenMQTTGateway.
OpenMQTTGateway is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenMQTTGateway is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef config_C37_YL83_HMRD_h
#define config_C37_YL83_HMRD_h
extern void setupZsensorC37_YL83_HMRD();
extern void C37_YL83_HMRDtoMQTT();
extern void MeasureC37_YL83_HMRDWater();
/*----------------------------USER PARAMETERS-----------------------------*/
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
#define C37_YL83_HMRD_TOPIC "/WATERtoMQTT/c37_yk83_hmrd"
#define C37_YL83_HMRD_ALWAYS true // if false only published current water leak detection if has changed from previous reading
#ifndef C37_YL83_HMRD_INTERVAL_SEC
# define C37_YL83_HMRD_INTERVAL_SEC 10000 // time between C-37, YL-83, HM-RD readings (ms)
#endif
#ifndef C37_YL83_HMRD_RESOLUTION
# define C37_YL83_HMRD_RESOLUTION 10 // conversion times: 9 bit (93.75 ms), 10 bit (187.5 ms), 11 bit (375 ms), 12 bit (750 ms)
#endif
/*-------------------PIN DEFINITIONS----------------------*/
#ifndef C37_YL83_HMRD_Digital_GPIO
# if defined(ESP8266)
# define C37_YL83_HMRD_Digital_GPIO 14
# elif defined(ESP32)
# define C37_YL83_HMRD_Digital_GPIO GPIO_NUM_14
# else
# define C37_YL83_HMRD_Digital_GPIO 14
# endif
#endif
#ifndef C37_YL83_HMRD_Analog_GPIO
# if defined(ESP8266)
# define C37_YL83_HMRD_Analog_GPIO A0
# elif defined(ESP32)
# define C37_YL83_HMRD_Analog_GPIO GPIO_NUM_35
# else
# define C37_YL83_HMRD_Analog_GPIO A0
# endif
#endif
#endif
+51 -7
View File
@@ -54,10 +54,10 @@ unsigned long timer_sys_checks = 0;
#endif
/**
* Deep-sleep for the ESP8266 we need some form of indicator that we have posted the measurements and am ready to deep sleep.
* Deep-sleep for the ESP8266 & ESP32 we need some form of indicator that we have posted the measurements and am ready to deep sleep.
* Set this to true in the sensor code after publishing the measurement.
*/
#ifdef ESP8266_DEEP_SLEEP_IN_US
#if defined(DEEP_SLEEP_IN_US) || defined(ESP32_EXT0_WAKE_PIN)
bool ready_to_sleep = false;
#endif
@@ -147,6 +147,9 @@ struct GfSun2000Data {};
#ifdef ZsensorHCSR04
# include "config_HCSR04.h"
#endif
#ifdef ZsensorC37_YL83_HMRD
# include "config_C37_YL83_HMRD.h"
#endif
#ifdef ZsensorDHT
# include "config_DHT.h"
#endif
@@ -779,9 +782,32 @@ void setup() {
Log.notice(F("OpenMQTTGateway Version: " OMG_VERSION CR));
# endif
# ifdef ESP8266_DEEP_SLEEP_IN_US
/**
* Deep-sleep for the ESP8266 & ESP32 we need some form of indicator that we have posted the measurements and am ready to deep sleep.
* When woken set back to false.
*/
# if defined(DEEP_SLEEP_IN_US) || defined(ESP32_EXT0_WAKE_PIN)
ready_to_sleep = false;
# endif
# ifdef DEEP_SLEEP_IN_US
# ifdef ESP8266
Log.notice(F("Setting wake pin for deep sleep." CR));
pinMode(ESP8266_DEEP_SLEEP_WAKE_PIN, WAKEUP_PULLUP);
# endif
# ifdef ESP32
Log.notice(F("Setting duration for deep sleep." CR));
if (esp_sleep_enable_timer_wakeup(DEEP_SLEEP_IN_US) != ESP_OK) {
Log.error(F("Failed to set deep sleep duration." CR));
}
# endif
# endif
# ifdef ESP32_EXT0_WAKE_PIN
Log.notice(F("Setting EXT0 Wakeup for deep sleep." CR));
if (esp_sleep_enable_ext0_wakeup(ESP32_EXT0_WAKE_PIN, ESP32_EXT0_WAKE_PIN_STATE) != ESP_OK) {
Log.error(F("Failed to set deep sleep EXT0 Wakeup." CR));
}
# endif
/*
@@ -993,6 +1019,10 @@ void setup() {
setupADC();
modules.add(ZsensorADC);
#endif
#ifdef ZsensorC37_YL83_HMRD
setupZsensorC37_YL83_HMRD();
modules.add(ZsensorC37_YL83_HMRD);
#endif
#ifdef ZsensorDHT
setupDHT();
modules.add(ZsensorDHT);
@@ -1684,6 +1714,9 @@ void loop() {
#ifdef ZsensorTSL2561
MeasureLightIntensityTSL2561();
#endif
#ifdef ZsensorC37_YL83_HMRD
MeasureC37_YL83_HMRDWater(); //Addon for leak detection with a C-37 YL-83 H-MRD
#endif
#ifdef ZsensorDHT
MeasureTempAndHum(); //Addon to measure the temperature with a DHT
#endif
@@ -1794,13 +1827,24 @@ void loop() {
#endif
/**
* Deep-sleep for the ESP8266 - e.g. ESP8266_DEEP_SLEEP_IN_US 30000000 for 30 seconds.
* Deep-sleep for the ESP8266 & ESP32 - e.g. DEEP_SLEEP_IN_US 30000000 for 30 seconds / wake by ESP32_EXT0_WAKE_PIN.
* Everything is off and (almost) all execution state is lost.
*/
#ifdef ESP8266_DEEP_SLEEP_IN_US
#if defined(DEEP_SLEEP_IN_US) || defined(ESP32_EXT0_WAKE_PIN)
if (ready_to_sleep) {
Log.notice(F("Entering deep sleep for : %l us." CR), ESP8266_DEEP_SLEEP_IN_US);
ESP.deepSleep(ESP8266_DEEP_SLEEP_IN_US);
delay(250); //Give some time for last MQTT messages to be sent
# ifdef DEEP_SLEEP_IN_US
Log.notice(F("Entering deep sleep for %l us." CR), DEEP_SLEEP_IN_US);
# endif
# ifdef ESP32_EXT0_WAKE_PIN
Log.notice(F("Entering deep sleep, EXT0 Wakeup by pin : %l." CR), ESP32_EXT0_WAKE_PIN);
# endif
# ifdef ESP8266
ESP.deepSleep(DEEP_SLEEP_IN_US);
# endif
# ifdef ESP32
esp_deep_sleep_start();
# endif
}
#endif
}