猿问

基于 GPIO 输入和时间的 Python rpi GPIO 输出控制

我正在尝试编写一个小脚本来根据两个因素控制两个树莓派的 GPIO 输出引脚:GPIO.input.17 的状态和一天中的时间。


当 gpio.input.17 为低电平时,我希望 gpio.output.23 和 gpio.output.25 为低电平。

我希望当 gpio.input.17 为高且时间在 0700-2159 之间时 gpio.output.23 变高。

我希望当 gpio.input.17 为高且时间在 2200-0659 之间时 gpio.output.25 变高。


到目前为止,我整理的代码如下所示:


#!/usr/bin/python



import time

import RPi.GPIO as GPIO

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)



# Setup GPIO pins

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)     # set GPIO 17 as input

GPIO.setup(23, GPIO.OUT)                                # set GPIO 23 as output

GPIO.setup(25, GPIO.OUT)                                # set GPIO 25 as output

GPIO.output(23, 0)                                      # set GPIO 23 as low

GPIO.output(25, 0)                                      # set GPIO 25 as low


while True: 

    dt = list(time.localtime())

    hour = dt[3]

    minute = dt[4]

    second = dt[5]

    time.sleep(1)

    print hour,minute,second;

    PIR_Active = GPIO.input(17)

    if not PIR_Active:

        GPIO.output(23, 0)

        GPIO.output(25, 0)

    elif (PIR_Active and (hour>=00 and hour<=6) and (minute >=00 and minute<=59) and (second >=0 and second<=59)):

        GPIO.output(25, 1)

    elif (PIR_Active and (hour>=7 and hour<=11) and (minute>=0 and minute<=36) and (second>=0 and second<=59)):

        GPIO.output(23, 1)

    else: (PIR_Active and (hour>=11 and hour<=23) and (minute >=37 and minute<=59) and (second >=0 and second<=59));

    GPIO.output(25, 1)

time.sleep(1)

GPIO.cleanup()


我将 LED 连接到引脚 23 和 25,脚本中显示的时间来自我的测试,我通过此代码看到的结果是:

Out.Pin 23 按照 In.Pin.17 的状态在高电平和低电平之间切换,同时时间变量是真的

Out.Pin 23 停止在高低之间切换,而时间变量不正确

我感觉 Out.Pin.23 正在工作...


当代码执行时,Out.Pin 25 立即亮起,并且无论 In.Pin.17 的状态或时间如何,都保持亮起。


请忽略脚本中的时间,它们来自我的测试,与上述要求不符。


我是编码和编写脚本的初学者,非常感谢社区的任何帮助。


慕码人8056858
浏览 164回答 3
3回答

慕莱坞森

