Raspberry Pi 继电器和 Python,如何在不同的功能中关闭和打开继电器?
Raspberry Pi relays and Python, how do I turn off and on a relay in separate functions?
我是 Raspberry Pi 的新手,正在开发一个 Pi3 程序,该程序将通过 1 通道继电器打开和关闭灌溉泵。
我制作了一个运行良好的函数,它采用延迟变量并启动继电器,然后等待延迟,直到它再次停止继电器。
但是,我还需要能够在另一个功能中打开继电器,并保持打开状态直到用户再次将其关闭,但我似乎无法让它工作。
当我尝试时,继电器没有任何反应(除了正常的有电源指示 LED)。
这是我的工作函数:
#This function switches on the relay for a set period of time, and then shuts it off again
def relay_control(wait_time):
# Sleeping for a second
time.sleep(1)
print("Relay control function")
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Selecting which GPIO to target
GPIO_CONTROL = 6
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
# Starting the relay
GPIO.output(GPIO_CONTROL, True)
# Sleeping for set amount of time
try:
time.sleep(wait_time)
except:
time.sleep(60)
print("Setting delay failed, using default 60 seconds")
# Stopping the relay
GPIO.output(GPIO_CONTROL, False)
# Cleanup
GPIO.cleanup()
然后这两个功能我都试过了,就是不行:
#This function switches on the relay - Doesn't currently work
def relay_on():
# Sleeping for a second
time.sleep(1)
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Selecting which GPIO to target
GPIO_CONTROL = 6
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
#Starting the relay
GPIO.output(GPIO_CONTROL, False)
#Logging the event
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched on, from functions.py')
time.sleep(5)
#Cleanup
GPIO.cleanup()
#This function switches on the relay off - Doesn't currently work
def relay_off():
# Sleeping for a second
time.sleep(1)
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Selecting which GPIO to target
GPIO_CONTROL = 6
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
#Stopping the relay
GPIO.output(GPIO_CONTROL, True)
#Logging the event
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched off, from functions.py')
#Cleanup
GPIO.cleanup()
任何人都可以看到问题所在并帮助我解决问题吗?
非常感谢任何帮助!
此外,工作功能似乎冻结了我的 Raspberry Pi 一段时间。它全部通过 GPIO 引脚供电,系统有一个 7" 触摸屏显示器(系统通过 USB 供电),提到的 230v 1 通道继电器和一个 Sense Hat。
您应该将代码 GPIO.setmode()
和 GPIO.setup()
移出您的函数,因为它只需要设置一次。
部分代码如下所示:
def relay_on():
GPIO.output(GPIO_CONTROL, False)
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched on, from functions.py')
def relay_off():
GPIO.output(GPIO_CONTROL, True)
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched off, from functions.py')
# The main program setup the GPIO
GPIO_CONTROL = 6
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
# Get input from user to turn on or off relay
while True:
data = input("Input 'on'/'off' to turn on/off relay, or 'exit' to end ")
if data == 'on':
relay_on()
elif data == 'off':
relay_off()
elif data == 'exit':
# program end, clean up GPIO
GPIO.cleanup()
我自己用不同的方式编写函数解决了这个问题。这个效果很好,可以按预期启动和停止继电器。
#This function switches on the relay on or off and expects the argument 'on' or 'off'
def relay_manual(action):
# Selecting which GPIO to target
GPIO_CONTROL = 6
if action == "on":
# Sleeping for a second
time.sleep(1)
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
#Starting the relay
GPIO.output(GPIO_CONTROL, True)
#Logging the event
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched on, from functions.py')
elif action == "off":
try:
#Stopping the relay
GPIO.output(GPIO_CONTROL, False)
except:
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
#Starting the relay
GPIO.output(GPIO_CONTROL, False)
#Logging the event
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched off, from functions.py')
#Cleanup
GPIO.cleanup()
我是 Raspberry Pi 的新手,正在开发一个 Pi3 程序,该程序将通过 1 通道继电器打开和关闭灌溉泵。
我制作了一个运行良好的函数,它采用延迟变量并启动继电器,然后等待延迟,直到它再次停止继电器。
但是,我还需要能够在另一个功能中打开继电器,并保持打开状态直到用户再次将其关闭,但我似乎无法让它工作。
当我尝试时,继电器没有任何反应(除了正常的有电源指示 LED)。
这是我的工作函数:
#This function switches on the relay for a set period of time, and then shuts it off again
def relay_control(wait_time):
# Sleeping for a second
time.sleep(1)
print("Relay control function")
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Selecting which GPIO to target
GPIO_CONTROL = 6
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
# Starting the relay
GPIO.output(GPIO_CONTROL, True)
# Sleeping for set amount of time
try:
time.sleep(wait_time)
except:
time.sleep(60)
print("Setting delay failed, using default 60 seconds")
# Stopping the relay
GPIO.output(GPIO_CONTROL, False)
# Cleanup
GPIO.cleanup()
然后这两个功能我都试过了,就是不行:
#This function switches on the relay - Doesn't currently work
def relay_on():
# Sleeping for a second
time.sleep(1)
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Selecting which GPIO to target
GPIO_CONTROL = 6
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
#Starting the relay
GPIO.output(GPIO_CONTROL, False)
#Logging the event
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched on, from functions.py')
time.sleep(5)
#Cleanup
GPIO.cleanup()
#This function switches on the relay off - Doesn't currently work
def relay_off():
# Sleeping for a second
time.sleep(1)
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Selecting which GPIO to target
GPIO_CONTROL = 6
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
#Stopping the relay
GPIO.output(GPIO_CONTROL, True)
#Logging the event
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched off, from functions.py')
#Cleanup
GPIO.cleanup()
任何人都可以看到问题所在并帮助我解决问题吗?
非常感谢任何帮助!
此外,工作功能似乎冻结了我的 Raspberry Pi 一段时间。它全部通过 GPIO 引脚供电,系统有一个 7" 触摸屏显示器(系统通过 USB 供电),提到的 230v 1 通道继电器和一个 Sense Hat。
您应该将代码 GPIO.setmode()
和 GPIO.setup()
移出您的函数,因为它只需要设置一次。
部分代码如下所示:
def relay_on():
GPIO.output(GPIO_CONTROL, False)
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched on, from functions.py')
def relay_off():
GPIO.output(GPIO_CONTROL, True)
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched off, from functions.py')
# The main program setup the GPIO
GPIO_CONTROL = 6
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
# Get input from user to turn on or off relay
while True:
data = input("Input 'on'/'off' to turn on/off relay, or 'exit' to end ")
if data == 'on':
relay_on()
elif data == 'off':
relay_off()
elif data == 'exit':
# program end, clean up GPIO
GPIO.cleanup()
我自己用不同的方式编写函数解决了这个问题。这个效果很好,可以按预期启动和停止继电器。
#This function switches on the relay on or off and expects the argument 'on' or 'off'
def relay_manual(action):
# Selecting which GPIO to target
GPIO_CONTROL = 6
if action == "on":
# Sleeping for a second
time.sleep(1)
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
#Starting the relay
GPIO.output(GPIO_CONTROL, True)
#Logging the event
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched on, from functions.py')
elif action == "off":
try:
#Stopping the relay
GPIO.output(GPIO_CONTROL, False)
except:
# We will be using the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Set CONTROL to OUTPUT mode
GPIO.setup(GPIO_CONTROL, GPIO.OUT)
#Starting the relay
GPIO.output(GPIO_CONTROL, False)
#Logging the event
logging.basicConfig(format='%(asctime)s %(message)s', filename='/home/pi/GardenBrain/events.log', level=logging.INFO)
logging.info('Relay has been manually switched off, from functions.py')
#Cleanup
GPIO.cleanup()