Created
February 7, 2014 17:34
-
-
Save pdp7/8867670 to your computer and use it in GitHub Desktop.
Raspberry Pi temp sensor logging to Xively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import xively | |
import subprocess | |
import time | |
import datetime | |
import requests | |
import glob | |
os.system('modprobe w1-gpio') | |
os.system('modprobe w1-therm') | |
base_dir = '/sys/bus/w1/devices/' | |
device_folder = glob.glob(base_dir + '28*')[0] | |
device_file = device_folder + '/w1_slave' | |
# extract feed_id and api_key from environment variables | |
FEED_ID = os.environ["FEED_ID"] | |
API_KEY = os.environ["API_KEY"] | |
DEBUG = os.environ["DEBUG"] or false | |
# initialize api client | |
api = xively.XivelyAPIClient(API_KEY) | |
def read_temp_raw(): | |
f = open(device_file, 'r') | |
lines = f.readlines() | |
f.close() | |
return lines | |
def read_temp(): | |
lines = read_temp_raw() | |
while lines[0].strip()[-3:] != 'YES': | |
time.sleep(0.2) | |
lines = read_temp_raw() | |
equals_pos = lines[1].find('t=') | |
if equals_pos != -1: | |
temp_string = lines[1][equals_pos+2:] | |
temp_c = float(temp_string) / 1000.0 | |
temp_f = temp_c * 9.0 / 5.0 + 32.0 | |
#return temp_c, temp_f | |
return temp_f | |
# function to read 1 minute load average from system uptime command | |
def read_loadavg(): | |
if DEBUG: | |
print "Reading load average" | |
return subprocess.check_output(["awk '{print $1}' /proc/loadavg"], shell=True) | |
# function to return a datastream object. This either creates a new datastream, | |
# or returns an existing one | |
def get_datastream(feed): | |
try: | |
datastream = feed.datastreams.get("temp_f") | |
if DEBUG: | |
print "Found existing datastream" | |
return datastream | |
except: | |
if DEBUG: | |
print "Creating new datastream" | |
datastream = feed.datastreams.create("temp_f", tags="temp_01") | |
return datastream | |
# main program entry point - runs continuously updating our datastream with the | |
# current 1 minute load average | |
def run(): | |
print "Starting Xively tutorial script" | |
feed = api.feeds.get(FEED_ID) | |
datastream = get_datastream(feed) | |
datastream.max_value = None | |
datastream.min_value = None | |
while True: | |
temp_f = read_temp() | |
print(read_temp()) | |
#load_avg = read_loadavg() | |
if DEBUG: | |
print "Updating Xively feed with value: ", temp_f | |
datastream.current_value = temp_f | |
datastream.at = datetime.datetime.utcnow() | |
try: | |
datastream.update() | |
except requests.HTTPError as e: | |
print "HTTPError({0}): {1}".format(e.errno, e.strerror) | |
time.sleep(10) | |
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
FEED_ID=2000810657 API_KEY=p5aGJfL0Lx68ujLxH3qVOcNszLGtJgBe5DwY0A0mO8O53JN0 DEBUG=true python ./xively-temp-sensor.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is temp sensor feed running from my Pi at home:
https://xively.com/feeds/2000810657
Xively (formerly Cosm & Pachube) tutorial: https://xively.com/dev/tutorials/pi
Here's the code: https://gist.github.com/pdp7/8867670
I'm reading the temp via this tutorial: http://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing/overview