Wednesday, May 29, 2013

PI in the Oven: Logging Raspberry PI Core Temperatures to

Objective:  Create a method of logging Raspberry PI data to the website.  In this example I plot the core temperature of two Raspberry PIs, but the method can be adapted to log virtually any form of data that you wish to capture or generate with the PI.  The python code is below to get you running quickly.

The graph above is generated by sending data from the PI to the API. is one cool place.  Their goal is to assist in internet connectivity of personal devices; "the internet of things".  They have widgets, tools, applications, channels and a few other things that I barely understand.  Play around with some and you will get the idea .  In my application, I have graphing the core temperature of two Raspberry PIs; a data point every 60 seconds.  Just for fun, I also keep track of the number of reading, calculate an average temperature, and display the temperature change since the last reading. allows you to keep this information private or display it to the public.  
So.... from the graph we see one PI is running about ~12F hotter than the other.  Why?  

Probably due to a few reasons:  
RasPI_1 is always running the "motion" webcam software and functioning as an OpenVPN server.  RasPI_1 is also in a fully enclosed case.  (Maybe I should take it out of that case....)

RasPI_2 is not in a case on is only running my Hand of PI project.  Hand of PI is a robot hand that you can control by sending twitter commands to it.  Click for build page.

Of course, both PIs are running the temperature logging script.
If you are still with me, the python source code is below.  It has been running flawlessly for a while, so it should be solid.  Occasionally will go down briefly for maintenance, but that is why I put in the error traps.  Good luck and tweet the Hand of PI to let us know you were here!!!
# May 2013

# Python script to read RaspberryPI
# internal core temp, covert from C to F
# and log to for graphing.

# If you get errros on the import
# make certain you have the 'apts' installed

import httplib
import json as simplejson
from random import randint
import time

# init some vars
run_number = 0
tempC = 0
tempF = 0

# Enter your private API KEY in quotes.  Enter the Feed ID# without quotes
SENSE_API_KEY = "x1xxxxxy2yyyyyyz3zzzz"  
FEED_ID1 = 12345

# Function to format for
# The try/expect are there to trap errors if goes down
# or is slow.  This keeps the script running.
def send_to_opensense(data):
# prepare data 
datalist = [{"feed_id" : FEED_ID1, "value" :data['F']},]
headers = {"sense_key": SENSE_API_KEY,"content-type": "application/json"}
conn = httplib.HTTPConnection("")
# format a POST request with JSON content
conn.request("POST", "/events/", simplejson.dumps(datalist), headers)
response = conn.getresponse()


# The try/expect are there to trap errors if goes down
# or is slow.  This keeps the script running
# read the PI core temperture and store in tempC
# then convert from C to F and send the data to sen,se
tempC = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3
tempF = (tempC * 1.8) + 32
run_number = run_number + 1
print "Run:", run_number, "    tempC:", tempC, "    tempF:",tempF
data = { 'F' : tempF}