2017-02-09



Over the Christmas break, my parents were out of town to celebrate the holidays. When they came back, they saw something every homeowner fears – a flooded basement. In a basement, there are pipes that collect water from around the foundation and bring it into the sump pit, which pumps it out away from the house. When the sump pump fails, water has no way to leave the basement and it floods.

I recently bought my first house, so this horror story is still weighing heavy on my mind. Our basement has a sump pit, but there isn’t a pump in the hole. Being the paranoid person I am, I saw two options: I could either poke my head down in the basement every time there’s a storm, or I can place a sensor near the pit to alert me.

Water alarms are nothing new; there are plenty of cheap ones on the market that will alert you with a loud piezo alarm when water touches the two sensing pads. But what if you’re out of town, or just out of the house? For that reason, I decided to make the ESP32 Water Alarm. Just like a standard water alarm, there are two electrical contacts that sense the presence of water, but instead of turning on audible alarm, it will send you a text message.

How does it work?

For this project, you can use either the ESP8266 Thing, or the ESP32 Thing. One of the many improvements made on the ESP32 is the capacitive touch circuitry built in to ten of the IO pins (we can see the pins capable of capacitive touch in the datasheet below). It should be noted though, that Touch1 cannot be used as capacitive touch sensor because of the pull-up resistor connected to GPIO pin 0. With the capacitive touch, we’ll be able to sense water with just two pins; the first will be connected directly to a capacitive touch pin, and the second will be connected to ground.



To receive a text message, I used the free (and beautifully simple) service If This Then That (IFTTT). IFTTT gives you a huge amount of control, from devices like smart switches and thermostats, to sending or receiving emails, to interacting with Twitter and much more. I combined two services: Maker, which receives a HTTP post from the ESP32, and SMS, which will send an SMS text message to the phone number you provide.

Setting up IFTTT

To make the IFTTT applet, I created a free account and selected “New Applet” from the pull-down menu next to the user name. I clicked on “+this” and searched for “Maker.” I then selected “Connect,” followed by “Receive a web request.” The ESP32 will issue a web request for an event trigger, which I called “ESP,” so whenever the ESP32 senses water it will trigger the SMS message. I then clicked on “+that”, and searched for “SMS.” Again, I clicked on “Connect,” entered the phone number I wanted to send a message to and pressed “Send PIN.” I entered the pin that was sent, and pressed “Connect” again. I next chose “Send me an SMS,” set the message to “The water level is {{Value1}}” and created the action, which is all I need for IFTTT to text my phone.

“Value1” is a JSON object I can fill in from the ESP32 to let me know whether the water is high or has returned back to low. The only other piece of information I needed was the key to my Maker channel, which are the random letters and numbers at the end of the URL of the Maker settings. I used this key in the code that I put on the ESP32 Thing. You can see where your code will be in the image below.



ESP32 Code

The code is relatively simple and straightforward; you can see it here. If you’d like to create your own ESP32 Water Sensor, or just want to figure out how to program the ESP32 from the Arduino IDE, check out this hookup guide.

The ESP32 connects to my wireless access point and the connection is checked at the beginning of the main loop. Next, it checks the water sensor a few times by calling touchRead(pin), and the readings are averaged together to minimize any false readings. If there is a change in water presence, the ESP32 sends the request to IFTTT, which then texts me. The request is made using an HTTP POST method containing the key we received from Maker and the value of the water sensor. With the code uploaded, I’m able to open my serial monitor, make sure I’m connected to the local wireless access point and test it out.

Changes I’d Like to Make

While I’m happy with how easy it was to get working, there’s been one problem that’s been staring me in the face: What happens when the power is out? If there’s a severe enough storm that the sump pit is filling with water, there’s a real chance that I might also lose power. I could attach a backup battery and sense if the power is out, but that means my wireless router and modem are also most likely without power. And what if the power is out because a tree fell on the power line? In that case, even if I had backup power for my modem and router, the the cable line on the pole is also likely damaged. For those reasons, I’d really like to eventually add a cellular shield with a preloaded SIM card to serve as a backup in the event of a power outage. Honestly, going with the cellular shield and some sort of pay-as-you-go SIM card would probably be a safer and more reliable option. But if you’d like to minimize the cost, the ESP32 is a great way to go.

comments | comment feed

Show more