白天/夜晚摄像机使用时间范围

2024-04-16 04:56:22 发布

您现在位置:Python中文网/ 问答频道 /正文

此脚本实际上通过GPIO输出打开/关闭相机的夜间模式。 需要以某种方式设置范围

比如说覆盆子重启&;脚本在21:00开始,已经是晚上了,但是现在脚本不知道

我怎样做一个靶场 日出和日落之间=白天

日落和日出之间=夜晚

谢谢,我的代码:


#import datetime
from suntime import Sun, SunTimeException

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time module


from datetime import datetime
now = datetime.now()

current_time = now.strftime("%H:%M")



GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial value to low (off)


print("Current Time =", current_time)


latitude = 51.34
longitude = -0.95

sun = Sun(latitude, longitude)

# Get today's sunrise and sunset in UTC
today_sr = sun.get_sunrise_time()
today_ss = sun.get_sunset_time()
print('Today at Heckfieldplace the sun raised at {} and get down at {} UTC'.
      format(today_sr.strftime('%H:%M'), today_ss.strftime('%H:%M')))


sunraise = today_sr.strftime('%H:%M')
sunset = today_ss.strftime('%H:%M')



while True:
        if current_time >= sunraise and current_time <= sunset:
                GPIO.output(12, GPIO.LOW)
                print("DAY")

        elif current_time >= sunset and current_time <= sunraise:
                GPIO.output(12, GPIO.HIGH)
                print("NIGHT")

        else:
                print("Nothing")



Tags: andthefromimport脚本todaydatetimegpio
1条回答
网友
1楼 · 发布于 2024-04-16 04:56:22

日出和日落都是相对于当天的午夜,所以日落总是比日出多。所以你的第二个夜间条件是一个不可能的条件。如果你创建了一个“明天的日出”变量,或者仅仅是午夜,你可以使用它。编辑:就这点而言,如果不是白天,应该是晚上,所以你可以用一个简单的else

相关问题 更多 >