I am wondering how I can get the relays to stay in the chosen state after the script has stopped.
I am trying to controll an inverter with the 3 relays, and would like them to stay in a certain state untill I change it or return to their original state if the RPI looses power. The latter is easy as the relays do it by themselves when they loose power, but the first one is my main problem.
As long as the script is running the relays stay in the state they were told, but as soon as the script ends they return to their normal unpowered state.
I am a n00b with python scripts, so it just might be that I need to learn more.
That might work, I’ll have to check how I could combine it into my other script. I read a few temperatures and the state of my heat-pump to decide if the inverter needs to keep or change its state.
Finally got my script done and working. I simply have it running in the background and have my other script update the hz.txt when a change in the frequenzy is needed.
#!/usr/bin/env python
import time
import automationhat
if automationhat.is_automation_hat():
automationhat.light.power.write(1)
while True:
data = open('hz.txt', 'r')
hz = data.readline()[:-1]
data.close()
if hz == '50':
automationhat.relay.one.off()
if automationhat.is_automation_hat():
automationhat.relay.two.off()
automationhat.relay.three.off()
time.sleep(60)
elif hz == '40':
automationhat.relay.one.off()
if automationhat.is_automation_hat():
automationhat.relay.two.off()
automationhat.relay.three.on()
time.sleep(60)
elif hz == '45':
automationhat.relay.one.off()
if automationhat.is_automation_hat():
automationhat.relay.two.on()
automationhat.relay.three.off()
time.sleep(60)
elif hz == '55':
automationhat.relay.one.on()
if automationhat.is_automation_hat():
automationhat.relay.two.off()
automationhat.relay.three.off()
time.sleep(60)
elif hz == '60':
automationhat.relay.one.on()
if automationhat.is_automation_hat():
automationhat.relay.two.on()
automationhat.relay.three.off()
time.sleep(60)
elif hz == '65':
automationhat.relay.one.on()
if automationhat.is_automation_hat():
automationhat.relay.two.off()
automationhat.relay.three.on()
time.sleep(60)
elif hz == '70':
automationhat.relay.one.on()
if automationhat.is_automation.hat():
automationhat.relay.two.on()
automationhat.relay.three.on()
time.sleep(60)
if automationhat.is_automation_hat(): is used to tell the difference between Automation HAT (which has three relays) and Automation pHAT (which has just one.)
Because RPi.GPIO cleans up after itself when the Python script exits, it’s not possible to set a GPIO with it and expect it to stay in that state when the program exits.
As long as your Pi is powered, however, you should be able to toggle a pin outside of Python. Relay 1, Relay 2 and Relay 3 are on BCM 13, BCM 19 and BCM 16.
Using a utility like “pigpio” you can toggle the GPIO directly and it will retain its state while the Pi is powered:
There’s nothing technically wrong with running the Python script constantly. If it’s sitting in a delay loop, or suspended properly, it will not use any significant resources or power.