需要能够在系统时钟到达特定时间时开始Python函数

需要能够在系统时钟到达特定时间时开始Python函数,python,real-time,raspberry-pi,clock,Python,Real Time,Raspberry Pi,Clock,我已经创建了python代码,用于定期从一条单线达拉斯总线读取温度,该总线上有三个温度传感器。单线总线连接到raspberry pi上的GPIO引脚。我可以通过循环显示这些读数,并使用循环中的time.sleep(60)函数来改变测量周期。时间。睡眠(60)循环每60秒读取一次 然而,这并不理想。我希望通过将下一次计算的测量时间与实时系统时钟进行比较,每半小时读取一次读数 我写了两段代码。 ..1,从传感器读取温度的部分 …2,将比较时间的部分 #!/usr/bin/env python im

我已经创建了python代码,用于定期从一条单线达拉斯总线读取温度,该总线上有三个温度传感器。单线总线连接到raspberry pi上的GPIO引脚。我可以通过循环显示这些读数,并使用循环中的time.sleep(60)函数来改变测量周期。时间。睡眠(60)循环每60秒读取一次

然而,这并不理想。我希望通过将下一次计算的测量时间与实时系统时钟进行比较,每半小时读取一次读数

我写了两段代码。 ..1,从传感器读取温度的部分 …2,将比较时间的部分

#!/usr/bin/env python

import datetime

tt = datetime.datetime.now()
print "\n"

#print tt
print str("year NOW:") + "\t"+ str(tt.year)
print str("month NOW:") + "\t" + str(tt.month)
print str("day NOW:") + "\t" + str(tt.day)
print str("hour NOW:") + "\t" + str(tt.hour)


print "\n"
print str("minute NOW:") + "\t" + str(tt.minute)

print "\n"
actual = datetime.datetime.now()
actual = actual.replace(second=0,microsecond=0)
print str("actual Now:") + "\t" + str(actual)

print "\n"

# round to nearest 10 mins
# sd = int(round(tt.minute/10)*10)
# print 'minute rounded:', sd

# round to nearest half hour
if (int(round(tt.minute/30))) < 1:
    sd=0
else:
    sd=30
print '1/2 hour rounded down:', sd

# round to nearest hour
# sd = int(round(tt.minute/1000))
# print 'hour rounded:', sd

# to nearest 10 mins
# z = tt.replace(minute=10+sd,second=0,microsecond=0)

# round to nearest half hour

if sd == 0:
    z = tt.replace(minute=30,second=0,microsecond=0)
elif sd == 1 and tt.hour <> 23:
    z = tt.replace(hour=tt.hour+1,minute=0,second=0,microsecond=0)
else:
    z = tt.replace(day=tt.day+1,hour=0,minute=0,second=0,microsecond=0)

# to nearest hour
#z = tt.replace(hour=tt.hour+1,minute=0+sd,second=0,microsecond=0)

print "\n"
print str("rounded time:") + "\t" + str(z)
print str("rounded hour:") + "\t" + str(z.hour)
print str("rounded minute:") + "\t" + str(z.minute)
print "\n"


print 'z > actual:', z > actual
print "\n"


#while
print tt.minute
print z.minute 
我不知道如何连续读取系统时钟,并将其与下一次计算的时间进行比较。在谷歌上花了很长时间,但答案还没有找到

感谢所有帮助:)

下面的两个代码部分都按预期工作

..1,从传感器读取温度的部分

#!/usr/bin/env python

import time

def TakeTemperatureReadings():

        datafile = open("temperaturedata.log", "a", 1)

        timestamp = time.strftime("%d/%m/%Y %H:%M:%S")

    # sensor on pi itself   
    tfile = open("/sys/bus/w1/devices/28-0000052c29e1/w1_slave")
        text = tfile.read()
        tfile.close()
        temperature_data = text.split()[-1]
        temperature = float(temperature_data[2:])
        temperature1 = round(temperature / 1000,1)

    # sensor half way along bus
        tfile = open("/sys/bus/w1/devices/28-0000052c33ef/w1_slave")
        text = tfile.read()
        tfile.close()
        temperature_data = text.split()[-1]
        temperature = float(temperature_data[2:])
        temperature2 = round(temperature / 1000,1)

    # sensor at end of line
        tfile = open("/sys/bus/w1/devices/28-0000052c6da7/w1_slave")
        text = tfile.read()
        tfile.close()
        temperature_data = text.split()[-1]
        temperature = float(temperature_data[2:])
        temperature3 = round(temperature / 1000,1)


    data_stream = str(temperature1) + ", on-pi, " + str(temperature2) + ", window, " + str(temperature3) + ", outside, " + str(timestamp)
    print data_stream        
    datafile.write(data_stream + "\n")
    # datafile.write(str(temperature1) + ", " + str(temperature2) + ", " + str(temperature3) + ", " + str(timestamp)+ "\n")
        datafile.close()


        time.sleep(60)

while True:
    TakeTemperatureReadings()
…2,将比较时间的部分

#!/usr/bin/env python

import datetime

tt = datetime.datetime.now()
print "\n"

#print tt
print str("year NOW:") + "\t"+ str(tt.year)
print str("month NOW:") + "\t" + str(tt.month)
print str("day NOW:") + "\t" + str(tt.day)
print str("hour NOW:") + "\t" + str(tt.hour)


print "\n"
print str("minute NOW:") + "\t" + str(tt.minute)

print "\n"
actual = datetime.datetime.now()
actual = actual.replace(second=0,microsecond=0)
print str("actual Now:") + "\t" + str(actual)

print "\n"

# round to nearest 10 mins
# sd = int(round(tt.minute/10)*10)
# print 'minute rounded:', sd

# round to nearest half hour
if (int(round(tt.minute/30))) < 1:
    sd=0
else:
    sd=30
print '1/2 hour rounded down:', sd

# round to nearest hour
# sd = int(round(tt.minute/1000))
# print 'hour rounded:', sd

# to nearest 10 mins
# z = tt.replace(minute=10+sd,second=0,microsecond=0)

# round to nearest half hour

if sd == 0:
    z = tt.replace(minute=30,second=0,microsecond=0)
elif sd == 1 and tt.hour <> 23:
    z = tt.replace(hour=tt.hour+1,minute=0,second=0,microsecond=0)
else:
    z = tt.replace(day=tt.day+1,hour=0,minute=0,second=0,microsecond=0)

# to nearest hour
#z = tt.replace(hour=tt.hour+1,minute=0+sd,second=0,microsecond=0)

print "\n"
print str("rounded time:") + "\t" + str(z)
print str("rounded hour:") + "\t" + str(z.hour)
print str("rounded minute:") + "\t" + str(z.minute)
print "\n"


print 'z > actual:', z > actual
print "\n"


#while
print tt.minute
print z.minute 
#/usr/bin/env python
导入日期时间
tt=datetime.datetime.now()
打印“\n”
#打印tt
打印str(“现在年份:”)+“\t”+str(现在年份)
打印str(“现在的月份:”)+“\t”+str(tt.month)
打印str(“现在的日期:”)+“\t”+str(tt.day)
打印str(“现在的小时:”)+“\t”+str(tt.hour)
打印“\n”
打印str(“分钟现在:”)+“\t”+str(tt.minute)
打印“\n”
实际值=datetime.datetime.now()
实际值=实际值。替换(秒=0,微秒=0)
打印str(“现在实际:”)+“\t”+str(实际)
打印“\n”
#四舍五入至最接近10分钟
#sd=整数(整数(tt.分/10)*10)
#打印“四舍五入分钟:”,sd
#大约半小时
如果(整数(整数分钟/30))<1:
sd=0
其他:
sd=30
打印“1/2小时四舍五入:”,sd
#四舍五入
#sd=整数(整数(tt.分/1000))
#打印“四舍五入小时:”,sd
#精确到10分钟
#z=tt.更换(分钟=10+sd,秒=0,微秒=0)
#大约半小时
如果sd==0:
z=tt.更换(分钟=30,秒=0,微秒=0)
elif sd==1,tt.23小时:
z=tt.替换(小时=tt.小时+1,分钟=0,秒=0,微秒=0)
其他:
z=tt.替换(天=tt.天+1,小时=0,分钟=0,秒=0,微秒=0)
#到最近的一小时
#z=tt.更换(小时=tt.小时+1,分钟=0+sd,秒=0,微秒=0)
打印“\n”
打印str(“四舍五入时间:)+“\t”+str(z)
打印str(“四舍五入小时:)+“\t”+str(z.hour)
打印str(“四舍五入分钟:)+“\t”+str(z.minute)
打印“\n”
打印“z>actual:”,z>actual
打印“\n”
#当
打印tt分钟
打印z分钟
  • 从TakeTemperatureReadings()方法中删除time.sleep(60)。
  • 使用time.time(),它比datetime.datetime.now()更容易处理
  • 主回路可以是:

    sample_time = time.time()
    while true:
      TakeTemperatureReadings()
      sample_time += 60 * 30
      while time.time() < sample_time:
        time.sleep(1)
    
    sample\u time=time.time()
    尽管如此:
    Take TemperatureReadings()
    采样时间+=60*30
    while time.time()
    time.sleep(1)行允许进程在等待时不“消耗”太多CPU。这将使样本随机和+或-1秒,如果这是一个问题,只需睡眠(0.1)

  • 如果希望在时钟上而不是在第一个时钟上同步样本 第一,你可以做:

    sample_time = time.time()
    sample_time -= sample_time % 60 * 30
    sample_time += 60 * 30
    while true:
      while time.time() < sample_time:
        time.sleep(1)
      TakeTemperatureReadings()
      sample_time += 60 * 30
    
    sample\u time=time.time()
    样本时间-=样本时间%60*30
    采样时间+=60*30
    尽管如此:
    while time.time()
    运行raspberry pi的Linux等类UNIX系统具有定期运行程序的机制

    要重复运行命令,请使用。有关
    cron(8)
    crontab(1)
    crontab(5)
    ,请参阅手册页面

    每三十分钟运行一次脚本的crontab命令可能如下所示:

    0,30   *   *   *   * python /yourdir/yourscript
    

    您考虑过使用cron作业吗?谢谢。我非常感激。这些都是非常简洁优雅的解决方案。第一个解决方案可行,但需要精确到半小时,即秒等于零。我试图找出如何做到这一点,但还没有答案。第二个解决方案在“while time.time()