我已经创建了python代码,用于定期读取带有三个温度传感器的1线Dallas总线上的温度。 1线总线连接到树莓派上的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
答案 0 :(得分:2)
sample_time = time.time()
while true:
TakeTemperatureReadings()
sample_time += 60 * 30
while time.time() < sample_time:
time.sleep(1)
time.sleep(1)行允许你的进程在等待时不要“吃掉”很多CPU。这将使samlple随机和+或 - 一秒,如果这是一个问题只是睡眠(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
答案 1 :(得分:0)
类似UNIX的系统(例如运行raspberry pi的Linux)具有定期运行程序的机制。
要重复运行命令,请使用cron
。请参阅cron(8)
,crontab(1)
和crontab(5)
的手册页。
用于每30分钟运行一次脚本的crontab命令可能如下所示:
0,30 * * * * python /yourdir/yourscript