Home heating monitor
Visualise your central heating.
You will need...
For this project, we’ll dunk our heads into the Internet of Things (IoT). We’ll determine the temperature of our home using a cost-effective sensor and push that data to the cloud and use it to populate a graph. The sensor we’re using is a Dallas DS18B20. These can be picked up relatively cheaply, but an easy solution is to buy the CamJam EduKit 2 as it includes a waterproof Dallas DS18B20. Assemble the hardware and attach to your Pi as per the diagram ( see right). Next, we set up the sensor and there’s a handy Cam Jam worksheet ( http://bit.ly/CamJamTempWorksheet) for this. To proceed you’ll need an www.initialstate.com account and your API key, which you’ll find in your account settings. To install the Initial State streamer type: \curl -sSL https://get.initialstate.com/python -o - | sudo bash
We start our code by importing libraries to work with the OS, time and to stream our data to the cloud: import os, glob, time from ISStreamer.Streamer import Streamer
Next, we load the kernel modules for the sensor using modprobe , we wrap the Bash commands in an os.system() function for Python and tell our code where to find the file for storing the temperature data: 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’
Next, we create a function to handle reading the contents of the file which stores the raw temperature data and stores the data as a variable. def read_temp_raw(): f = open(device_file, ‘r’) lines = f.readlines() f.close() return lines
Now we read the data and process it into something more usable. We keep the information and strip the rest of the data before converting the data to a temperature. 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 return temp_c Our last section is a loop that constantly checks the temperature, performs conversions and streams the data to Initial State every minute. while True: temp_c = read_temp() temp_f = temp_c * 9.0 /5.0 + 32.0 streamer.log(‘temperature (C)’, temp_c) streamer.log(‘temperature (F)’, temp_f) time.sleep(60) Save the code and click on Run > Run Module to start.