Script fails after some hours - Raspberry Pi Forums


hi

need post command pi 2 lan pc depending on state of 2 gpio pins.

ignoring gpio states, if fire off command using curl pi, @ time, works great. payload of 500 sets on, payload of 100 set off, no issues ever curl sure fundamentals work.

running python script using gpio states via cron -e @ boot watch gpio states works fine maybe few hours, days. fails. damn it! btw, gpio connected go high if momentary switch connects them 3.3v, works fine initially.

total noob python, looking pointers how proceed, log errors (?) maybe or add sort of watchdog? missing obvious aspect code prevent failing?

ideas

code: select all

 #!/usr/bin/env python  # gpio 23 & 24 set inputs. both pulled down. # 23 , 24 go 3v3 (3.3v) when momentary button pushed.  # 23 post on # 24 post off  import time import rpi.gpio gpio gpio.setmode(gpio.bcm) import requests  # handle 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 gpio library out  # event on pins deal calling  # 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 gpio on ctrl+c exit   

@359 can't see obvious start littering code print()s try track down bit has stopped working , when (add time.time() prints)

ps in python can else: if ..: in 1 elif ..:

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) 
way, pins go high "momentarily" waiting half second checking inputs stand chance of missing signal. intentional i.e. there reason why don't use channel callback has conveniently been provided?

code: select all

   if channel == 23:       payload = {'relay': '500'}    elif channel == 24:       payload = {'relay': '100'} 


raspberrypi



Comments

Popular posts from this blog

How do I add a forum to my site? - Joomla! Forum - community, help and support

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums