python - 需要能够在系统时钟达到特定时间时启动Python函数

标签 python real-time raspberry-pi clock

我创建了 python 代码来定期从带有三个温度传感器的 1 线达拉斯总线读取温度。 1-wire 总线连接到树莓派上的 GPIO 引脚。我可以通过运行一个循环并在循环中使用 time.sleep(60) 函数来改变测量的周期来显示这些读数。 time.sleep(60) 每 60 秒循环读取一次。

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

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

我不知道如何不断读取系统时钟并将其与下一个计算时间进行比较。谷歌了很久也没找到答案。

感谢所有帮助:)

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

..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 

最佳答案

  • 从 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)
    

    time.sleep(1) 行允许您的进程在等待时不会“吃掉”太多 CPU。这将使样本随机并+或-一秒,如果这是一个问题,只需 sleep (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
    
  • 关于python - 需要能够在系统时钟达到特定时间时启动Python函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22114168/

    相关文章:

    python - 将for循环转换为python3中的递归函数

    java - 流式输出java

    javascript - 对两个不同的页面使用相同的端口

    ruby - 如何在 Debian Linux for ARM 上运行 pry

    html - Apache - 流式传输 HTML5 视频,无需为每个视频制作网页

    python - Pandas - 更改因子类型对象的级别顺序

    python - 如何查找复杂对象的类型

    ios - 使用 swift 播放实时音频

    ruby-on-rails - 在 Raspberry Pi Raspbian 上运行的引导 Ruby on Rails 服务器启动

    python - python 中的事件处理程序,类似于 C# 吗?监听串行消息