<file_sep>Temperature Logger for Raspberry Pi
This uses the DS18B20 digital temperature sensor to log data to an sqlite3 database
<file_sep>import plotly.plotly as py
from plotly.graph_objs import Scatter, Layout, Figure
import os
import time
import datetime
import temp

username = os.environ['PLOTLY_USERNAME']
api_key = os.environ['PLOTLY_API_KEY']
stream_token = os.environ['PLOTLY_STREAM_TOKEN']

py.sign_in(username, api_key)

trace1 = Scatter(
	x=[],
	y=[],
	stream=dict(
		token=stream_token,
		maxpoints=20000
	)
)

layout = Layout(
	title = 'Raspberry Pi Temperature Log'
)

fig = Figure(data=[trace1], layout=layout)

print py.plot(fig, filename='Raspberry Pi Temperature Stream')

stream = py.Stream(stream_token)
stream.open()

while True:
	temperature = temp.toF(temp.readTemp())
	stream.write({'x': datetime.datetime.now(), 'y': temperature})
	temp.logData(temperature)
	time.sleep(60)
<file_sep>import time
import datetime
import sqlite3 as lite
import sys

def readTemp():
	tempfile = open("/sys/bus/w1/devices/28-0000065d9c99/w1_slave")
	text = tempfile.read().split("\n")[1].split(" ")[9]
	tempfile.close()
	temp = float(text[2:])/1000
	return temp

def toF(temp):
	return temp * (9.0/5.0) + 32.0

def logData(temp):
	conn = lite.connect('temp.db')
	cur = conn.cursor()
	try:		
		cur.execute("INSERT INTO tbl VALUES (? , ?)",[toF(readTemp()),datetime.datetime.now()])
		conn.commit()
	except lite.Error, e:
		if conn:
			conn.rollback()
		print "Error %s:" % e.args[0]
		sys.exit(1)
	finally:
		if conn:
			conn.close()
