Introduction: Flood Detection

About: I spent my career in computer networks. Now retired I am interested in topics that work my neurons. I am passionate about home automation and site administration that I host at home. I acquired skills in the f…

Installation of a leak or water intrusion detection probe. I'm using the assembly to monitor the tightness of my water heater and the presence of water in my garage following heavy rain.

Features:

  • Quick detection of the presence of water
  • Battery level monitoring.
  • Very low current consumption for long battery life.
  • Use of native ESP12F functions (non-volatile memory, power supply voltage).
  • Choice of two programs for uploading information.
  • Connection to an MQTT server for processing by a home automation server such as "openHAB", "homeassistant" or similar.
  • Send an email message.

Supplies

Step 1: Assembly Description

All the project files (Arduino programs and schematics) can be downloaded here.

The heart of the system is an ESP12F module. It reads the sensors and communicates via its Wifi interface.

It also monitors the presence of water and the battery charge level.

O => Power switch

A => Water presence detector. When water is present, the Gate becomes positive and the CMOS transistor switches. The voltage at (A) is also transmitted via a diode to the GPIO05 port to be considered by the controller.

C => When the 2N7000 circuit switches, the voltage goes to zero and a negative pulse is transmitted, via a capacitor, to the RESET port (D) and the ESP12F starts up.

B => The push button toggles the CMOS circuit and in the same way starts the ESP12F.

E => The GPIO01 port is connected to the RESET port via a diode. The controller's "deep sleep" function activates the port at the end of the sleep period to restart the controller (via a negative pulse).

H => The controller board is fitted with resistors which are essential for the programming and deep sleep functions. If the board is not to be used, the two 10k resistors should be added.


G, F => Connecting the USB-TTL converter is very simple. Only the GND, RX and TX links are used. Programming mode is as follows: Switch off the power supply (O), press the switch (F) and switch the power supply back on. The controller then waits for the new program to be downloaded. In production mode, the converter is not required.

The battery level is checked by an internal controller function (see program).

Step 2: Special Assembly Features

Assembly is simple.

When using a test board with plug-in connectors, it is important to ensure the quality of the contacts and the use of short links. It may happen that the controller does not start or falls into an unstable state. This is usually due to the wiring. So don't panic - check the quality of the connections.

During operation, the programme is started by the presence of water or after the "deep sleep" period, which is set at 1 hour.

The programme checks for the presence of water (pin 5) and/or the voltage of the power supply and/or whether the hour counter has been exceeded (24 hours in our case).

In all three cases, if a limit is reached, the programme sends the information by activating the WiFi.

Programmes

I have developed two programmes with different targets:

  • Sending information to an MQTT server for use by a home automation server.
  • Sending information to an email user.

Power consumption

In deep sleep mode, power consumption is 6µA.

Consumption rises to 18mA for 3s when the programme is started and no anomaly is detected.

When the programme connects via WiFi, power consumption rises to 80mA for 10s.

Deep sleep and timer

The maximum duration of a deep sleep state on the ESP12F is 71 minutes. For the programme it is set to 60 minutes (slightly more).

A counter (hourcounter) is incremented every time the controller wakes up, i.e. every hour. The value is then stored in the controller's non-volatile memory to be read again the next time the controller wakes up.

When the counter reaches value 24, the Wifi connection is started, and the information is sent.

This daily reading ensures that the device is still working.

Serial port speed

It is set to 74880 (like is the Arduino console port) to match the default speed at controller start-up. System start-up errors are then readable (messages before program start-up).

Step 3: Arduino Program for MQTT

The program ("ESP12F_water_alarm_cop" directory) consists of two parts: the main program and the set of functions.


The functions have been taken from Github (https://github.com/256dpi/arduino-mqtt ), where detailed explanations can also be found.

The main program, with its annotations, is simple and easy to read.

Added functions

TestCurrentTime queries a web site to find the date and time.

WriteCounter writes the hour counter value to non-volatile memory.

ReadCounter reads the hour counter value from non-volatile memory.

Specific ESP APIs: ESP.rtcUserMemoryWrite, ESP.rtcUserMemoryRead, ESP.getVcc are used to store the hour counter and read the battery voltage.

Information sent

  • ProbeWaterStatus indicates the presence of water
  • ProbeBattery indicates the battery voltage
  • ProbeTimeOne indicates the time (hhmm)
  • ProbeDate indicates the current date (yyyymmdd)
  • ProbeWifiLevel indicates the Wif signal level (in Db)
  • ProbeWifiTime indicates the time taken to connect to Wifi (in s)

openHAB integration

I use an openHAB home automation server. It is connected to the MQTT server and retrieves information to be processed by rules.

When information arrives, I use the following rules:

Detect water or low battery and send a message: File rulestest.rules

Check whether the sensor is still operational (Jruby language): the current date is compared with the last date received. If the dates differ, then the sensor has a problem because it should send the date once a day: File chauffeeau1.rb

Step 4: Arduino Program for Email

The program ("ESP12F_water_alarm_email_cop" directory) consists of two parts: the main program and the set of functions.

The functions are taken from Github (https://github.com/mobizt/ESP-Mail-Client ), where detailed explanations can also be found.

The main program with its annotations becomes easier to read.

Step 5: Conclusion

In this simple project I wanted to highlight some little-known programming ideas (non-volatile memory, battery level measurement) as well as the handling of the "deep sleep" function and the relationship with a home automation server via an MQTT server or sending a mail.

If this gives you any ideas, I'd be delighted. "Enjoy it.

Sources of inspiration:

https://docs.arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device

https://github.com/256dpi/arduino-mqtt

https://github.com/esp8266/Arduino/blob/master/libraries/esp8266/examples/RTCUserMemory/RTCUserMemory.ino

https://github.com/espressif/esp-idf/blob/master/components/nvs_flash/include/nvs.h

https://arduino-esp8266.readthedocs.io/en/latest/libraries.html#esp-specific-apis

https://github.com/mobizt/ESP-Mail-Client