View Single Post
Old 09-24-12, 01:04 AM   #22
opiesche
Helper EcoRenovator
 
Join Date: Jul 2012
Location: Rohnert Park, CA
Posts: 99
Thanks: 4
Thanked 14 Times in 9 Posts
Default

As requested

Adventures in home improvement: Temperature Logging

I'll update this with some more pictures and information, and finally code, this week. Just to show how simple it really was, here's the script that reads the sensors and outputs the values to a .csv file. I'll eventually make this prettier and more flexible as well (right now it's hardcoded for my particular sensors and setup).


Code:
#!/usr/bin/python

import os
import datetime

def extract_temp(filename):
        temp = 0.0
        lines = open(filename, 'r').readlines()
        line = lines[1]
        tempstr = line[29:34]
        temp = float(tempstr)
        temp = temp/1000.0
        return temp


# read the sensors and stick their output to a temporary file
os.system("cat /sys/bus/w1/devices/w1_bus_master1/28-000003742037/w1_slave > /tmp/curtemp1.txt")
os.system("cat /sys/bus/w1/devices/w1_bus_master1/28-0000040df025/w1_slave > /tmp/curtemp2.txt")

# grab the temperature only from the file (there's other data in the sensor output)
temp1 = extract_temp("/tmp/curtemp1.txt")
temp2 = extract_temp("/tmp/curtemp2.txt")

# now log to file with timestamp
date_str = str(datetime.datetime.today())[0:10]
time_str = str(datetime.datetime.now())[11:19]

strn = time_str+", "+str(temp1)+", "+str(temp2)

filename = "/home/raspbian/"+date_str+".csv"
with open(filename, "a") as outfile:
        outfile.write( strn+"\r\n" )

print strn
I'll keep tinkering and adding to this. The source code and build instructions will likely end up on github when I'm done.
The cool thing about doing this with the Raspberry Pi is, that it's network accessible (I grab the plot images from a webserver running on it) and that I can program it remotely via SSH. It can sit in its current location running (at about 1W, by the way!), and I can tweak it, tune it, and grab the data and plots from it without ever touching the physical machine

Last edited by opiesche; 09-24-12 at 01:22 AM..
opiesche is offline   Reply With Quote