Wednesday, May 29, 2013

PI in the Oven: Logging Raspberry PI Core Temperatures to Sen.se

Objective:  Create a method of logging Raspberry PI data to the sen.se 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 sen.se API.  Sen.se 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 sen.se some and you will get the idea .  In my application, I have sen.se 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.  Sen.se 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 Sen.se 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!!!
------
# whiskeytangohotel.com
# May 2013

# Python script to read RaspberryPI
# internal core temp, covert from C to F
# and log to sen.se 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 sen.se API KEY in quotes.  Enter the Feed ID# without quotes
SENSE_API_KEY = "x1xxxxxy2yyyyyyz3zzzz"  
FEED_ID1 = 12345

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

while(True):

# The try/expect are there to trap errors if sen.se goes down
# or is slow.  This keeps the script running
try:
# 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}
send_to_opensense(data)
time.sleep(60)
except:
pass
-----

Saturday, May 11, 2013

Speech Synthesis on the Raspberry PI

Here is a quick and simple tip to add speech output to jazz up your Raspberry PI projects.  It is easy as installing the "festival" Text to Speech (TTS) application then calling it from the command line or Python script.

To install "festival" you need to be at a terminal prompt on the Raspberry PI.  Then type:
$ sudo apt-get install festival festival-freebsoft-utils
You will get asked to confirm the install.  Type "Y".

To make the PI talk, just issue a command from the terminal prompt, such as:
$ echo  "Whiskey Tango Hotel dot com Where stupidity meets reality"| festival --tts
That's it.  Of course, you can also execute this from a Python script, etc.

Here's a sample of the output.  Sounds pretty good to me.