Skip navigation.

Uploading the Raspberry Pi - DS18B20 Temperature Data to the Internet

Previously I had written up on how I had used gnuPlot to graph my house temperatures. This is all well and good, but I didn't want to be uploading the graphs to this website every 5 minutes, or writing a script to pull the data live from the DB and generating the graphs on this webserver.  I had seen other people raving over the services provided by Cosm and Carriots, so it was time to give it a try.  Since I stumbled across Cosm first (via Adafruit), I decided to give the service a try.

Overview:

So basicly, I have a Python script that makes a number of queries to the MySQL temperature DB.  1) daily temperatures for inside sensors, 2) daily temperatures for the outside sensors, 3) min/max temperature for the outside sensor.  The data is dumped into a temporary text file, so that it can be imported into the gnuPlot text file. I also call a few function from the pyephem python module to figure out the previous days sunrise and sunset.  The Python script then calls some gnuPlot scripts to generate the required graphs.  These can then be viewed on the internal Intranet.

To upload the Outside sensor temperature data to Cosm, I have cron run every five minutes and call a similar Python script to extract the just the current outside temperature, dump the data into a Json text file and then call a Curl shell script to upload the Json data file to Cosm.

Yes, I should be using something like the gnuplot.py module instead of calling the gnuPlot executable, but learning how to do that can come later.  I have too many other things I want to do at the moment.

A bit more detail:

After creating a Cosm account, I read up on a few of their examples on how to use Curl (you have to love the power of Curl), to upload data points.  Here are the scripts I use to do this:

This is the master Python script that is called from cron:

#!/usr/local/bin/python
#
# this script is designed to be called from crontab every 5 mintes and will extract the current outside temperature and upload it to the Cosm Internet graphing website.
#
#  Parkview 2013-04-05
#

import os
import time
import MySQLdb as mdb
import sys
import subprocess
import datetime, time
from time import sleep

MYSQL_HOST = '<MySQL_svr_IP>'
MYSQL_DB = "temperature"
MYSQL_USER = "<MySQL_user>"
MYSQL_PASSWD = "<MySQL_pswd>"
curl_file="/tmp/cosm-curltemperature.dat"

 #  First need to query the DB for the current temperature from the outside sensor (Sensor17)
try:
    con = mdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD, MYSQL_DB);
    cur = con.cursor()
    # now lets get the Outside temp data 
    cur.execute("""SELECT Sensor17 FROM temperature.temp_5min_tbl order by datetime desc limit 1;""")
    f=open(curl_file, 'w')
    for row in cur.fetchall():
       # now lets  add this into a list variable for use
       DATA1 =  '{ "version":"1.0.0","datastreams":[ { "id":"Outside","current_value":"%s" } ] }' % str(row[0])
       f.write(DATA1)
    #print
    #print " DATA: ", DATA1
    #print
    f.close()

        
except mdb.Error, e:
    print "Error %d: %s" % (e.args[0],e.args[1])

    sys.exit(1)
        
finally: 
    if con:     
      con.close()

try:
    subprocess.Popen('/usr/local/bin/curl-COSM_feed.curl',shell = True)  # works!
except OSError, e:
    print 'OS Error: '
except subprocess.CalledProcessError, e:
    print cmd_output," - "

Here is the shell script that is called from the above Python script (curl-COSM_feed.curl) uses curl to up load the Json data to cosm.com:

#!/bin/sh

/usr/local/bin/curl --request PUT \
     --data-binary @/tmp/cosm-curltemperature.dat \
     --header "X-ApiKey: <csom_API_ID>" \
     --verbose \
    http://api.cosm.com/v2/feeds/122949

Finaly, this is an example of what the Json text file (cosm-curltemperature.dat) that is uploaded via the curl above script to Cosm:

{ "version":"1.0.0","datastreams":[ { "id":"Outside","current_value":"19.062" } ] }

 

So here is a small sample plot of the Cosm based temperature data, you can click through to a few larger graphs: 

Note: all the above scripts are run from a FreeBSD based server, not a Raspberry PI, so some lines will need modifying depending on the OS your running the scripts from.