Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6814

Troubleshooting • gpiozero buttons stop working after a few days

$
0
0
I have a Raspberry Pi 4 running a code that monitors several buttons. When a button is pressed or released, it does a requests post and prints two lines. After a few days, all of the buttons stop working. No requests posts, no lines printed. And no errors indicated. The code simply stops working. I have been running this manually lately so that I can manually stop and restart it, and that always gets it going for a few days. But only for a few days.

The code:

Code:

from gpiozero import Button, MotionSensor, LED, PWMLEDimport requestsfrom signal import pausefrom time import sleepimport timestart = time.time()enablepir = 0from datetime import datetimefrom autoremote import shanenow = datetime.now()current_time = now.strftime("%H:%M:%S %d %b %Y")#print("Current Time =", current_time)#  Define LED indicators - these are GPIO numbersmotionmon = LED(8)  # LED indicating motion is being monitoredmotionact = LED(7)  # LED indicating that motion has been detectedgaragerun = PWMLED(17)  # LED indicating that this python program is running#  Start notifications and indicatorsalert = "Garage started"URL = shane + alertr = requests.post(URL)# garagerun.blink(1,2,3,2)garagerun.blink(.05, 2.5)  # LED blinks to indicate program is running (off or solid indicates stoppage)motionmon.on()  # LED is commanded on, though the ground is controlled by an external switchprint("Service started at", current_time)#  Define inputs - these are GPIO numbersleftdoor = Button(26)  # Input from left garage door buttonrightdoor = Button(16)  # Input from right garage door buttongarageentry = Button(21)  # Input from garage entry doorgaragepassage = Button(20)  # Input from garage passage doorstairspassage = Button(18)  # Input from door at top of stairshallpir = MotionSensor(13)  # Signal input from PIR in hallmotionenable = Button(19) # Switched ground from Sonoff channel 3frontdoor = Button(25) # Input from front doorgaragelock = Button(5) # Input from garage entry door lock#  Define what to do when switches or PIRs are activedef ropen():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Right garage door closed at", current_time)    alert = "Right door closed"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'pressed'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending press request: {e}")def rclosed():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Right garage door open at", current_time)    alert = "Right door open"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'released'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending release request: {e}")def lopen():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Left garage door closed at", current_time)    alert = "Left door closed"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'press'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending press request: {e}")def lclosed():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Left garage door open at", current_time)    alert = "Left door open"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'released'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending release request: {e}")    def hallmotion():    global start, enablepir    if enablepir == 1:        now = time.time()        if now > start:            now = datetime.now()            current_time = now.strftime("%H:%M:%S %d %b %Y")            print("Basement hall motion detected at", current_time)            alert = "Stairs motion"            URL = shane + alert            r = requests.post(URL)            motionact.blink(.2, .2)  # LED blinks to indicate that motion has been detected            motionmon.off()  # LED indicating that motion monitoring is active turns off            start = time.time()            start = start + 5def hallmotionstop():    motionact.off()  # LED stops blinking when motion stops    motionmon.on()  # LED turns on to indicate motion is being monitoreddef garpassclosed():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Garage passage door open at", current_time)    alert = "Garage passage open"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'pressed'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending press request: {e}")def garpassopen():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Garage passage door closed at", current_time)    alert = "Garage passage closed"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'release'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending release request: {e}")def garentopen():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Garage entry door closed at", current_time)    alert = "Garage entry closed"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'pressed'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending press request: {e}")def garentclosed():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Garage entry door open at", current_time)    alert = "Garage entry open"    URL = shane + alert    #r = requests.post(URL)       try:        response = requests.post((URL), json={'status': 'release'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending release request: {e}")        def motionenabled():    global enablepir    alert = "Motion started"    URL = shane + alert    r = requests.post(URL)     sleep(4)  # Change the sleep time if you need more time before motion enabled    enablepir = 1    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Motion monitor started at", current_time)        def motiondisabled():    global enablepir    enablepir = 0    alert = "Motion ended"    URL = shane + alert    r = requests.post(URL)     motionact.off()    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Motion monitor ended at", current_time)def stairspassopen():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Stairs passage door open at", current_time)    alert = "Stairs door open"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'pressed'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending press request: {e}")def stairspassclosed():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Stairs passage door closed at", current_time)    alert = "Stairs door closed"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'release'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending release request: {e}")    def frontopen():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Front door open at", current_time)    alert = "Front door open"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'pressed'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending press request: {e}")    def frontclosed():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Front door closed at", current_time)    alert = "Front door closed"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'release'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending release request: {e}") def garagelocked():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Garage entry door locked at", current_time)    alert = "garagelocked"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'pressed'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending press request: {e}")def garageunlocked():    now = datetime.now()    current_time = now.strftime("%H:%M:%S %d %b %Y")    print("Garage entry door unlocked at", current_time)    alert = "garageunlocked"    URL = shane + alert    #r = requests.post(URL)    try:        response = requests.post((URL), json={'status': 'release'})        print(f"Response: {response.status_code}")    except Exception as e:        print(f"Error sending release request: {e}")#  Define switch and PIR statesleftdoor.when_released = lopenleftdoor.when_pressed = lclosedrightdoor.when_pressed = rclosedrightdoor.when_released = ropengaragepassage.when_pressed = garpassopengaragepassage.when_released = garpassclosedgarageentry.when_pressed = garentopengarageentry.when_released = garentclosedhallpir.when_motion = hallmotionhallpir.when_no_motion = hallmotionstopmotionenable.when_pressed = motionenabledmotionenable.when_released = motiondisabledstairspassage.when_pressed = stairspassclosedstairspassage.when_released = stairspassopenfrontdoor.when_pressed = frontclosedfrontdoor.when_released = frontopengaragelock.when_pressed = garagelockedgaragelock.when_released = garageunlockedpause()
I am thinking it is a problem with gpiozero, so I've tried to convert it to rpi.gpio but I keep getting bounce issues. I've tried a few AI code converters (blackbox.ai and ChatGPT) but nothing is really working out of the box. Should I reinstall gpiozero? Should I start with a fresh new install of the OS?

Statistics: Posted by duckredbeard — Tue Jul 15, 2025 10:35 am — Replies 1 — Views 23



Viewing all articles
Browse latest Browse all 6814

Trending Articles