element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Open Source Hardware
  • Technologies
  • More
Open Source Hardware
Forum pcDuino will email you when water leakage happens
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Open Source Hardware to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 0 replies
  • Subscribers 315 subscribers
  • Views 171 views
  • Users 0 members are here
  • openhardware
  • arduino
  • pcduino
Related

pcDuino will email you when water leakage happens

alvin_jin
alvin_jin over 12 years ago

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

  • Sign in to reply
  • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube