Page 1 of 1

make script loop

Posted: Mon Apr 21, 2014 3:44 pm
by Anthonyfromtheuk
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?

Re: make script loop

Posted: Mon Apr 21, 2014 4:04 pm
by paddyg
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.

Re: make script loop

Posted: Mon Apr 21, 2014 4:08 pm
by Anthonyfromtheuk
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

Re: make script loop

Posted: Mon Apr 21, 2014 4:36 pm
by paddyg
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.

Re: make script loop

Posted: Mon Apr 21, 2014 4:52 pm
by croston
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/

Re: make script loop

Posted: Mon Apr 21, 2014 5:57 pm
by Anthonyfromtheuk
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! :)

Re: make script loop

Posted: Mon Apr 21, 2014 5:58 pm
by Anthonyfromtheuk
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.

Re: make script loop

Posted: Mon Apr 21, 2014 6:00 pm
by Anthonyfromtheuk
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/