Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 摄像机的白天/夜间时间范围_Python_Python 3.x_Raspberry Pi - Fatal编程技术网

Python 摄像机的白天/夜间时间范围

Python 摄像机的白天/夜间时间范围,python,python-3.x,raspberry-pi,Python,Python 3.x,Raspberry Pi,此脚本实际上通过GPIO输出打开/关闭相机的夜间模式。 需要以某种方式设置范围 让我们假设覆盆子重新启动&脚本开始让我们假设在21:00已经是晚上了,但是现在脚本不知道这一点 我怎样做一个靶场 日出和日落之间=白天 日落和日出之间=夜晚 谢谢,我的代码: #import datetime from suntime import Sun, SunTimeException import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library fr

此脚本实际上通过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")



#导入日期时间
从suntime导入Sun,SunTimeException
将RPi.GPIO作为GPIO导入#导入Raspberry Pi GPIO库
从时间导入睡眠#从时间模块导入睡眠功能
从日期时间导入日期时间
now=datetime.now()
当前时间=现在。strftime(“%H:%M”)
GPIO.setwarnings(False)#暂时忽略警告
GPIO.setmode(GPIO.BCM)
GPIO.setup(12,GPIO.OUT,initial=GPIO.LOW)#将引脚8设置为输出引脚,并将初始值设置为LOW(关闭)
打印(“当前时间=”,当前时间)
纬度=51.34
经度=-0.95
太阳=太阳(纬度、经度)
#在UTC中获取今天的日出和日落
今天=太阳。获得日出时间()
今天=太阳。得到日落时间()
打印('今天在Heckfield,太阳在{}升起,在{}UTC降落'。
格式(今天\u sr.strftime('%H:%M')、今天\u ss.strftime('%H:%M'))
sunraise=今天\u sr.strftime(“%H:%M”)
日落=今天\u ss.strftime(“%H:%M”)
尽管如此:

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

作为旁白,我会稍微改变一下。因为日出和日落在某一天不会改变,所以你不需要无休止地检查它们。计算现在和下一次更改之间的差异,然后休眠或设置触发器,以便在未来的很长时间内更改相机状态。