虽然我可以在 IDLE 中运行它并遵循代码中的逻辑,但我无法将其集成到我的脚本中(它破坏了我首先工作的内容:GPIO.output.23 遵循 GPIO.input.17 的状态“在活动时间内”。鉴于我的新手,你能指导我一下你的建议哪里出了问题吗?我喜欢整理 if/elif 语句的想法。#!/usr/bin/pythonimport timeimport datetime as dtimport RPi.GPIO as GPIOGPIO.setwarnings(False)GPIO.setmode(GPIO.BCM)# Define Time Variablesnow = dt.datetime.now().time()day = dt.time(16, 32, 59, 000000)night = dt.time(16, 33, 00, 000000)# Setup GPIO pinsGPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)&nbsp; &nbsp; &nbsp;# set GPIO 17 as inputGPIO.setup(23, GPIO.OUT)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # set GPIO 23 as outputGPIO.setup(25, GPIO.OUT)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # set GPIO 25 as outputGPIO.output(23, 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # set GPIO 23 as lowGPIO.output(25, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # set GPIO 25 as lowwhile True:&nbsp;&nbsp; &nbsp; dt = list(time.localtime())&nbsp; &nbsp; hour = dt[3]&nbsp; &nbsp; minute = dt[4]&nbsp; &nbsp; second = dt[5]&nbsp; &nbsp; time.sleep(1)&nbsp; &nbsp; print hour,minute,second;&nbsp; &nbsp; PIR_Active = GPIO.input(17)&nbsp; &nbsp; if not PIR_Active:&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(23, 0)&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(25, 0)&nbsp; &nbsp; elif all([PIR_Active, day < now < night]):&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(23, 1)&nbsp; &nbsp; else: all([PIR_Active, day < now < night]);&nbsp; &nbsp; GPIO.output(25, 1)time.sleep(1)GPIO.cleanup()

交互式爱情

我已经设法解决了这个问题...我原来的 else 语句无效,因此我添加了第三个 elif 和我的“夜间”条件,并用将两个 GPIO.out 引脚设置为 1 的 else 完成了该语句。我还反转了输出的“静止”状态,因为我认为我得到的继电器单元被应用了负值这是带注释的工作代码:#!/usr/bin/pythonimport timefrom time import sleepimport RPi.GPIO as GPIOGPIO.setwarnings(False)GPIO.setmode(GPIO.BCM)# Setup GPIO pinsGPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)&nbsp; &nbsp; &nbsp;# set GPIO 17 as inputGPIO.setup(23, GPIO.OUT)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # set GPIO 23 as outputGPIO.setup(24, GPIO.OUT)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # set GPIO 25 as outputGPIO.output(23, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # set GPIO 23 as high - relay boards are neg applied to activate, this keeps the relay powered off without a signalGPIO.output(24, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # set GPIO 25 as high - relay boards are neg applied to activate, this keeps the relay powered off without a signalwhile True:&nbsp;&nbsp; &nbsp; dt = list(time.localtime())&nbsp; &nbsp; hour = dt[3]&nbsp; &nbsp; minute = dt[4]&nbsp; &nbsp; second = dt[5]&nbsp; &nbsp; time.sleep(.01)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#The first time.sleep command value impacts any similar statements made below it&nbsp; &nbsp; print hour,minute,second;&nbsp; &nbsp; PIR_Active = GPIO.input(17)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#Define a condition which is met in the statements below&nbsp; &nbsp; if not PIR_Active:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #If the input is not active, reset both outputs to 'off'&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(23, 1)&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(24, 1)&nbsp; &nbsp; elif (PIR_Active and (hour>=00 and hour<=6) and (minute >=00 and minute<=59) and (second >=0 and second<=59)):&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(24, 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #If all of the above is true, set this output to on&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(30)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Hold this output 'on' for 30 seconds&nbsp; &nbsp; elif (PIR_Active and (hour>=7 and hour<=21) and (minute>=0 and minute<=59) and (second>=0 and second<=59)):&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(23, 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #If all of the above is true, set this output to on&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(30)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Hold this output 'on' for 30 seconds&nbsp; &nbsp; elif (PIR_Active and (hour>=22 and hour<=23) and (minute >=00 and minute<=59) and (second >=0 and second<=59)):&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(24, 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #If all of the above is true, set this output to on&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(30)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Hold this output 'on' for 30 seconds&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#Cleanly exit out of the if/elif statements with an else that:&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(23, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Resets this output to 'off'&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(24, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Resets this output to 'off'GPIO.cleanup()

Cats萌萌

首先,我承认这不是对你问题的完整答案,但对于评论来说太多了。此部分答案的目的是建议简化日期时间测试。设定now时间设定day时间设定night时间使用一个简单的逻辑运算符来测试是白天还是晚上。例子:import datetime as dtnow = dt.datetime.now().time()day = dt.time(7, 00)night = dt.time(22, 00)# Test if it's day time.now>>> datetime.time(14, 8, 6, 000000)day < now < night>>> True# Test again at midnight.now>>> datetime.time(0, 0)day < now < night>>> False将此逻辑集成到您的代码中将有助于简化if/elif,and语句。例如,这个:(PIR_Active and (hour>=00 and hour<=6) and (minute >=00 and minute<=59) and (second >=0 and second<=59))...可以变成这样 - 显然使用你自己的时间定义。all([PIR_Active, day < now < night])
随时随地看视频慕课网APP

相关分类

Python
我要回答