Water Leak Detector: Difference between revisions
No edit summary |
No edit summary |
||
Line 6: | Line 6: | ||
<center>{{#ev:youtube| | <center>{{#ev:youtube|U4sqfvTcpC8|||||rel=0}}</center> | ||
==Parts== | ==Parts== |
Revision as of 17:01, 26 June 2023
Introduction
If you’ve ever worried about coming home to a flooded basement, this project is for you. We will be creating a water leak detection system that will send you an email if a water leak is detected.
Parts
This project utilizes the following hardware:
If you haven't used a Raspberry Pi with Phidgets before, check out this article.
Software
A simple Python program will run on the Raspberry Pi. It will perform the following task:
- Create a Voltage Ratio object, and map it to the Phidget Soil Moisture Sensor.
- Monitor the reading from the sensor and send an email if water is detected.
The program will execute every 30 minutes through a cron job.
Email Configuration
We recommend creating a new Gmail account for this project. In order to send emails from an app, you will need to enable 2-step verification in your account settings:
Next, generate an app password that you can paste into the code below:
Code
import smtplib
import ssl
from datetime import datetime
import time
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
from Phidget22.Devices.Log import *
from Phidget22.LogLevel import *
from Phidget22.Net import *
#Email Init
SMTP_SERVER = "smtp.gmail.com" #Change this if you are not using GMAIL
SMTP_PORT = 465 #Change this if you are not using GMAIL
USERNAME = "YOUR EMAIL@gmail.com"
PASSWORD = "YOUR PASSWORD - FOR GMAIL, THIS IS YOUR APP PASSWORD"
TO = USERNAME #Set a recipient
#Other Config
SENSOR_VALUE_DANGEROUS = 0.3 #Determine this value through testing
MINS_30 = 1800
#Send an Email
def sendMessage(time,voltageRatio):
context = ssl.create_default_context()
with smtplib.SMTP_SSL(SMTP_SERVER,SMTP_PORT,context=context) as server:
server.login(USERNAME,PASSWORD)
message = """Subject: Water Leak Detected!
A water leak has been detected!
------------details------------
Time: {0}
Voltage Ratio: {1}
-------------------------------
This email will repeat every {2} minutes until the voltage ratio drops below the threshold of {3} V/V.
""".format(time,voltageRatio,int(MINS_30/60),SENSOR_VALUE_DANGEROUS)
server.sendmail(USERNAME,TO,message)
def main():
#Create
waterSensor = VoltageRatioInput()
#Open
waterSensor.openWaitForAttachment(2000)
#Get current time
now = datetime.now()
#If water level is dangerous, send an email
if waterSensor.getVoltageRatio() >= SENSOR_VALUE_DANGEROUS:
sendMessage(now.strftime("%m/%d/%Y, %H:%M:%S"),waterSensor.getVoltageRatio())
main()
Schedule with Cron
After adding the Python file to your Raspberry Pi, create a cron job that runs every 30 minutes:
Result
You should now have a system that will send out an email when water is detected!