Water Detector
Create a water leak detector using the Raspberry Pi and Phidgets
Supplies
- Raspberry Pi
- VINT Hub
- Moisture Phidget
Setup
- Connect your VINT Hub to your Moisture Phidget.
- Connect your VINT Hub to your Raspberry Pi.
Overview
The goal of this project is to create a Python program that runs on the Raspberry Pi and does the following:
- Connect to Moisture Phidget
- Send an alert if water is detected by Moisture Phidget.
The flowchart above shows what the program should try to accomplish.
In this project, an alert will be sent via email. This will be accomplished using PycURL.
An interesting note is that many cellular providers have an email to text feature. This will allow you to send a text message to your phone!
Software Setup
Install the Phidget libraries on your Raspberry Pi. This tutorial shows how to do that.
Install the PycURL libraries by running pip install pycurl.
Write Code - Python
Copy the code below into new Python script:
from pycurl import * from Phidget22.Phidget import * from Phidget22.Devices.VoltageRatioInput import * import sys import time try: from io import BytesIO except ImportError: from StringIO import StringIO as BytesIO def alert(): mail_from = "MAIL FROM ADDRESS" mail_from_password = "MAIL FROM PASSWORD" mail_to = "MAIL TO ADDRESS" mail_server = "smtp://smtp.gmail.com:587" #this is for gmail message = \ "To: %s\r\n" \ "From: %s\r\n" \ "Subject: Leak Alert\r\n" \ "\r\n" \ "Water has been detected.\r\n" % (mail_to, mail_from) PY3 = sys.version_info[0] > 2 if PY3: message = message.encode('ascii') io = BytesIO(message) curl = Curl() curl.setopt(URL, mail_server) curl.setopt(MAIL_FROM, mail_from) curl.setopt(MAIL_RCPT, [mail_to]) curl.setopt(READDATA, io) curl.setopt(USERNAME, mail_from) curl.setopt(PASSWORD, mail_from_password) curl.setopt(SSL_VERIFYHOST, False) curl.setopt(SSL_VERIFYPEER, False) curl.setopt(USE_SSL, USESSL_ALL) curl.setopt(UPLOAD,True) curl.setopt(VERBOSE, True) curl.perform() #Create water = VoltageRatioInput() #Open water.openWaitForAttachment(1000) water_level = 0 #Use your Phidgets while (True): water_level = water.getVoltageRatio() print("Water Level: " + str(water_level)) if(water_level > 0.8): #if water is present, sent alert alert() time.sleep(1.0)
Code Review - Python
There are two sections to the code above:
- Alert function
- Phidget section
Let's start by reviewing the Phidget section:
Create
Here a VoltageRatioInput object is created and it is called water. This object will be used to interact with the Moisture Phidget.
Open
The water object is opened so that data can be collected from it.
Use your Phidgets
Here a simple while loop is used to poll the Moisture Phidget. The water level is recorded and printed on the screen every second. The range of values from the Moisture Phiget is from 0 to 1. The closer you get to 1, the more water is present on the sensor. If you have the sensor on hand you can test and evaluate what makes sense for you. Here we used 0.8 as the maximum water level before sending an alert.
Next let's take a look at the Alert section
Note: the alert function was formed by using the following resources:
Definitions
At the top of the method, all of the email information is configured:
- mail_from: this is the email address that the message will be sent from.
- mail_from_password: this is the password for the mail_from address
- mail_to: this is where you want the message to go. You can send it to an email, or you can actually text yourself (more on this later).
- mail-server: this is where you specify your mail server. An example for Gmail is provided.
After entering the information above, your email will send out when the condition is met.
Notes on security, safety, etc.
In this example, you are storing your email address password as plaintext on your device. There is information on the libcurl website about doing this in a more secure manner, but that is beyond the scope of this project. For this project, a burner email address was used which may be good enough for you (assuming educational purposes).
Receive Email
This is what your email should look like if you have the code working properly.
Text Message
As mentioned above, many (all?) major cellular providers have an email to text feature available. This means you can get a text sent to your device to let you know when something happens!
Next Steps
The code provided is fairly basic. Here are some things you will want to look into:
- Securely sending emails. This was mentioned above.
- Making your main loop smarter. You probably don't want to send an alert every second if water is hitting your sensor. If an alert is sent, maybe wait an hour before sending another one? Or implement a reset button on your Pi (use a DigitalInput + button) that tells the program it is ready to send alerts again.
Good luck!