Solving a Leaky Basement with Raspberry Pi

September 29 2014 02:00 PM

When my wife and I bought our first house a few years ago, we thought we had everything figured out. We moved in mid-September and, after an initial issue with the heat pump, everything went smoothly. But we discovered just how quickly that can change when our basement flooded the next Spring.

After the initial panic wore off, I discovered that the sump pump had failed, allowing the water level to rise and overflow the crock. I replaced the sump pump and assumed that all would be fine. A few months later, however, I arrived home to another soggy basement. This time, the sump pump had shaken itself against the side of the crock and had trapped the float switch into the off position.

We were fortunate both times in that I happened to catch the flood as it was starting, which allowed me to save the carpet and many of our belongings from ruin. While I took measures to prevent the sump pump from moving out of place, I knew that it was only a matter of time before it somehow failed again.

I needed a way to be alerted of an impending disaster before it happened. My first step lead me to a simple water level alarm like this one. This saved us from two more potential incidents, but it comes with a critical flaw: it is only effective if someone is around to hear it. If we are on vacation or even just out for the day, the basement may still flood and no one would know until it was too late. Since I always have my phone, I wanted something that could send an alert, via SMS or email, if something was going wrong. This would allow me to call a friend or neighbor to check on things and save the day even if I am across the country.

I played with a few ideas with an Arduino, but I was never satisfied with the networking options available to the platform. When the Raspberry Pi was announced, I knew I had finally found the device that I needed: a small, extremely low-powered Linux system with a full suite of the standard tools. In other words, it was cheap and reliable. I immediately preordered and (eventually) received one of the first shipment Model B devices.

Raspberry Pi Model B

The Milone Tech eTape Continuous Fluid Level Sensor is a printed, solid-state sensor with a resistance that varies in accordance with the level of the liquid in which it is immersed. No moving parts to get stuck! I used the MCP3008 Analog to Digital Converter and a custom differential amplifier circuit to interface the analog sensor output with the General Purpose Input and Output pins on the Raspberry Pi.

eTape Sensor

I wrote a simple Python script to periodically poll the current value of the eTape sensor. Since the output value from the ADC is a 10-bit integer (i.e.: between 0 and 1023), this was an appropriate place to convert the value into a depth (in inches). This script publishes the readings to a ZeroMQ topic.

Another python script subscribes to the topic. When the value exceeds a threshold, it sends an email to my wife and I alerting us of the issue. It follows up with another email when the value returns to normal.

A third script subscribes to the topic and archives the readings into a data file. I would like to use this data in the future to enhance the alerting capabilities. For example, if the sensor value swings wildly or remains relatively static for an unusually long period of time, it may indicate a malfunction. For now, this script merely collects the data.

Finally, because no sensor project is complete without charts, a final script subscribes to the topic and forwards all of the sensor readings to Xively. Xively provides a simple way for me to view the current water level and chart recent values from anywhere. The graph below shows the past 24 hours of readings taken.

Xively chart of water level ADC readings

Ironically, there has not been a single high-water event since building and installing this system, but knowing that this system is in place and functioning gives me significant peace of mind whenever I travel.