Smart water leak sensor

== Youtube video of the sensor ==
== Introduction ==
These smart sensors detect water leaks by measuring resistance between two contacts (two M3 bolts). If a water leak is detected:
* Local alarm goes off (loud piezo buzzer signals, blinking red LED)
* All other identical water sensors throughout the house also sound alarm (loud piezo buzzer signals, number of red LED flashes in a sequence = sensor ID of the sensor were the leak was detected). For this, WiFi and MQTT server are required
* Alarm is displayed in OpenHAB browser (or app) interface (on smart phones etc.). WiFi, MQTT server, and OpenHAB server are required.
* Email is sent to a predefined email address. WiFi, MQTT server, and OpenHAB server with the Mail action add-on are required.
After alarm goes off, pressing the button on any sensor will silence all alarms for predefined interval (30s by default). Only buzzer becomes silent, but the rest works as usual (water detection, red LED alarm, OpenHAB notifications, emails etc). After the predefined quiet time, if the water leak is still present, alarm again goes off on all sensors.
If water leak is no longer detected, alarm is canceled on all sensors.
Green LED turns on when WiFi connection is established. If no MQTT connection can be established, green LED slowly flashes.
If water sensor is shorted, piezo and red LED are chirping every 5s.
The sensor is powered via micro USB 5V power.
Total cost for the parts is ~6$.
== Schematics ==
== Components ==
* ESP8266 (WEMOS D1 Mini version): 4$, e.g. https://www.ebay.ca/itm/Mini-ESP8266-ESP-12-WeMos-D1-NodeMcu-Lua-WIFI-CH340G-Development-Board-Antenna/263710345463
* DC-DC step up boost converter MT3608: 0.45$, e.g. https://www.ebay.ca/itm/5pcs-MT3608-DC-DC-Step-Up-Power-Booster-Module-5V-28V-2A-Efficient-Board-TE686/202853262876
* Piezo buzzer SFM-27: 0.70$, e.g. https://www.ebay.ca/itm/1-5-10Pcs-SFM-27-High-Decibel-Alarm-Continuous-Sound-Electronic-Buzzer-DC-NNH/333171719082
* Power N-channel MOSFET transistor IRFZ44N: 0.16$, e.g. https://www.ebay.ca/itm/10Pcs-IRFZ44N-IRFZ44-N-Channel-49A-55V-Transistor-MOSFET/381375026221
* Micro switch: 0.27$, e.g. https://www.ebay.ca/itm/10pcs-Micro-Switch-Useful-Micro-Switch-Limit-Switch-for-Living-Room-Home-Bedroom/283799967360
* One 5mm red LED and one green/blue LED.
* Resistors (0.125W): 180 Ohm, 330 Ohm, 180 kOhm.
* A few small cone-headed screws, two M3 bolts and 6 nuts.
* Small sticky plastic feet from a dollar store, 4pcs.
== Software ==
https://github.com/pulsar123/Water_leak_sensor
== 3D printer design ==
https://www.thingiverse.com/thing:4195267
I used black ABS filament, 0.1 mm layers (so less need to sand), and 100% infill (for maximum strength, and to make it easier to drill out larger holes for larger screws, if needed).
== Building instructions ==
I simply glued the two LEDs to the top cover.
Make sure the red button goes through the corresponding hole in the top cover with very little friction. The micro switch should fit properly into the bottom part of the enclosure - not too tight, and not too loose. If it's not the case, you might need to edit the Fusion 360 project file and print it again.
Don't solder both buzzer wires to the step up voltage converter - leave it for the very end. Program the microcontroller with the "#define BUZZER_TEST" uncommented in config.h. In this regime, pressing the button would enable the buzzer (which is not connected yet). Attach voltmeter to the output of the step up voltage converter, press the button on the water sensor (you will see some voltage reading), and start adjusting the pot on the voltage converter until the output voltage goes to 23V (don't go over 24V, as this is the limit for the buzzer). This will ensure the loudest possible alarm without the risk of damaging the buzzer. Then power off the sensor, and solder both buzzer wires to the voltage converter. Power it up again, and test the buzzer by pressing the button.
At the end, comment out the "#define BUZZER_TEST". Make sure that each of your sensors have a different SENSOR_ID (in config.h), and that you provided the correct information for your WiFi network in private.h file (see instructions inside config.h). Then upload the code to the sensor again - now it should be fully functional.
After powering up, the green/blue LED should light up within a few seconds - that means it's connected to WiFi. It should be a steady light. If it's slowly blinking, the sensor couldn't connect to your MQTT server (see below). If the green/blue LED light is too dim or bright, you can change its brightness by changing the parameter LED_PWM for your specific SENSOR_ID in config.h (goes from 0: black, to 255: full brightness). If you have more than 2 sensors, add lines like these for each new sensor in config.h:
 #elif SENSOR_ID == 3
 const byte LED_PWM = 10;
 
 #elif SENSOR_ID == 4
 const byte LED_PWM = 10;
 ...
