我是树莓派的新手,正在开发一个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()
有没有人能看到问题所在,帮我把它修好?
任何帮助都是非常感谢的!
此外,工作函数似乎冻结了我的树莓派一段时间。它都是通过GPIO引脚供电的,系统有一个7英寸的触摸屏显示屏(系统正在通过USB供电),提到的230v 1通道继电器和一个Sense Hat。
发布于 2018-06-23 19:11:19
我自己通过以不同的方式编写函数解决了这个问题。这一个运行良好,可以按预期启动和停止中继。
#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()
发布于 2018-06-23 08:33:00
您应该将代码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()
https://stackoverflow.com/questions/50996559
复制相似问题