Anthonyfromtheuk
Posts: 17
Joined: Sat Mar 29, 2014 3:28 pm
Location: UK

make script loop

Mon Apr 21, 2014 3:44 pm

I have this

Code: Select all

#!/usr/bin/env python

from time import sleep
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
state = GPIO.input(23)

if (state is False):
    print "Water detected!" 
else:
   print "Water not detected"
Hope this makes sense...What I would like is for "else" to make the script check the state again. So it will just loop until the state is false when it will print "Water detected!"
have found some examples online but they are a bit confusing to me, can anybody help please?

User avatar
paddyg
Posts: 2359
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: make script loop

Mon Apr 21, 2014 4:04 pm

If you do

Code: Select all

while GPIO.input(23):
  time.sleep(1) # don't print anything as you will get lots of lines, pause the maximum resolution you can get away with
# GPIO.input(23) must become false in order to drop out of loop
print "Water detected!"
If you need your program to do other things you will have to set up something more sophisticated than this but you should make sure you understand what's going on first.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Anthonyfromtheuk
Posts: 17
Joined: Sat Mar 29, 2014 3:28 pm
Location: UK

Re: make script loop

Mon Apr 21, 2014 4:08 pm

Got it working the way I want :D
now i can just run it once and if water is detected i will get a text message

Code: Select all

#!/usr/bin/env python
import os, glob, time, sys, smtplib
from email.mime.text import MIMEText
from time import sleep
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
state = GPIO.input(23)
fortext= "Grow Bed water level is to high"
while True:
    if ( GPIO.input(23) == False ):
        print "Water detected!"
        USERNAME = "[email protected]"
        PASSWORD = "MYPASSWORD"
        MAILTO = "[email protected]"
        msg = MIMEText(fortext)
        msg['Subject'] = '#test'
        msg['From'] = USERNAME
        msg['To'] = MAILTO
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo_or_helo_if_needed()
        server.starttls()
        server.ehlo_or_helo_if_needed()
        server.login(USERNAME,PASSWORD)
        server.sendmail(USERNAME, MAILTO, msg.as_string())
        print "Text Message sent"
        server.quit()
        break

User avatar
paddyg
Posts: 2359
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: make script loop

Mon Apr 21, 2014 4:36 pm

Anthony, good. I hope the water being detected isn't anything catastrophic such as flood water entering your front door. Or if it is, that you never get that text message.

As sending a text message doesn't sound like a time critical control function you might want to put a sleep into your while loop outside the if block. Without that the processor will be running flat out.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: make script loop

Mon Apr 21, 2014 4:52 pm

Your program would be much better if it used GPIO.wait_for_edge() rather than polling. A full explanation is here:

https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/

Anthonyfromtheuk
Posts: 17
Joined: Sat Mar 29, 2014 3:28 pm
Location: UK

Re: make script loop

Mon Apr 21, 2014 5:57 pm

I will check it out, didn't really think about it taxing the processor. Its all new to me! but good fun none the less when its working! :)

Anthonyfromtheuk
Posts: 17
Joined: Sat Mar 29, 2014 3:28 pm
Location: UK

Re: make script loop

Mon Apr 21, 2014 5:58 pm

It is to detect overflow of an aquaponic grow bed.
paddyg wrote:Anthony, good. I hope the water being detected isn't anything catastrophic such as flood water entering your front door. Or if it is, that you never get that text message.

Anthonyfromtheuk
Posts: 17
Joined: Sat Mar 29, 2014 3:28 pm
Location: UK

Re: make script loop

Mon Apr 21, 2014 6:00 pm

That seems useful, I intend to have this running as a background process from startup so I will have to figure out how to use that wait_for_edge
croston wrote:Your program would be much better if it used GPIO.wait_for_edge() rather than polling. A full explanation is here:

https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/

Return to “Python”