2023-12-06

Features

Water leak detector with WhatsApp
The water leak deteector with WhatsApp notification
  • Up to five attachable water sensors
  • Relay switch output for an alarm device (e.g. car horn)
  • WhatsApp notification in case of water being detected
  • Fancy OLED display for status information
  • Blinking LED (to see if the doodad still works)
  • Attractive transparent housing (used screw box from tool store)

Description

Motivation

The man's last refuge in the house is in our basement. This is the place where old computers are piled up next to construction materials and where power supplies, cables, transistors, resistors and some cellar spiders are at home. Where programming, tinkering, screwing and sawing takes place. Where cables and pipes hang from the bare concrete ceiling and aesthetic aspects obviously are of subordinate matter.
Down here in the man cave is where I keep all my collected "toys".

I don't want imagine how fatal a burst pipe or water leaking through a window during heavy rain could be. Regardless of whether insurance compensates for the damage, many treasures would be irretrievably lost if a significant amount of water enters unnoticed. - Where would I get a new CRT monitor today? Or 5.25 inch floppy disks? My old Walkman? And so forth ...
😦 Ugh! It's better not to let it get that far. A water detector is needed! And one which not only howls around here in the basement, but one to also notify me when nobody is at home.

Concept

Actually, a water detector is a fairly primitive device: when water shorts two electric contacts, then trigger an alarm.
You could even simply place uninsulated wire of a car horn on the floor as contacts, a few millimeters apart, and any water between them will do the rest to switch the horn on.
Or if you want it a little more elaborated, you can use inexpensive Arduino rain sensors for the job.
So this part of the general design is downright easy.

Things get more interesting with the notification function. How do you reach someone anytime, anywhere? Well sure, via cell phone!
My first idea was to get an old Nokia 3210 to send me a text message on command when water leaks. I still have a few of them in a box somewhere here and even a suitable M-Bus data cable with a serial interface that was used to remotely control the Nokia.
An old 3210 is quite a legendary piece. But there are also downsides to the SMS approach: you would need a dedicated SIM card for the water detector phone and beside of that, the GSM reception is very lousy down here in the basement. Most of the time there is no reception at all.
But what is available here is WiFi. And a left over WeMos D1 Mini module with an ESP8266 as a remain from a former project which was never realized.
This can do WiFi and has just enough I/O ports to connect to a few of the Arduino rain detector sensors. So let's take this to notify me on my cell phone via some messaging service.

The water leak detector

Sensors

Arduino rain sensor
Rain sensor for Arduino projects
As sensors for the water detector I used rain sensors for Arduino projects, which you can get on eBay for little money.
They work with 3.3V and, in addition to an analogue output they also have a digital output, the switching threshold of which can be adjusted using a potentiometer. Normally the digital output has 3.3V and when it comes into contact with water, it is pulled to 0V. That makes it ideal for inputs on the ESP8266 module.
To be on the safe side, I connected the ESP inputs to 3.3V using external 1.2kΩ pull-up resistors. - You don't want to get a false alarm from floating inputs.
The only other thing to note about the sensors is that the contact surface is made of untreated copper strips - whether these oxidize over time and then no longer conduct sufficiently when in contact with water remains to be seen. If in doubt, I just get new ones and tin them with a little solder.

Alarming

Installation in the basement
Installation in the basement
If water, then do noise.
This is how you could describe the program, somewhat simplified. To make sure that one can hear the noise in the entire house, I chose a horn with an integrated signal light as an alarm device. This makes a lot of noise, but also requires a lot of power. Much more than the ESP module could deliver.
That's why the water detector simply switches a small relay via a BC547 transistor, which in turn supplies the horn via a separate power supply.
This works well and is currently installed in this way, but I'm not yet entirely happy with this concept. - An alarm from a smoke detector could perhaps be a more elegant solution, as long as it can be powered by the ESP module from the 5V of the USB power supply.
So maybe I will change that in the future.

