Alex_NL
Posts: 3
Joined: Tue Jul 01, 2014 6:59 am

GPIO loop, stops

Tue Jul 01, 2014 7:42 am

Hello,
I ma running s script in rc.local (sleep 10;sudo python /etc/zonneregelaar.py) &
and this script check's two temperature probes (DS1820) and if the delta between the two is greater than one degree, GPIO 7 is turned on. If it is smaller, its turn off. Behind GPIO 7 is a relai that turn on a pump.

Some how and I do not understand, but when the state of GPIO had must change the scripts halts.
So lets say GPIO 7 is 0 but it has to change to 1, then the scripts halts.
The files are a checksum, to see how the script is handeling.

Code: Select all

import RPi.GPIO as GPIO  
import time
from datetime import datetime
n = datetime.now()
print n
file = open("/HD/log/zbstarting.txt", "w")
file.write("Datum tijd %s\n"%n)
file.close()

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.OUT)
while True:
    # Solorcollector
    while True:
        tfile = open("/sys/bus/w1/devices/10-00080278d076/w1_slave")
        text = tfile.read()
        tfile.close()
        secondline = text.split("\n")[1]
        temperaturedata = secondline.split(" ")[9]
        temperature = float(temperaturedata[2:])
        if temperature != -1250.0:
    		break
        time.sleep (.1)
    ZC = temperature
    print ZC
    # Solor Boiler
    while True:
        tfile = open("/sys/bus/w1/devices/28-000005e6d9ca/w1_slave")
        text = tfile.read()
        tfile.close()
        secondline = text.split("\n")[1]
        temperaturedata = secondline.split(" ")[9]
        temperature = float(temperaturedata[2:])
        if temperature != -1250.0:
	    	break
        time.sleep (.1)
    tempboiler = temperature
    print tempboiler

    delta = ZC - tempboiler
    print delta
    n = datetime.now()
    if delta > 1000.0 and tempboiler < 90000.0:
        print "delta greater than one 1, pomp on"
        GPIO.output(7, 1)
        time.sleep(.1)
        file1 = open("/HD/log/pomp.txt", "w")
        file1.write("pump on %s\n"%n)
        file1.close()
    elif delta < 1000.0:
        print "delta smaller than one 1, pomp off"
        GPIO.output(7, 0)
        time.sleep(.1)
        file2 = open("/HD/log/pomp.txt", "w")
        file2.write("pump off%s\n"%n)
        file2.close()
    else:
        # temperature boiler almost on 100 degree, safety on, pomp off
        GPIO.output(7, 0)
        time.sleep(.1)
        file3 = open("/HD/log/pomp.txt", "w")
        file3.write("pump off %s\n"%n)
        file3.close()
    time.sleep(30)
Does anybody know what I am doing wrong ?

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: GPIO loop, stops

Tue Jul 01, 2014 5:16 pm

Why are you opening same files (device) inside a loop?

Open it once read it many times inside a loop, then close when finished.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: GPIO loop, stops

Tue Jul 01, 2014 11:00 pm

Instead of running it from rc.local, run it from the command-line - that will allow you to see what error message is being displayed when your program crashes.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: GPIO loop, stops

Tue Jul 01, 2014 11:27 pm

Missed the rc.local bit, when run in background where do you expect the prints to appear?

Test from command line first.

When you want to convert to running in background put in all sorts of tests and error trapping for things like
  1. Unable to open or write to log file (make sure a message goes somewhere)
  2. Limit amount of times to read the sensor, log the reading and number of attempts or error log and exit if cannot take reading or device not present.
  3. log action performed
If running as background considering using syslog for fatal errors (log file, device not present and any exiting conditions).

Rereading your code you if/elif/else logic looks wrong
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
joan
Posts: 14179
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: GPIO loop, stops

Wed Jul 02, 2014 6:10 am

There is nothing wrong with the script. Not sure what the OP is actually seeing.

Alex_NL
Posts: 3
Joined: Tue Jul 01, 2014 6:59 am

Re: GPIO loop, stops

Wed Jul 02, 2014 7:00 am

I think I found the bug, I did a long test last night, and after a hour it crashed

Code: Select all

Traceback (most recent call last):
  File "zonneregelaar.py", line 29, in <module>
    tfile = open("/sys/bus/w1/devices/28-000005e6d9ca/w1_slave")
IOError: [Errno 2] No such file or directory: '/sys/bus/w1/devices/28-000005e6d9ca/w1_slave'
The prope was gone for a second and script stops

If I do this, would this be a good sollution ?

Code: Select all

    if os.path.exists("/sys/bus/w1/devices/28-000005e6d9ca/"):
        while True:
            tfile = open("/sys/bus/w1/devices/28-000005e6d9ca/w1_slave")
            text = tfile.read()
            tfile.close()
            secondline = text.split("\n")[1]
            temperaturedata = secondline.split(" ")[9]
            temperature = float(temperaturedata[2:])
            if temperature != -1250.0:
	        	break
            time.sleep (.1)
        tempboiler = temperature
        print tempboiler
    elif:
        ZC = - 100000.0 # safety,set temp collector to minues 100 to make sure pomp is turned off

