Tuesday, 27 August 2013

How to post core temperature from a raspberry pi to a senseboard using python

Stuff you need:

A sen.se account (just apply for one)
A raspberry pi (with python installed)
Internet connection

This code literally reads the pi's core temperature, every minute, and then displays it, in python, and then sends it to a sen.se account on the internet which can be viewed from anywhere.
Sen.se is designed for people to store information obtained using arduino or raspberry pi boards.


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

run_number = 0
tempC = 0

SENSE_API_KEY = "your api key here(note it is in quotes)"
FEED_ID1 = your feed key here #note it is not in quotes

def send_to_opensense(data):
    try:
        datalist = [{"feed_id" : FEED_ID1, "value" :data['C']},]
        headers = {"sense_key": SENSE_API_KEY,"content-type":"application/json"}
        conn = httplib.HTTPConnection("api.sen.se")
        conn.request("POST","/events/",simplejson.dumps(datalist),headers)
        response = conn.getresponse()
        conn.close()
    except:
            pass

while(True):
    try:
        tempC = int(open('/sys/class/thermal/thermal_zone0/temp').read())/1e3
        run_number = run_number + 1
        print "Run:", run_number," tempC:", tempC
        data = {'C' :tempC}
        send_to_opensense(data)
        time.sleep(60)
    except:
            pass