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

Python • Trying to GPIO pin in same state using GPIOZERO

$
0
0
Hello,

I have a python script that collects data from various sensors, mainly temperatures, one of these temperature sensors is in my main server cabinet.
I have struggled and search for a solution for a few days but I get lots of non-relevant answers or non-working examples.

Setup:
Raspberry Pi Zero 2
Bookworm OS
Python 3 using GPIOZERO
My python script that collect temp data runs every 5mins from a CRON job

Right I now want to add some code using GPIOZERO that will read one of the temperature readings and turn a GPIO pin on if the temps go above 40deg c.
This will trigger a relay which will turn on some fans on however the fans keep turning off when the python scripts closes. I don't want to put this python script into a forever loop which will work for the fans turning on/off but not necessary for temperature collection every 5 mins.

Here is some simple code...This will turn the relay on and then the script terminates (and then the relay switches off due to termination).

Code:

#!/usr/bin/pythonimport sysimport timeimport gpiozeroRELAY_PIN = 18relay = gpiozero.OutputDevice(RELAY_PIN, active_high=True, initial_value=False)time.sleep(5)relay.on()sys.exit(0)
Is there a way to keep the fans going after the sys.exit(0) is called (WITHOUT using a forever loop)

Thanks

Statistics: Posted by SniffTheGLove — Mon Jun 16, 2025 3:54 pm



Viewing all articles
Browse latest Browse all 1584

Trending Articles