I have created python code to read temperature periodically from a 1-wire Dallas bus with three temperature sensors on it. The 1-wire bus is connected to the GPIO pins on the raspberry pi. I can display these readings by running round a loop and using the time.sleep(60) function in the loop to vary the periodicity of the measurements. time.sleep(60) loops for the reading every 60 seconds.
However this is not ideal. I wish to take the reading every half hour, on the half hour, by comparing the next calculated measurement time, with the real time system clock.
There are two sections of code I have written.
..1, the section that reads the temperature from the sensors
..2, the section that will compare the times.
I don't know how to continually read the system clock and compare this with the next calculated time. Spent a long time on Google and the answer has not arrived.
All help appreciated :)
The two code sections below, both work as intended.
..1, the section that reads the temperature from the sensors
#!/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, the section that will compare the times.
#!/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