User avatar
joan
Posts: 14179
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: GPIO loop, stops

Wed Jul 02, 2014 7:10 am

Yes and no.

Yes because it might catch 99% of the problems. No because the file could disappear after the exists check but before the open/read.

I'd put the file open/read/close in a try clause.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: GPIO loop, stops

Wed Jul 02, 2014 8:47 am

Alex_NL wrote:I think I found the bug, I did a long test last night, and after a hour it crashed

Code: Select all

Traceback (most recent call last):
  File "zonneregelaar.py", line 29, in <module>
    tfile = open("/sys/bus/w1/devices/28-000005e6d9ca/w1_slave")
IOError: [Errno 2] No such file or directory: '/sys/bus/w1/devices/28-000005e6d9ca/w1_slave'
The prope was gone for a second and script stops
Suggesting that for some reason the device disappeared, which could be anything from dry joint/intermittent wiring to requesting readings too fast. Please note unless you are dealing with sudden changes in temperature open of oven/furnace/freezer you rarely need to read temperature faster than once a second. Anyway the device takes 750ms to do a reading and you have a delay of 100ms
If I do this, would this be a good sollution ?
What you need espesially if running as background task

Code: Select all

 # Function to take a reading from a ds1820 and log errors
# open device passed in string like "/sys/bus/w1/devices/10-00080278d076/w1_slave"
# Up to 10 times attempt to take a reading
# if no reading log error and return 100
def read_ds1820( device ):
   try:
      tfile = open( device )
   except:   # insert conditions for error
      #insert logging of error
      #insert exit here
   # device opened take a reading up to 10 times
   loop = 10
   while loop > 0:
       try:
             text = tfile.read()
       except:    # insert conditions for error
             #insert logging of error
             #insert exit here
       secondline = text.split("\n")[1]
       temperaturedata = secondline.split(" ")[9]
       temperature = float(temperaturedata[2:])
       if temperature != -1250.0:
           break
       time.sleep (1)   # time delay of 1 second as device takes 750ms to do a reading
       loop -= 1
   tfile.close()
   if loop == 0 :
       temperature = 100000.0
       #insert logging of error
       #insert exit here
   return temperature

#main code
while True:
    # Solorcollector
    ZC = read_ds1820( "/sys/bus/w1/devices/10-00080278d076/w1_slave" )
    print ZC
    tempboiler = read_ds1820( "/sys/bus/w1/devices/28-000005e6d9ca/w1_slave" )
    print tempboiler 
    delta = ZC - tempboiler
    print delta
# rest of code but put boiler safety turn of first
if tempboiler >= 90000.0:
   # do safety shutdown
elif .......
Last edited by techpaul on Thu Jul 03, 2014 12:27 am, edited 2 times in total.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: GPIO loop, stops

Wed Jul 02, 2014 8:50 am

joan wrote:There is nothing wrong with the script. Not sure what the OP is actually seeing.
Hmm
  1. background task with no logging of device errors
  2. loop delay of 100ms for device that takes 750ms to do a conversion
  3. repeated block of reading and conversion that should have been a function
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: GPIO loop, stops

Wed Jul 02, 2014 11:51 am

Does your script run without relais?
-Spikes caused by your relais can halt your script.
-Use a second power supply for your relais.
-Avoid ground loops.
Success with your project!
Ferdinand

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: GPIO loop, stops

Wed Jul 02, 2014 11:12 pm

@techpaul In your read_ds1820 function you'll want to move that tfile.close() outside of the while loop, and you want to add a tfile.seek(0) before the tfile.read()
:)

"# if no reading log error and return 100" - how do you tell if a result of 100 is an error or an actual temperature reading? ;)

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: GPIO loop, stops

Thu Jul 03, 2014 12:20 am

AndrewS wrote:@techpaul In your read_ds1820 function you'll want to move that tfile.close() outside of the while loop, and you want to add a tfile.seek(0) before the tfile.read()
:)
The file close was originally indentation screw up by this place (now corrected)

You should only need to do a file.seek if the driver for that device really requires it which for that type of device would be unusual
"# if no reading log error and return 100" - how do you tell if a result of 100 is an error or an actual temperature reading? ;)
In the original logic anything over 90 was a shutdown so would make no difference, also the logic would not have worked in all cases. You could make it 200 or anything I was going from the limited code supplied originally.

Relying on a delta from a -100 to create this is prone to problems from getting two -100 readings

If one temperature when valid indicates an error then use one temperature only.

If you cannot get readings either shut down immediateley or use a time lag of repeated out of range readings, but neither case has been specified.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Return to “Python”