Introduction: MQTT/Google Home Flood/Water WIFI Sensor With ESP-01
In this instructable, I will show you how to build wifi flood/water sensor with minimal cost. The entire project costs me less than $8 for parts that I acquire from ebay and my existing spare-parts.
In this project, we will use ESP-01 to provide Wifi and MQTT client to detect presence of water, and optionally use directly connected speaker/buzzer to provide localized alarm.
My specific application for project is to detect flood/water inside my sump pump well, in the case of sump pump failure. When water is detected by 2 open wires, it would send message to MQTT broker. MQTT broker would then relays the message to NodeRED. Upon receiving MQTT message, NodeRED would send announcement to multiple google home devices and also optionally send message to cellphone/browser via pushbullet
Now of course this project would only works if home electricity is ON. In the next instructable I will integrate the battery backup circuit. But if you make the power supply the same way I did, you can just plug in a USB power-bank for battery backup. If you have a power-bank that let you charge and supply power at the same time, then you're all set.
I use RaspberryPi ZeroW to host Mosquitto MQTT server and NodeRED. It has been running for over a year without any issue.
Raspberry Pi:http://www.switchdoc.com/2016/02/tutorial-installi...Install NodeRED on Raspberry Pi: https://nodered.org/docs/hardware/raspberrypi
Step 1: Parts That You Will Need
(2) 10K ohm Resistor
(1) small signal generic NPN transistor (I used 2N3904)
(2) long wires
(1) 5V generic power supply (this circuit requires less than 300mA current)
(1) 3.3V regulator module AMS1117
(1) Micro-USB To DIP Adapter Female Connector PCB Converter DIY Kit
(1) USB-A to MicroUSB cable.
(1) 8-pin IC socket - can be omitted if you want to solder ESP-01 directly to circuit board.
Cut the plastic bridges that create the gap between the rows, and then glue the 2 rows together, see photo.
(1) Small enclosure for the project
Below are optional parts if you need a localized alarm using speaker/buzzer
(1) Generic PNP Transistor, choose according to speaker/buzzer current/wattage requirement. In my case I use 2N2907 since my speaker is only 0.3W (8 ohm), it would provide enough power to drive the speaker. You could choose a larger transistor & speaker if you want louder sound.
(1) Speaker, see note on PNP Transistor above
(1) 100 - 110 ohm Resistor
Step 2: Circuit Diagram
The first step would be to create the circuit shown in the diagram.
I built the 3.3VDC power supply using an old 5V cellphone charger coupled with AMS1117 3.3VDC regulator. For the ESP-01 socket, I use an 8-pin standard IC socket, and cutting the plastic bridges that create the gap between the rows, and then glue the 2 rows together.
The circuit that I designed is to sense the presence of water between the two wires. When water reaches the tip of both wires, it would create a resistance of approximately 10K to 20K ohm. Then in series with the 10K ohm R1, it provides a small current to the base of the Q1 causing Q1 to saturate, clamping the GPIO-2 to ground. R1 is necessary to provide protection to Q1 in case there's an accidental short on the sensing wires.
R2 is a pull-up resistor to allow ESP-01 to boot from flash.
Now for the optional speaker/buzzer, if you just need ESP-01 to speak MQTT and don't want to implement this localized alarming, you can remove R2, Q2, Speaker, and place a 10K pull-up resistor between GPIO-0 and VCC.
If you don't feel the need of using the female Micro-USB to DIP adapter, you can solder wires between the 5V PS to the 3.3V regulator module. I prefer using the female MicroUSB adapter so I can use any generic cellphone charger and MicroUSB cable.
Step 3: Building the Circuit
Solder all components and parts into PCB according to the circuit diagram in the previous page, and cut the PCB to size.
Place the PCB inside an enclosure that would fit the PCB and the optional speaker. In my case, all the parts would fit inside a small phone outlet box, though I have to heat up the cover a little to create a bulge so that the ESP-01 module would fit.
Step 4: Flashing the ESP-01
In this step, we will flash the ESP-01 with arduino sketch. If you've never flashed ESP-01 module, you can follow my instructable to get you started: https://www.instructables.com/id/USB-to-ESP-01-Boa...
You can find my sketch in my github page: https://github.com/andyjawa/MQTT-Wifi-Water-Flood-...
In the sketch, at the minimum you need to change the following information pertaining to your home network/setup:
#define MQTT_SERVER "10.0.0.30"
const char* ssid1 = "SSID";
const char* password1 = "MYSSIDpassword";
const char* ssid2 = "SSID1";
const char* password2 = "MYSSIDpassword";
In my home network, I have 2 different access point that broadcast 2 different SSID's, and this sketch would allow redundancy by connecting to the next SSID if the communication to the current AP is lost. If you have only one SSID, populate both ssid1 and ssid2 with the same value.
Once you made the modification, upload the sketch into ESP-01, and plug in the ESP-01 into the interface board.
Step 5: Test Run
To test if our project works, the easiest would be to monitor MQTT messages in the network. To do that, you need to open an SSH session to the mosquitto broker and issue the following command:
mosquitto_sub -v -t '#'
The above command would allow us to see all MQTT messages coming into the broker.
Now power up our circuit, and if everything works, in a few seconds you should at least see the following MQTT message:
Now test the water sensor by dipping the 2 sensing wires into a cup of water, and you should see this message:
And if you take the wires out of the water, you should see this message:
If you see those messages, your project is a success.
I also included several useful MQTT topics in the sketch that you can use:
"stat/SumpWaterSensorInfo" : this message is sent every minute to provide uptime and other info.
"cmnd/SumpWaterSensorInfo" : ESP-01 will sends out info if it receives this topic with value of '1' (ascii=49)
"cmnd/SumpWaterSensorCPUrestart" : ESP-01 will restart if it receives this topic with value of '1' (ascii=49)
"cmnd/SumpWaterSensorBeep" : ESP-01 will sound the speaker if it receives this topic with value of '1' (ascii=49)
"cmnd/SumpWaterSensorBeepFreq" : Sets the frequency of the speaker alarm, default = 900 (Hz)
"cmnd/SumpWaterSensorDebug" : Enable and set serial debugging level (default is 0 - no debugging)
Step 6: Mount the Sensor
In my application, I want to monitor the water level inside my sump pump well, and to notify me if water reaches above the sump pump float switch, which means that my sump pump is not working. I ran the wires and use wire ties to secure it along the drain pipe.
Step 7: Final Touch
Now that we've got the project working and able to publish MQTT message to broker, the next step is to think of idea what to do with that.
In my project, I use Node-RED to listen/subscribe to the "tele/SumpWaterSensor" MQTT topic and announce to several google home speakers if water is detected. In addition to that, I also linked the flow to a pushbullet node to send notification to my android phone.
I also created a web front-end to see the status of the sensor (on/offline, uptime, etc). Sometimes I saw that it goes offline few times in the course of 1 week, from the stats, a lot of times it's due to ESP-01 getting disconnected from the wifi or MQTT. But not too worry, my sketch has included routine to restart the ESP-01 if it keeps on failing trying to connect to WIFI and/or MQTT broker.
The image on this step, shows the Node-RED flow to accomplish this. You can also paste the flow from my github page into your Node-RED: https://github.com/andyjawa/MQTT-Wifi-Water-Flood-...
Google home announcement is just one example for this project, but I think it's the most useful and practical. You can always interface to other MQTT listener, or even using IFTTT to drive other devices upon water being detected.