Yesterday I wrote a post showing how to create a Raspberry Pi Temperature Logger using a cheap and simple USB Thermometer. If you don’t have and don’t want to buy a USB Thermometer, here’s a quick and dirty alternative – using the Raspberry Pi CPU temperature sensor to get an approximate room temperature.
Reading the Raspberry Pi CPU temperature
Running the command above will give you an output such as:
As you can see, the temperature is a little high – the thermometer in this case is a sensor right inside the Raspberry Pi’s CPU. With a little code, we can extract just the temperature and apply an offset to get it back to an approximate room temperature.
Given that I do have a USB Thermometer, I can compare the two temperatures as per the below:
pi@raspberrypi:~ $ sudo temper-poll Found 1 devices Device #0: 21.6°C 70.9°F pi@raspberrypi:~ $ /opt/vc/bin/vcgencmd measure_temp temp=37.9'C
This was taken once the Pi had been on and active for a while, but without anything too stressful going on. So in my case, we can approximate the room temperature by offsetting the Raspberry Pi CPU temperature by 16.3 degrees Celsius.
A quick update to yesterdays python script to read the Raspberry Pi CPU temperature sensor rather than the USB thermometer:
import os.path import datetime import time import subprocess while True: try: #If the file is new, we'll write a header row header_row = None if os.path.isfile('./temperature_log.csv') == False: header_row = 'datetime,temperature_c\n' #The temperatures will be logged to this file f = open('temperature_log.csv', 'a') #Write the header row if needed if header_row: f.write(header_row) cpu_temp = float(subprocess.check_output(["/opt/vc/bin/vcgencmd","measure_temp"]).replace("temp=","").replace("'C","")) - 16.3 #Write the datetime and temperature f.write(str(datetime.datetime.now()) + ',' + str(cpu_temp) + '\n') #Close the file until next time... f.close() print str(datetime.datetime.now()) + " - " + str(cpu_temp) #Badly handle any exceptions... except Exception as e: print "An error occured..." + str(e) pass #Sleep for 10 mins before taking the next reading time.sleep(600)
Same as before, it will print the temperature and log to a CSV file every 10 mins. I’ll leave this running and provide some output shortly!
Note – don’t rely on this, if you have a baby and are worried about the temperature in their room, get a decent room thermometer!