Monitor Temperature
Raspberry Pi

Preliminary Discussion

A common function of network management is to monitor the environment in the network equipment rooms. Things to watch are temperature, humidity, smoke, entry into the room, and so on. This can be done by purchasing devices that incorporate various sensors and notification methods. In this lab we will see how these devices do what they do as well as learn how to create such a device.

Setup

The following equipment is needed for this lab:

Video camera that can stream video to a computer for display by a projector

The following parts are needed for this lab:

Raspberry Pi Board

Raspberry Pi Case

Raspberry Pi Power Supply

DS18B20 Temperature Sensor

4.7K Ohm Resister

Solderless Breadboard

Three Jumper Wires

An Internet Connection

This program is required:

Any method that allows interaction with the Raspberry Pi will work. In this example the following are used:

RealVNC server running on the Raspberry Pi

RealVNC Client running on a computer attached to the same network as the Raspberry Pi

Assemble the Parts

The parts are laid out in this manner.

The active components used for this lab perform the following functions:

The Raspberry Pi board reads the input from the sensor and then outputs it. In this example we see it as a temperature display on a monitor.

The datasheet for the DS18B20 temperature sensor states that the DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line and ground for communication with a central microprocessor. It has an operating temperature range of -55°C to +125°C and is accurate to ±0.5°C over the range of -10°C to +85°C.

The resistor is used as a pull-up from the DATA to VCC lines. Sparkfun explains a pull-up resistor this way:

Let’s say you have an MCU with one pin configured as an input. If there is nothing connected to the pin and your program reads the state of the pin, will it be high - pulled to VCC - or low - pulled to ground? It is difficult to tell. This phenomenon is referred to as floating. To prevent this unknown state, a pull-up or pull-down resistor will ensure that the pin is in either a high or low state, while also using a low amount of current.

Now that we have all of the pieces of the lab, let’s assemble them.You can see how I do this by watching the camera view projected on the screen.

Here is where the parts go

Insert the temperature sensor into breadboard holes a1 a2 a3 with the flat side of the sensor toward row b.

Insert the resistor into holes e1 e2

Insert a jumper wire into hole d2 and then to the GPIO pin 4

Insert a jumper wire into hole c1 and then to GPIO pin +3.3v

Insert a jumper wire into hole c3 and then to GPIO pin GND

The finished system looks like this:

Let’s apply power to the system by plugging the USB cable into the port on the Raspberry Pi and the other end to apower outlet.

Load the Code

So that the parts can talk to each other and to us, we need to run a Python script.

importos, glob, 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'

defread_temp_raw():

f = open(device_file, 'r')

lines = f.readlines()

f.close()

return lines

defread_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=')

ifequals_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

returntemp_c, temp_f

while True:

print("temp C=%f\ttemp F=%f" % read_temp())

time.sleep(1)

Watch the Result

With the code loaded the temperature will be displayed. For example,

If we increase the heat by breathing on the sensor the temperature will increase.

The data could be sent the same way to a management program that would notify the network administrator that the temperature in the room was exceeding preset limits.

Source

The basic procedure used here is from thebook Raspberry Pi Cookbook 2nd Edition.

1

Copyright 2014 Kenneth M. Chipps Ph.D.