Automation-HAT relay operation

Hi,

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.

-gooFI

you can set the relay state as part of a while loop… or I might be misunderstand your requirements and resulting difficulty?

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)

Can you please advise what is the purpose of?
“if automationhat.is_automation_hat(): …”

I need a way to change the status of a relay without a while loop, for instance with:

automationhat.relay.toggle() or
automationhat.relay.write(1) # 1 = ON, 0 = OFF

I would not like to keep a python script active in the system, I just would like to open and close one relay with one command.

I apologize for the question but I can not understand how to use these commands. Can anyone please advise more details on?

Please clarify.

Thanks,

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:

  • “sudo apt-get install pigpio”
  • sudo pigpiod

Then toggle Relay 3 with:

  • pigs w 16 1
  • pigs w 16 0

You can also use sysfs directly, see: https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=182734&p=1159545#p1159545

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.

1 Like