Jul-15-2025, 01:48 PM
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?
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()
