Make a laundry-saving Rain Detector

By Lucy Hattersley. Posted

Save your washing from a soaking. This easy-to-build wire-free rain detector alerts you to sudden downpours

There’s nothing quite like clean air-dried clothes fresh from the line, unless an unexpected shower ruins everything. Ever heard that cry of “RAIN!” from a member of the household, only to be followed by the thundering of feet down the stairs in a desperate bid to save your Sunday best from another trip to the washing machine?

Catch the rain as soon as it starts with this simple standalone build that alerts your phone as soon as it detects raindrops. There’s no soldering required, just a few cables. We need low power consumption and WiFi, so this is a perfect project for a Raspberry Pi Zero W.

Rain Detector: What you'll need

 Here’s everything you need to build your rain detector.  Try to use a container with a rubber or silicone seal

When everything is assembled, it may be tricky later on to gain access to the Raspberry Pi. So, before doing anything else, install a copy of Raspbian Stretch Lite on an SD card (we have no need for a desktop) and insert into the Pi. It’s now time for the usual routine of updates and configuration. Get the Pi on your WiFi network at this point using raspi‑config and make sure you have enabled SSH access. Perform the usual ceremony of sudo apt update && sudo apt upgrade then reboot, check your SSH connection, and then power down.

Mount the sensors to the lid

You can use any number of sensors you wish, but two works well. Either secure the two plates to the lid using insulation or duct tape. Alternatively, 3D-print the enclosure pictured (STL files available from here) and secure with glue or sticky pads.

Two pairs of jumper cables need to be attached; one to each sensor plate. Polarity does not matter. The other end of the cables will have to thread into the container, so make as small a hole as possible in an appropriate place so the wires can get through, minimising the chance of any water ingress.

Connect the sensors to the controller

In order for the Raspberry Pi to understand what’s going on, a small controller board (supplied with the sensors) is required. This takes the small current that is shorted by water and converts it into a digital signal. Using the breadboard, connect the two pairs of wires from the sensors in parallel (so that either sensor could make the circuit) and then insert the controller’s receiving pins (the side with two connectors) into the breadboard so that each pin connects with one wire from each sensor.

 The sensor plates need to be connected to the controller board in parallel, not in series. That way, either one can trigger the alert. Polarity is unimportant

Connecting the controller

To complete our circuit, take a careful look at the four pins on the controller board. They will be marked as A0, D0, GND, and VCC. Using some jumper wires, hook the controller up to the Pi as follows: VCC to GPIO pin 2 (5 V), GND to any GND on the GPIO (e.g. pin 6) and D0 to GPIO 17 (pin 11). D0 and A0 are two different ways of reading output from the sensor. D0 is a straight digital on or off, the threshold being controlled by the variable resistor on the board. A0 is an analogue output that (when converted to digital) ranges between 0 and 1024 depending on how heavy the rain is.

 The controller takes the output from the two panels and converts it into a digital signal. This is then connected to the Pi, which also supplies 5 V to the controller

Assembling the Rain Detector

Connect the micro USB cable from your power bank to the power input on the Raspberry Pi and arrange everything inside your container. Ideally things shouldn’t move about, so keep everything in place with sticky pads or tack. You should now be able to seal the container with everything inside, the wires to the sensor plates coming out without being squashed or stressed. Once you’re happy, open it up and attach the power bank, then close it again and check your connection. The power bank, depending on its rating, should keep the Pi Zero W alive for a few hours at least.

 Make sure everything fits well, is secured and that the jumper cables are not going to pop out

Rain Detecting Software

Add the script at the end of this feature and save it as rainbot.py (or download from GitHub) into a convenient spot such as ~/pi/rainbot. Once in place, perform an initial test by running python3 ~/pi/rainbot/rainbot.py. You should see a readout every five seconds: ‘True’ if it’s dry, ‘False’ if it’s wet. Press CTRL+C to stop the script.

Pushover: get rain alerts on your mobile phone

To get alerts, we’re going to use Pushover, a neat one-time-payment notification service for smartphones (there’s a seven-day free trial). Sign up at pushover.net. When logged in, you’ll see a ‘User Key’; make a copy of this. Now follow the instructions to create an ‘Application Token’. You’ll be given an API key. Edit the script to replace the API key values, where prompted, with the keys you have been given. Make sure the Pushover app is installed on your phone.

Run the script again. This time, wet one of the panels slightly. A light should illuminate on the controller. If all is well, a few seconds later your phone will display an alert.

Run the Rain Detector automatically

Let’s set the script to run on startup.
Create the following file as a superuser:

sudo nano /lib/systemd/system/rainbot.service

Add in the following text:

[Unit]
Description=Rainbot
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/pi/rainbot/rainbot.py

[Install]
WantedBy=multi-user.target
Press CTRL+X to save and quit out of nano. Now issue the following commands:
sudo chmod 644 /lib/systemd/system/rainbot.service
sudo systemctl enable rainbot.service
sudo systemctl daemon-reload
Reboot the Pi. The script will start on reboot (although you won’t see any output). Test it with water again.

Make it your own Rain Detector improvements

There are lots of improvements that can be made, which we’ll leave up to you to explore. Pushover is convenient, but the function could be easily replaced with, well, anything you like. The frequency of checks could be altered (it’s currently every five seconds). How about adding an analogue to digital converter and use the A0 output to gauge how heavily it’s raining? It’s also a great start for a weather station project if you start recording the data. One useful addition would be adding a button for safe shutdown of the Pi after use.
from gpiozero import DigitalInputDevice
from time import sleep
import http.client, urllib.parse

# Some setup first:
APP_TOKEN = 'YOUR_PUSHOVER_APP_TOKEN'    # The app token - required for Pushover
USER_TOKEN = 'YOUR_PUSHOVER_USER_TOKEN'   # Ths user token - required for Pushover

# Set up our digital input and assume it's not currently raining
rainSensor = DigitalInputDevice(17)
dryLastCheck = True

# Send the pushover alert
def pushover(message):
    print(message)
    conn = http.client.HTTPSConnection("api.pushover.net:443")
    conn.request("POST", "/1/messages.json",
      urllib.parse.urlencode({
        "token": APP_TOKEN,    # Insert app token here
        "user": USER_TOKEN,    # Insert user token here
        "title": "Rain Detector",
        "message": message,
      }), { "Content-type": "application/x-www-form-urlencoded" })
    conn.getresponse()

# Loop forever
while True:

    # Get the current reading
    dryNow = rainSensor.value
    print("Sensor says: " + str(dryNow))

    if dryLastCheck and not dryNow:

        pushover("It's Raining!")
        
    elif not dryLastCheck and dryNow:
        
        pushover("Yay, no more rain!")
        
    # Remember what the reading was for next check
    dryLastCheck = dryNow

    # Wait a bit
    sleep(5)

 

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.