== MQTT and OpenHAB ==
See my detailed instructions for setting up MQTT and OpenHAB servers here:
https://pulsar124.fandom.com/wiki/Smart_light_switch#MQTT.2C_OpenHab.2C_and_Apache_servers
In addition to those instructions, you'll also need to install the "Expire binding" add-on for OpenHAB.
To add water sensors to your smart home, you can add the following lines to a *.items file inside your openhab/conf/items/ folder:
 /* Water sensors */
 String Water1_status "" <mywater> {mqtt="<[broker:water_sensor1/alarm:state:MAP(onoff.map)]"}
 Number Water1_pulse <empty> {mqtt="<[broker:water_sensor1/pulse:state:default]", expire="1m10s,NULL"}
 String Water2_status "" <mywater> {mqtt="<[broker:water_sensor2/alarm:state:MAP(onoff.map)]"}
 Number Water2_pulse <empty> {mqtt="<[broker:water_sensor2/pulse:state:default]", expire="1m10s,NULL"}
 ...
Place the three water icons (named mywater.png, mywater-off.png, and mywater-on.png) inside openhab/conf/icons/classic/ folder:
 
Add these lines in your sitemap file inside conf/sitemaps folder:
 Text item=Water1_status label="1: Utility room" visibility=[Water1_pulse!=NULL]
 Text item=Water2_status label="2: Laundry room" visibility=[Water2_pulse!=NULL]
 ...
 
Create text onoff.map file inside conf/transform with these lines:
 0=OFF
 1=ON
== Automatic alarm emails ==
Once you set up MQTT and OpenHAB servers, you can add more functionality to the sensor - e.g., a sensor alarm can trigger an email to a predefined email address, which includes the sensor ID.
This is how I did it:
* Install Mail action add-on in you OpenHAB instance: https://www.openhab.org/addons/actions/mail/
* Get a new Gmail account, to be used as the sender. (You wouldn't want to use your normal gmail account for this, as you'll store the gmail password as clear text on your computer.)
** Make sure you enable "unsafe third party applications" to access your new gmail account. (It is off by default.) : https://hotter.io/docs/email-accounts/secure-app-gmail/
* Edit the text file openhab/conf/services/mail.cfg . Set the following fields:
 hostname=smtp.gmail.com
 port=587
 username=your_gmail_account@gmail.com
 password=your_password
 from=your_gmail_account@gmail.com
 tls=true
* Create a file myrules.rules inside openhab/conf/rules with the following content:
 rule "WATER_EMAIL1"
 when
 Item Water1_status changed from OFF to ON
 then
 sendMail("your_main_account@gmail.com", "Water leak detected (1)!", "Water leak detected in sensor 1!")
 end
Repeat this for every sensor you have. Restart your OpenHAB server, and the next time alarm goes off you should receive an email.