为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕码人8056858 2023-12-20 19:47:37
我正在尝试编写一个小脚本来根据两个因素控制两个树莓派的 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/pythonimport timeimport RPi.GPIO as GPIOGPIO.setwarnings(False)GPIO.setmode(GPIO.BCM)# Setup GPIO pinsGPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)     # set GPIO 17 as inputGPIO.setup(23, GPIO.OUT)                                # set GPIO 23 as outputGPIO.setup(25, GPIO.OUT)                                # set GPIO 25 as outputGPIO.output(23, 0)                                      # set GPIO 23 as lowGPIO.output(25, 0)                                      # set GPIO 25 as lowwhile 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 的状态或时间如何,都保持亮起。请忽略脚本中的时间,它们来自我的测试,与上述要求不符。我是编码和编写脚本的初学者,非常感谢社区的任何帮助。
查看完整描述

3 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

虽然我可以在 IDLE 中运行它并遵循代码中的逻辑,但我无法将其集成到我的脚本中(它破坏了我首先工作的内容:GPIO.output.23 遵循 GPIO.input.17 的状态“在活动时间内”。


鉴于我的新手,你能指导我一下你的建议哪里出了问题吗?我喜欢整理 if/elif 语句的想法。


#!/usr/bin/python



import time

import datetime as dt

import RPi.GPIO as GPIO

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)



# Define Time Variables

now = dt.datetime.now().time()

day = dt.time(16, 32, 59, 000000)

night = dt.time(16, 33, 00, 000000)


# 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, 1)                                      # 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 all([PIR_Active, day < now < night]):

        GPIO.output(23, 1)

    else: all([PIR_Active, day < now < night]);

    GPIO.output(25, 1)

time.sleep(1)

GPIO.cleanup()


查看完整回答
反对 回复 2023-12-20
?
交互式爱情

TA贡献1712条经验 获得超3个赞

我已经设法解决了这个问题...我原来的 else 语句无效,因此我添加了第三个 elif 和我的“夜间”条件,并用将两个 GPIO.out 引脚设置为 1 的 else 完成了该语句。


我还反转了输出的“静止”状态,因为我认为我得到的继电器单元被应用了负值


这是带注释的工作代码:


#!/usr/bin/python



import time

from time import sleep

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(24, GPIO.OUT)                                # set GPIO 25 as output

GPIO.output(23, 1)                                      # set GPIO 23 as high - relay boards are neg applied to activate, this keeps the relay powered off without a signal

GPIO.output(24, 1)                                      # set GPIO 25 as high - relay boards are neg applied to activate, this keeps the relay powered off without a signal


while True: 

    dt = list(time.localtime())

    hour = dt[3]

    minute = dt[4]

    second = dt[5]

    time.sleep(.01)                         #The first time.sleep command value impacts any similar statements made below it

    print hour,minute,second;

    PIR_Active = GPIO.input(17)             #Define a condition which is met in the statements below

    if not PIR_Active:                      #If the input is not active, reset both outputs to 'off'

        GPIO.output(23, 1)

        GPIO.output(24, 1)

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

        GPIO.output(24, 0)                  #If all of the above is true, set this output to on

        time.sleep(30)                      #Hold this output 'on' for 30 seconds

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

        GPIO.output(23, 0)                  #If all of the above is true, set this output to on

        time.sleep(30)                      #Hold this output 'on' for 30 seconds

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

        GPIO.output(24, 0)                  #If all of the above is true, set this output to on

        time.sleep(30)                      #Hold this output 'on' for 30 seconds

    else:                                   #Cleanly exit out of the if/elif statements with an else that:

        GPIO.output(23, 1)                  #Resets this output to 'off'

        GPIO.output(24, 1)                  #Resets this output to 'off'

GPIO.cleanup()


查看完整回答
反对 回复 2023-12-20
?
Cats萌萌

TA贡献1805条经验 获得超9个赞

首先,我承认这不是对你问题的完整答案,但对于评论来说太多了。

此部分答案的目的是建议简化日期时间测试。

  • 设定now时间

  • 设定day时间

  • 设定night时间

  • 使用一个简单的逻辑运算符来测试是白天还是晚上。

例子:

import datetime as dt


now = 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])


查看完整回答
反对 回复 2023-12-20
  • 3 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信