359
Posts: 16
Joined: Thu May 29, 2014 7:55 pm

Script fails after some hours

Tue Apr 21, 2015 10:45 am

Hi

I need to post a command from my pi 2 to a lan pc depending on state of 2 gpio pins.

Ignoring the gpio states, if I fire off a command using curl from the pi, at any time, works great. Payload of 500 sets it on, payload of 100 set it off, no issues ever with curl so I am sure the fundamentals work.

Then running this python script using gpio states via cron -e at boot to watch the gpio states works fine for maybe a few hours, sometimes days. But then it fails. Damn it! btw, the gpio are connected to go high if a momentary switch connects them to 3.3v, this also all works fine initially.

I am a total noob to python, looking for some pointers how to proceed, log errors (?) maybe or add some sort of watchdog? Am I missing some obvious aspect to the code to prevent it failing?

Thanks for any ideas

Code: Select all


#!/usr/bin/env python

# GPIO 23 & 24 set up as inputs. Both are pulled down.
# 23 and 24 will go to 3V3 (3.3V) when momentary button is pushed.

# 23 is POST ON
# 24 is POST OFF

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import requests

# handle the button event
def my_callback(channel):
	
	time.sleep(0.5)
	
	if GPIO.input(23):
		payload = {'relay': '500'}
		r = requests.post("http://192.168.0.2:80/relay", data=payload)
		time.sleep(2)
		
	else:
			if GPIO.input(24):
				payload = {'relay': '100'}
				r = requests.post("http://192.168.0.2:80/relay", data=payload)
				time.sleep(2)

# setup pins
    
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)

# tell the GPIO library to look out for an 
# event on pins deal with it by calling 
# the buttonEventHandler ON/OFF functions
    
GPIO.add_event_detect(23, GPIO.RISING, callback=my_callback, bouncetime=5000)
GPIO.add_event_detect(24, GPIO.RISING, callback=my_callback, bouncetime=5000)

while True:
	
	time.sleep(2)

GPIO.cleanup()       # clean up GPIO on CTRL+C exit



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

Re: Script fails after some hours

Tue Apr 21, 2015 3:21 pm

@359 I can't see anything obvious but I would start by littering the code with print()s to try to track down what bit has stopped working and when (add time.time() to prints)

PS in python you can do else: if ..: in one with elif ..: so you could

Code: Select all

def my_callback(channel):
   
   time.sleep(0.5)
   
   if GPIO.input(23):
      payload = {'relay': '500'}
   elif GPIO.input(24):
      payload = {'relay': '100'}
   r = requests.post("http://192.168.0.2:80/relay", data=payload)
   time.sleep(2.0)
By the way, you say that the pins go high "momentarily" but by waiting half a second then checking the inputs you stand a good chance of missing the signal. Is this intentional i.e. is there any reason why you don't use the channel with which the callback has conveniently been provided?

Code: Select all

   if channel == 23:
      payload = {'relay': '500'}
   elif channel == 24:
      payload = {'relay': '100'}
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Script fails after some hours

Tue Apr 21, 2015 4:01 pm

How does it fail?

In other words, what does 'fail' mean?

Does the RPi reset? Does the code just stop running? Do you get an error message? Does the callback become unresponsive? Does the callback stop working?

If you use htop to view the processes while running correctly you will probably notice that your callback is running in a separate thread. Does the main problem continue, but the thread dies? Does the whole thing die?

Perhaps your logs are filling? Perhaps you have an intermittent hardware failure. Most of the time intermittent failures that cannot be reproduced precisely are usually hardware related; or timing related... which is also often hardware.
marcus
:ugeek:

359
Posts: 16
Joined: Thu May 29, 2014 7:55 pm

Re: Script fails after some hours

Tue Apr 21, 2015 9:33 pm

paddyg wrote:..you say that the pins go high "momentarily" but by waiting half a second then checking the inputs you stand a good chance of missing the signal. Is this intentional..
Hi paddyg. The pins are connected to either side of the same 2 position momentary switch. It appears very 'noisy', lots of false edges, this was one way around it. The post command only fires off if the button is confirmed held down for that half second then released, am using the edge to start the callback but then the extra check before the command goes

Thanks for the idea of adding prints with times, sounds like a good start.
MarkHaysHarris777 wrote:How does it fail?
Hi, after some random period, can be and hour or 12 or 24, the button push doesn't fire off the command. The pi is totally on and fine, other things are still running and responsive, just this script. If I then reboot, all ok again for another period.

I hadn't heard of htop, had a quick look and that's really useful. I'll reboot the pi, check my script is running then after it fails again, see if it is still in the list?

Is there some sort of software way to have a watchdog watch my script and restart it if it stops? Not even sure this makes sense (!), very much beginning in this. Thank you for the replies

MarkTF
Posts: 293
Joined: Tue Mar 03, 2015 4:59 pm

Re: Script fails after some hours

Tue Apr 21, 2015 9:43 pm

If your "requests.post" statement fails for whatever reason, you'll get an unhandled exception and the program will terminate. You might try putting that statement inside a "try / except".

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Script fails after some hours

Tue Apr 21, 2015 9:48 pm

get htop like this:

sudo apt-get install htop

htop my be configured to show threads in a different color... also, you can view threads in a 'tree' with f5 toggle, or you can configure the 'tree' option on all the time.

Just for your awareness, push-buttons are a terrible way to trigger an interrupt; the reason is contact bounce and crappy hysteresis. If you want a push-button to trigger an interrupt (rising edge or falling edge) then you want to condition the button to eliminate the contact bounce, and to provide a clean hysteresis for the sensing electronics.

In my own projects I tend to use an NE555 timer. Its an 8 pin chip that can be configured to send a single timed pulse based on an event like a sensor or pushbutton. See these:

http://www.555-timer-circuits.com/

http://www.555-timer-circuits.com/operating-modes.html

Read about the monostable mode... often called a one-shot. Basically it works by getting a brief signal (your button push, ground shot to the 555) and then it responds by producing a very clean timed single pulse out. The input is on pin 2 the output is on pin 3. The timing is controlled by a resistor and a capacitor. The timing can be just micro seconds, or seconds, up to minutes. It is a VERY versatile chip... maybe the most versatile chip in the entire industry.
marcus
:ugeek:

359
Posts: 16
Joined: Thu May 29, 2014 7:55 pm

Re: Script fails after some hours

Tue Apr 21, 2015 10:48 pm

MarkTF wrote:If your "requests.post" statement fails for whatever reason, you'll get an unhandled exception and the program will terminate. You might try putting that statement inside a "try / except".
V interesting, hadn't thought about this. Will add a try/except like you mention
MarkHaysHarris777 wrote:..snip
This is all completely new to me, also very interesting. I shall read up, appreciate your help!

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

Re: Script fails after some hours

Wed Apr 22, 2015 6:53 am

@359, hope you track down the issue. I also noticed that you have no way out of your loop apart from CtrlC which will just stop the execution without doing the gpio cleanup. Normally people put the while loop in a try and catch the CtrlC exception to do the cleanup. Alternatively you could use keyboard input and check the character (you're not worried about blocking the while loop as you use callbacks)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

359
Posts: 16
Joined: Thu May 29, 2014 7:55 pm

Re: Script fails after some hours

Sun Apr 26, 2015 9:46 am

Thank you! MarkTF, implemented your suggestion, has worked flawlessly since, appreciate your idea.

paddyg, noted and code updated

Now this fixed, time to think up next python plans. Cheers all for the help, this forum is great!

Return to “Python”