The Server Control Cabinet (or SCC) turns out to be far more complex. So I decided to split it into a few separate posts. This part here deals with measuring the temperature using DS18B20 sensors.
Measuring the temperature
The DS18B20 sensor measures temperatures. It is a ‘onewire’ or ‘1-wire’ device, which makes it possible to run a number of different sensors off the same GPIO pin. In my case, this is ideal, as the Raspberry Pi has a limited amount of Pins. Adafruit has a nice tutorial with one sensor. As I use two (one internal, one external) I have adapted it to work with two. Code further below. I wanted to use a different pin than GPIO4 for the sensors, but somehow, it didn’t work. In the Adafruit tutorial, GPIO4 is used. As it turns out, GPIO4 is the dedicated pin for ‘one-wire’ sensors and they won’t work on any other pins. Thanks to Frank Buss for pointing this out in his post.
Adapting the code
I made some minor changes to the python tutorial to test the temperature part of the project:
import os import glob import time os.system('modprobe w1-gpio') os.system('modprobe w1-therm') s1 = '28-000004dbc27c' s2 = '28-000004dc52ef' base_dir = '/sys/bus/w1/devices/' device_folder_s1 = glob.glob(base_dir + s1) device_folder_s2 = glob.glob(base_dir + s2) device_file_s1 = device_folder_s1 + '/w1_slave' device_file_s2 = device_folder_s2 + '/w1_slave' def read_temp_raw(sensor): if sensor == 's1': device_file = device_file_s1 else: device_file = device_file_s2 f = open(device_file, 'r') lines = f.readlines() f.close() return lines def read_temp(sensor): lines = read_temp_raw(sensor) while lines.strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw(sensor) equals_pos = lines.find('t=') if equals_pos != -1: temp_string = lines[equals_pos+2:] temp_c = float(temp_string) / 1000.0 temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c, temp_f while True: print(read_temp('s1')) print(read_temp('s2')) time.sleep(1)
Loading the module automatically
For the temperature sensors to work after a restart, the 1-wire module has to be loaded automatically at startup. Add a new line with “w1-gpio” to the file “/etc/modules”.
Switching the fan on and off
The GPIO pins do not provide enough power to drive a fan directly nor to switch a relay on or off.
Testing the circuits
The fan relay.
Kevin Sangeelee has an excellent post about how to use a relay with a Raspberry Pi.