IOT – temperature sensor project

So I’ve been experimenting with a Raspberry Pi in hopes of eventually creating a project to possibly make one of those really cool animated christmas light displays that synchronize with music.  That’s definitely something for the future but I’d still like to do it.

 

What I was able to do was access a thermometer sensor connected to the GPIO pins on the pi.  I had a heck of a time even getting some LED’s to turn on and off until I switched to a different unit.  This makes me think the original one is possibly defective or I may have fried the GPIO board. I bought a B+ which is the precursor to the Raspberry Pi 2.  It has the same basic configuration but less powerful than the 2.  It has 256mb of memory and slower CPU but more than adequate for my needs.  I connected a DS18B20 waterproof temperature sensor and was having trouble reading from it until I realized that you have to use a specific GPIO pin.

 

This particular sensor uses a protocol called 1wire.  It has to be enabled in the kernel and then rebooted to load the drivers.  Once you do that, you should see a directory in /sys/bus/w1/devices/ that starts with 28-XXXXXXXXXXXX.  The X’s represent a hex value that will be different for each sensor.  Then you can cd into the directory and cat the contents of w1_slave.  You should see something like this:

 

root@garagepi:/sys/bus/w1/devices/28-000006b62cb7# cat w1_slave
3a 01 4b 46 7f ff 06 10 42 : crc=42 YES
3a 01 4b 46 7f ff 06 10 42 t=19625
root@garagepi:/sys/bus/w1/devices/28-000006b62cb7#

 

The number at the end of the second line is the temperature in Celcius.  You have to divide that number by 1000 to get the actual reading (basically just move the decimal point 3 positions to the left):

19625/1000 = 19.625 c

The formula to convert from Celsius to Fahrenheit is T = {temp in Celsius} x 9 / 5 + 32. So here’s the reading in Fahrenheit:

19625/1000 = 19.625 C
19.625 x 9 = 176.625
176.625 / 5 = 35.325
35.325 + 32 = 67.325 F

67 degrees Fahrenheit is the current temperature in my garage.  Then I wrote a quick script to poll the sensor and do the math to convert to Fahrenheit and it spits out just the temperature value:

#!/usr/bin/python

import os
import glob
import time

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'

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_f

while True:
        print(read_temp())
        quit()

It was a python script I found that uses the 1wire protocol to read the contents of the sensor. I modified it to spit out just the one reading. I then configured MRTG to graph out the temperature historically using the following mrtg.cfg:

######################################################################
# Multi Router Traffic Grapher -- Sample Configuration File
######################################################################
# This file is for use with mrtg-2.5.4c

# Global configuration
WorkDir: /var/www/mrtg/

Timezone[temp]: America/Chicago
Target[temp]: `/usr/local/bin/get-temp.py 0`
MaxBytes[temp]: 99
Title[temp]: Garage Temperature
PageTop[temp]: Garage Temperature
ShortLegend[temp]: F 
YLegend[temp]: Farenheit 
Options[temp]: growright,nopercent, nobanner, noinfo, gauge 
Unscaled[temp]: ymd

I’m not going to go into how to set up MRTG, you’ll have to google that or get help from someone if you’re having trouble. Regardless, here’s the output I’ve gotten so far:

 

Garage Temperature

The statistics were last updated Saturday, 10 October 2015 at 12:35 America/Chicago

`Daily’ Graph (5 Minute Average)

temp-day

Max Average Current
In 69.0 F 63.0 F 69.0 F
Out 0.0 F 0.0 F 0.0 F

`Weekly’ Graph (30 Minute Average)

temp-week

Max Average Current
In 68.0 F 61.0 F 65.0 F
Out 0.0 F 0.0 F 0.0 F

`Monthly’ Graph (2 Hour Average)

temp-month

Max Average Current
In 66.0 F 56.0 F 60.0 F
Out 0.0 F 0.0 F 0.0 F

`Yearly’ Graph (1 Day Average)

temp-year

Max Average Current
In 0.0 F 0.0 F 0.0 F
Out 0.0 F 0.0 F 0.0 F
GREEN ### Incoming Traffic in Bytes per Second
BLUE ### Outgoing Traffic in Bytes per Second

You can see that MRTG normalizes data as it averages farther out in time by observing the max high and low are different in the year chart than the daily chart.  It’s averaging out the values of the previous level of data.  Once you get to the yearly graph- it’s taking the average of an entire day’s high temperatures to get the high temperature for the year.  I may have to adjust how I tell MRTG what it’s reading so it’s more accurate.  I’d like to see the day’s high, low and current reading.  For now I’m content with what I have so far.

 

I hope to expand this to gathering humidity as well- I have another sensor that measures both humidity and temp but I haven’t gotten that one working quite yet.  Anyway- I hope you find this interesting and possibly useful in setting up your kit.  Good luck!

Advertisements

One thought on “IOT – temperature sensor project

  1. Pingback: Temperature monitoring script with email alerts | Eric Steed – Technology Evangelist

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s