Since the D1 Mini only has a few I/Os, I had to connect the alarm output to RX. However, this pin is also used when programming the module via the Arduino IDE. To avoid having thousands of short alarms while the program is being uploaded, there is a breaker switch between the RX and the relay transistor - which also allows for muting the alarm during operation if necessary.

Display

Display
OLED display
A Cypax-project like this wouldn't be complete without a display. After all, you'll need a way to show what the status is; i.e. whether it worked to get into the WLAN, whether notification via WhatsApp worked, and so on.
Small 64x128 pixel OLED displays, which you can get cheaply and for which there are also Arduino libraries, are just perfect for the purpose. And besides of that, they glow nicely in the dark ;-)

Since the water detector will probably be quite bored for most of its life and there isn't constantly news to be displayed, the program completely inverts the display content every hour: what was previously lit now turns black and what was previously black is now lit. This isn't to combat boredom, but rather to prevent the display from screen burn-in (at least that's what I hope to avoid).

WhatsApp notification

Now for the interesting part.
In order to send a message to a smartphone, you first need a WiFi connection. Thanks to the Arduino libraries and numerous example programs, it was an easy exercise to integrate the ESP8266 into the home WLAN. I just adapted the code so that it regularly checks whether the WLAN connection still exists and, if not, retries connecting.

Which messaging service?

How do you get the information that the basement is under water on your cell phone?
One option, that would not require any external services, would be to run a small web server on the ESP and the cell phone would then continuously query this and give a signal when there is a water alarm. Kind of like an RSS feed. But conceptually that wouldn't be very smooth. It is better to use a push service, i.e. the cell phone is actively informed by the ESP as the case arises.

There are many messaging services and a few of them I use on my cell phone. My least favorite WhatsApp is actually and in fact the one service that has always worked reliably and is not put to sleep by my Android when the App is not in use.
Besides, there are also a number of free providers that have ready-to-use APIs and even Arduino code examples for the ESP8266 to send WhatsApp messages (see links below).
I've choosen Whatabot as service provider - but the code should also work just as well for CallMeBot.

The concept is: you send a WhatsApp message to Whatabot from your phone in which you allow WhatsApp messages to be sent to your mobile phone number and you receive an API key in response. Then you call a URL from the ESP8266 with the API key and the desired message as parameters and Whatabot will send the questioned message via WhatsApp to the cell phone that was used to generate the API key.

This makes the realization of the notification feature pretty easy.
However, it should be noted that the ESP establishes a secure SSL connection to the Whatabot server. For this the WiFiClientSecureAxTLS library is integrated into the code, which - as of December 2023 - only works with version 2.7.4 of the ESP8266 board manager in the Arduino IDE. Not with higher versions. Thus, you may have to downgrade your ESP8266 board manager for this project.

Of course, such an API service on the Internet might sometimes be unavailable, so I adapted the code for the water detector such that if the WhatsApp message is not sent, it tries again regularly as long as the alarm status lasts.
In addition, a WhatsApp message is also sent when the power is on. - This way you know whether sending messages actually works without having to cause a water alarm first.

The documentation

The code is documentation enough.

No seriously. The entire program consists of less than 700 lines, the majority of which are explanatory comments. All functions are described, at the beginning of the file there is a section of definitions / constants that you have to adapt specifically for yourself (WLAN SSID, WLAN password, API key, ...), a section of values that you can adapt (repetition intervals in the event of an connection error, duration of alarm activation, ...) and a detailed description of the general program flow along with a circuit diagram in ASCII art.
So you'll get along well with it 😉

Links

APIs: Useful resources:

Licence and notes

WhatsApp is a brand of the Meta Platform. I receive no compensation from Meta / WhatsApp or the providers of the APIs for this project.
The water detector circuit and code are trivial; I put it under Public Domain (CC0).

Download

Download of the Arduino code: waterdetector.ino (approx. 18 kB) - includes schematics as ASCII illustration