Python Forum
gpiozero buttons stop working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
gpiozero buttons stop working
#1
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.

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?

from gpiozero import Button, MotionSensor, LED, PWMLED import requests from signal import pause from time import sleep import time start = time.time() enablepir = 0 from datetime import datetime from autoremote import shane now = datetime.now() current_time = now.strftime("%H:%M:%S %d %b %Y") #print("Current Time =", current_time) # Define LED indicators - these are GPIO numbers motionmon = LED(8) # LED indicating motion is being monitored motionact = LED(7) # LED indicating that motion has been detected garagerun = PWMLED(17) # LED indicating that this python program is running # Start notifications and indicators alert = "Garage started" URL = shane + alert r = 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 switch print("Service started at", current_time) # Define inputs - these are GPIO numbers leftdoor = Button(26) # Input from left garage door button rightdoor = Button(16) # Input from right garage door button garageentry = Button(21) # Input from garage entry door garagepassage = Button(20) # Input from garage passage door stairspassage = Button(18) # Input from door at top of stairs hallpir = MotionSensor(13) # Signal input from PIR in hall motionenable = Button(19) # Switched ground from Sonoff channel 3 frontdoor = Button(25) # Input from front door garagelock = Button(5) # Input from garage entry door lock # Define what to do when switches or PIRs are active def 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 + 5 def hallmotionstop(): motionact.off() # LED stops blinking when motion stops motionmon.on() # LED turns on to indicate motion is being monitored def 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 states leftdoor.when_released = lopen leftdoor.when_pressed = lclosed rightdoor.when_pressed = rclosed rightdoor.when_released = ropen garagepassage.when_pressed = garpassopen garagepassage.when_released = garpassclosed garageentry.when_pressed = garentopen garageentry.when_released = garentclosed hallpir.when_motion = hallmotion hallpir.when_no_motion = hallmotionstop motionenable.when_pressed = motionenabled motionenable.when_released = motiondisabled stairspassage.when_pressed = stairspassclosed stairspassage.when_released = stairspassopen frontdoor.when_pressed = frontclosed frontdoor.when_released = frontopen garagelock.when_pressed = garagelocked garagelock.when_released = garageunlocked pause()
Reply
#2
When it stops running does the garagerun LED stop blinking?

Did you look at your output to check if every response prints a status message? I would check that for every "Garage entry door unlocked at..." there is a corresponding "Response...".
Reply
#3
(Jul-15-2025, 05:00 PM)deanhystad Wrote: When it stops running does the garagerun LED stop blinking?

Did you look at your output to check if every response prints a status message? I would check that for every "Garage entry door unlocked at..." there is a corresponding "Response...".

There is no line printed and there is no response code. The LED light continues to blink. It seems that it is simply no longer processing the button inputs. I can remotely cause a button input by controlling a wifi controlled switch. That switch controls the ground for a PIR as well as provides a ground input to a GPIO. This is "motionenable" on GPIO19. When this remote switch is activated, the ground allows the PIR to work and I get the feedback that the PIR is active. A second function of this is that I can test the detection of button inputs without having to go to a door, and I can test it from any where. Another app for the switch can confirm that the wifi controlled switch is functioning. If the pi doesn't do what it is supposed to do (requests.post and print line), then I can only say it is the RPi's fault.

I will build up a spare Pi with a fresh OS install today and copy over the script today. If that fixes it then it is OS or hardware. If it doesn't fix it then it must be code...
Reply
#4
All your functions print something. Either they are not running, or the output is not displaying. Which is it?
Reply
#5
(Jul-17-2025, 04:24 AM)deanhystad Wrote: All your functions print something. Either they are not running, or the output is not displaying. Which is it?

Everything associated with a button stops working. Nothing prints, there is no request post happening. It is like the buttons aren't pressed.
Reply
#6
I don't understand "Everything associated with a button stops working. Nothing prints".

In your first post you said this:
"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"

That makes it sound like there was printing, and then printing stopped. What was the last thing printed? Most of your functions print something, perform an action, and then print something. If you see the first message printed but not the second, it would point to a problem performing the action. If the last thing printed is the second message, that points to the functions not getting called.
Reply
#7
(Jul-17-2025, 03:49 PM)deanhystad Wrote: I don't understand "Everything associated with a button stops working. Nothing prints".

In your first post you said this:
"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"

That makes it sound like there was printing, and then printing stopped. What was the last thing printed? Most of your functions print something, perform an action, and then print something. If you see the first message printed but not the second, it would point to a problem performing the action. If the last thing printed is the second message, that points to the functions not getting called.

The last thing that is printed was is always the "print(f"Response: {response.status_code}")" line, and that code is always 200, which indicates that the last event's task is complete and successful. I do not get the first line that prints what had happened like the "print("Right garage door open at", current_time)" line for the newest event.
Reply
#8
Do the motionmon and motionact LED's stop working?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  gpiozero button turn off LED that is already on duckredbeard 3 1,818 Dec-11-2024, 06:23 PM
Last Post: duckredbeard
  GPIOzero Newbe Question Rchrd 0 1,027 Nov-10-2024, 03:40 PM
Last Post: Rchrd
  How to make my Telegram bot stop working at 16:15 and not work on Fridays? hus73 2 2,479 Aug-10-2024, 12:06 PM
Last Post: hus73
  Buttons not working in tabs kenwatts275 2 2,668 May-02-2022, 04:45 PM
Last Post: kenwatts275
  Buttons or Radio Buttons on Tkinter Treeview draems 0 4,533 Oct-31-2017, 04:06 AM
Last Post: draems

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.