How I Used Raspberry Pi to Detect Water Leaks in My Home
Tags: raspberrypi, javascript, tutorial
Published: 2022-04-21 17:44:33

When I realized there was a leak in my home, I knew it was time to put my expertise to work. I needed to call on my years of experience and passion for my job - as a software engineer? While at first glance, it may appear that a plumber would have been a better option, my argument is that there is a step to be taken before a plumber is even involved to fix a leak. And that is to detect the leak when it first happens. 

Sure, I could have a plumber on call who checks for leaks every day, but seeing as that’s not an effective use of time or money, we needed to find an alternative to waiting for the problem to become bad enough to become a major problem. So if a 24/7 plumber-butler wasn’t an option, I thought some kind of automated system would be more realistic.

As it turns out, I had been thinking for some time about how to use a Raspberry Pi with Courier to bring notifications to life. This happened to be a perfect use case to try it out, so I decided to invest in the appropriate hardware and make use of my own software development skills to make sure that I would get a notification any time there was even a small leak in my home. This way, I’d know to call the plumber and handle the problem before it became too big to handle.

## A leak can become an expensive problem
I myself had a water leak in my apartment some time ago. Because I didn’t know there was a problem until it was already too late, I had to call for a plumber when the damage was already done. Even for a simple leak like this, I ended up spending more than $500 to fix it. 

But things could have been much worse. Major damage is often caused by mold spores.
Water leaks may also cause structural damage to property. Walls start swelling and warping, leading to cracks and holes in them. Furthermore, water leaks may negatively impact the value of a home.

I never wanted to deal with the complications, so I decided to take matters into my own hands. But of course, it was important to find a solution that would be fun and interesting as well as useful. 

## Building alerts for water leaks using Raspberry Pi
During my research for a solution in the form of alerts for water leaks, I found that I could use a small, affordable [Raspberry Pi](https://www.raspberrypi.com/) single-board computer to achieve my goal. With this tiny computer, I could read signals from my water sensor and then send those signals to my monitor. 

To develop the project, which I called [potential-octo-lamp](https://github.com/suhasdeshpande/potential-octo-lamp), I first got all the hardware I needed to detect water leaks and connect to my computer. These devices included:

* [Raspberry Pi 4 Model B](https://www.amazon.com/dp/B07WYC73LF?psc=1&ref=ppx_yo2_dt_b_product_details)
* [Floor Water Sensor for Flood and Leak Detection](https://www.amazon.com/dp/B079YB1T8J?psc=1&ref=ppx_yo2_dt_b_product_details)
* [SanDisk 128GB Ultra MicroSDXC UHS-I Memory Card with Adapter](https://www.amazon.com/dp/B08GYKNCCP?psc=1&ref=ppx_yo2_dt_b_product_details)
* [GPIO Breakout Expansion Kit for Raspberry Pi 4B](https://www.amazon.com/dp/B08736NSPK?psc=1&ref=ppx_yo2_dt_b_product_details)
The next step was to build “potential-octo-lamp” to send alerts for any detected water leaks. I built the project to check for leaks from the sensor using JavaScript and TypeScript programming languages. When potential-octo-lamp detects one, it then sends the result to my phone using Courier and Twilio.

With [Courier](https://www.courier.com/), I could create custom alerts and define their titles and body text. I could also specify the communication channels through which the alerts should go and the recipients of these alerts. I also added a [Twilio](https://www.twilio.com/) integration on Courier to deliver the contents of my alerts through the channels I specified — in this case, SMS and email.

## How to use potential-octo-lamp to get alerts 
Luckily, I’m a really nice guy and have made potential-octo-lamp open source. You can use my program to protect your own home from water leaks. Once you have all the hardware requirements listed above, connect your Raspberry to your water sensor. If you need help setting them up, this [tutorial](https://www.youtube.com/watch?v=KwKiiWYQies) will help. Then you can begin using potential-octo-lamp to receive alerts for water leaks in five easy steps:

### 1. Create Your Courier and Twilio Accounts
If you don’t already have [Courier](https://app.courier.com/register) and 
[Twilio](https://www.twilio.com/try-twilio) accounts, you’ll need to sign up for both to configure your alert system. You may sign up for free on Courier and Twilio using an email address. Courier also allows you to use your Google or GitHub account to create an account.

### 2. Add Your Twilio Integration in Courier
[Start by getting](https://www.courier.com/docs/guides/providers/sms/twilio/) your Account SID, Auth Token, and messaging service SID from Twilio. Then log into your Courier account and go to [Integrations](https://app.courier.com/integrations). Add the Twilio info to their corresponding fields in Courier.

### 3. Retrieve Your Courier Authorization Token
When you create an account with Courier, you get an Auth Token so that you can safely make requests to and from Courier. Once logged in, you may retrieve your Courier Auth Token from the [API keys page in Settings](https://app.courier.com/settings/api-keys).

### 4. Clone potential-octo-lamp and Add Your Courier Auth Token
Next, go to GitHub and clone [potential-octo-lamp](https://github.com/suhasdeshpande/potential-octo-lamp). Because it’s unsafe to input your Courier Auth Token directly in your code, create a .env file and add it there. That way, it’s only visible to you.

### 5. Start the App
Finally, run the following commands one after the other to install dependencies and start checking for water leaks:

>> npm install

>> npm run build

>> npm run start

[Loom Video](https://www.loom.com/embed/6c977bc087fd4943b27fbda8c2069f65)

As soon as your floor water sensor detects leaks, potential-octo-lamp sends alerts for water leaks to the recipients you added on Courier. Then you can quickly repair the leak to prevent further damage.

## Alerts are your answer to urgent situations
Potential-octo-lamp is open-source, so you can clone it and use it yourself. Any improvements or suggestions you may have are also welcome. You can raise an issue or [reach out](https://www.linkedin.com/in/suhasrdeshpande/) to me if you’d like to contribute to the project!