• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinoShowcasepcDuino will email you when water leakage happens
Previous Next

pcDuino will email you when water leakage happens

Posted by: Alvin Jin , January 25, 2014

In this tutorial, we show how to make a water leakage detector that is powered by pcDuino. When water is detected, pcDuino will send out email to the target email.

To detect water, we use the moisture sensor from DFRobot.  There are three pins of moisture sensor, GND, VCC, and S. S is connected to A2 of pcDuino, and GND of the sensor is connected to GND of pcDuino, while VCC of sensor is connected to 5V of pcDuino.

watersensor_1

Now, lets look at code part.

1. Download Python library:

1
$git clone https://github.com/pcduino/python-pcduino.git

2. After that, we will see the library files under pcduino. Create a directory named ‘waterdetect’ under directory ‘Samples’, the following is the code:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python
#-*- coding: utf-8 -*-
import smtplib
import time
from adc import analog_read
server= 'smtp.gmail.com'
port = 587
sender = 'xxxxx@gmail.com'
recipient = 'yyyy@linksprite.com'
password='xxxx'
subject = 'Water Leakage Detected'
body = 'Water detected at basement water heater 1.'
def delay(ms):
    time.sleep(1.0*ms/1000)
def setup():
        print "read channel ADC2 value ,the V-REF = 3.3V"
        delay(3000)
body = "" + body + ""
headers = ["From: " + sender,
           "Subject: " + subject,
           "To: " + recipient,
           "MIME-Version: 1.0",
           "Content-Type: text/html"]
headers = "\r\n".join(headers)
def loop():
    while(1):
        value = analog_read(2)
    voltage = (value * 3.3)/4096
        print ("value =  %4d"%value)
    print ("voltage =  %4.3f  V" %voltage)
        if voltage > 3.0 :
             session = smtplib.SMTP(server, port)
             session.ehlo()
             session.starttls()
             session.ehlo
             session.login(sender, password)
             session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
             session.quit()
    delay(1000)
def main():
    setup()
    loop()
main()

We will set up to run python script as daemon at boot time.

To do this, you must create this file and save it in /etc/init.d/. The file name we choose is ‘waterdetect’:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/sh -e
DAEMON="/home/ubuntu/python-pcduino/Samples/waterdetect/waterdetect.py"
DAEMONUSER="ubuntu"
DEAMON_NAME="waterdetect.py"
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
d_start () {
        log_daemon_msg "Starting system $DEAMON_NAME Daemon"
        start-stop-daemon --background --name $DEAMON_NAME --start --user $DAEMONUSER --exec $DAEMON
        log_end_msg $?
}
d_stop () {
        log_daemon_msg "Stopping system $DEAMON_NAME Daemon"
        start-stop-daemon --name $DEAMON_NAME --stop --retry 5 --name $DEAMON_NAME
          log_end_msg $?
}
case "$1" in
        start|stop)
                d_${1}
                ;;
        restart|reload|force-reload)
                        d_stop
                        d_start
                ;;
        force-stop)
               d_stop
                killall -q $DEAMON_NAME || true
                sleep 2
                killall -q -9 $DEAMON_NAME || true
                ;;
        status)
                status_of_proc "$DEAMON_NAME" "$DAEMON" "system-wide $DEAMON_NAME" && exit 0 || exit $?
                ;;
        *)
                echo "Usage: /etc/init.d/$DEAMON_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
                exit 1
                ;;
esac
exit 0

Do not forget to give execution rights to your python server and to the service script :

1
2
$ sudo chmod 755 /etc/init.d/waterdetect
$ sudo chmod 755 /home/ubuntu/python-pcduino/Samples/waterdetect/waterdetect.py

You can start or stop it using:

1
2
3
4
5
6
$ sudo service waterdetect start
 * Starting system waterdetect.py Daemon                          [ OK ]
$ sudo service waterdetect status
 * /home/ubuntu/python-pcduino/Samples/waterdetect/waterdetect.py is running
$ sudo service waterdetect stop
 * Stopping system waterdetect.py Daemon                          [ OK ]

and define it as a startup service using:

1
$ sudo update-rc.d waterdetect defaults

We install the detector at the washer machine:

IMG_2852

When there are water spill, we got the email:

IMG_2853

Tags: Showcase

Share!
Tweet

Alvin Jin

About the author

One Response to “pcDuino will email you when water leakage happens”

  1. Log in to Reply
    sorea
    September 20, 2014 at 10:22 am

    Hi, can the gsm module send notification to multiple phone numbers (instead of emails)? Can the user enter the phone number manually?

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors