Read outside temperature with Ubuntu part 2

Let’s continue with small project reading and writing room temperature into some file. Root user has in crontab calling next command:

python /root/scripts/temperature_logger.py

 

Python script temperature_logge.py has some more lines:

[php]import os.path
import datetime
import time
from temperusb import TemperHandler

 

#Set up the temerusb module to read the temperature
th = TemperHandler()
devs = th.get_devices()
format = "%Y-%m-%d %H:%M:%S"
today = datetime.datetime.today()
s = today.strftime(format)

 

while True:
try:
#If the file is new, we’ll write a header row
header_row = None
if os.path.isfile(‘/root/scripts/temperature_log.csv’) == False:
header_row = ‘datetime,temperature_c\n’

#The temperatures will be logged to this file
f = open(‘/root/scripts/temperature_log.csv’, ‘a’)

#Write the header row if needed
if header_row:
f.write(header_row)

#Write the datetime and temperature
f.write(str(datetime.datetime.now()) + ‘ , ‘ + str(devs[0].get_temperatures()[0][‘temperature_c’]) + ‘\n’)

#Close the file until next time…
f.close()
print s, " – " + str(devs[0].get_temperatures()[0][‘temperature_c’])

 

#Badly handle any exceptions…
except Exception as e:
print "An error occured…" + str(e)
pass
break
[/php]

This script was running every 10 minutes from July 2016 to the last day of December. Some statistical data about temperature in my cabinet. Average temperature was 21,33 degree, the hottest was on July 22 at 6 PM, it was 31,75 degree. The coldest was in the last day of year 2016 from 9:20 to 11:00, it was only 11,625 degree. Script produced 24.869 lines, that is why I decided to run it only once an hour.
 


 

Whole solution was found on web page: http://stuffbabiesneed.com/at-night/raspberry-pi-temperature-logger-raspberry-pi-baby-monitor-part-2/