I recently remodeled my home office and I now have a dedicated closet for my electronics (Server, NAS, AV Receiver, etc.) During the build I planned for heat remediation by installing an exhaust fan that dumps air from the closet into my adjoining office. However, the temperature in the closet hovers around 90°F (32°C), even with the fan on. Although this temperature is within hardware thresholds, it’s a bit warmer than I would prefer. To get a better understanding of my heat dissipation needs, I decided to monitor and record temperature fluctuations over several days to see what temperature ranges I was experiencing.
Monitoring temperature levels is a perfect project for the Raspberry Pi. I have used an analog TMP36GZ low voltage temperature sensor before in an Arduino project but this would be my first attempt at using the Raspberry Pi’s GPIO pins. Unfortunately, after a bit of research, I discovered that my analog temperature sensor wouldn’t work with the Raspberry Pi’s “digital only” IO pins. While I could have prototyped a solution using an ADC and some spare components, I really wanted a simple build so I could just start coding on the Pi.
The solution to my problem was a DS18B20 Digital Temperature Sensor IC which I found on Amazon.com. The DS18B20 uses the 1-Wire communication bus which is perfect for the BCM GPIO4 pin (PIN 7) on the Raspberry Pi. Other caveats, you can work with the DS18B20 from the Linux terminal, and you can connect multiple 1-Wire devices, in series, to PIN 7.
I had some spare CAT5e cable so I stripped and soldered 3 wires to the three pins on the sensor – orange for +3.3v, brown for ground, and green for data. Also, the DS18B20 requires a pull-up resistor between the power and data leads.
Then, I used electrical tape to insulate the exposed areas and I shrink-wrapped everything to protect the connections.
To the other end of the CAT5e cable I attached three female jumper wire cable housing connectors. These will principally be used for quick connections to a splitter rather than connecting directly to the Pi because I need to connect several devices to a single pin (specifically PIN7 for 1-wire).
Next, I manufactured three tiny Y-splitters (2 male to 1 female) to join the VDD, DQ, and GND lines from 2 sensors before connecting to the Pi.
Here is the finished build. Note the three splitters are plugged into PIN1 (orange/3.3v), PIN6 (brown/GND), and PIN7 (green/data).
After connecting the DS18B20’s to the Raspberry Pi, you can interact with the devices using the below terminal commands. Note, your device IDs will be specific to your 1-Wire devices. In my case, my devices are 28-0000055f311a and 28-0000055f327d.
#Load the drivers
sudo modprobe w1-gpio
sudo modprobe w1-therm
#Show available devices (example 28-0000055f327d)
#Read the output from the device
Here is what my terminal window looks like after running the above.
In the terminal window above, the interesting information from reading from the device is the first line ending with “YES” – which means no errors – and the second line ending with “t=” followed by the temperature in thousandths of degrees Celsius (°C * 1000). Using this information, we can divide the temperature by 1000 to get Celsius and convert to Fahrenheit using…
°F = °C * (9/5) + 32.
Once we verify that the DS18B20 is operating correctly using terminal commands, we can construct a python script to read from the devices and write the temperature to a CSV file. Later we will plot the temperature readings to see how much they fluctuate over time.
#Change to your device path(s)
lst = 
#Change to your Log Path
temp_log = '/mnt/nas/kevin/RPi/temp.csv'
date_log = str(datetime.datetime.now())
#To read the sensor data, just open the w1_slave file
f = open(device, 'r')
data = f.readlines()
deg_f = ''
if data.strip()[-3:] == 'YES':
temp = data[data.find('t=')+2:]
#If temp is 0 or not numeric an exception
#will occur so lets handle it gracefully
deg_f = 32
deg_f = (float(temp)/1000)*9/5+32
print "Error with t=", temp
for device in lst:
device_name = device.split('/')
with open(temp_log, 'a') as f:
s = device_name + ','
s += date_log + ','
s += str(get_temp(device)) + '\r\n'
#When there are multiple devices, a short pause
#interval between reading sensors seems to work best
Once created, you can run the above Python script from the terminal by using the following command.
sudo /usr/bin/python /var/www/Documents/Temperature.py
Running the above terminal command a few times produces output, like the below, in a CSV file. Note that I have 2 Temperature sensors connected to the Pi. One is hanging in the center of the top of the closet, measuring ambient temperature, and the other is attached to the hottest exterior surface on my server.
Now that we have a script to collect temperature data and save it to a CSV file, we need to schedule it to run periodically. In my case, I wanted to run the script every minute of every day for several days. To schedule the python script, we can use crontab from the terminal.
#List current crontab jobs
sudo crontab –l
#edit the list of crontab jobs
sudo crontab –e
While in edit mode, add a crontab job using the following syntax. Note that this command should be all on one line in crontab.
#Note, the below should be entered ALL ON ONE LINE
* * * * * sudo /usr/bin/python
> /dev/null 2>&1
The five asterisks (*) mean to run the job every minute, every hour, every day of month, every month, and every day of the week. The part about “> /dev/null 2>&1″ just means don’t save the output or errors in a log and don’t show anything on any screens (i.e. run silently even if errors occur).
After a few days of collecting data, I can plot the results to see how effective I am at dissipating heat from my closet. Below is a chart of the output after running the Python script, via crontab, for several days.
Now that I have a baseline, I can experiment with different heat dissipating methods to find what works best to keep my electronics cool. When I’m done monitoring my electronics closet, I can see myself redeploying the rig to other projects such as attics, crawl spaces, automotive projects, and mini-fridge hacking. Let me know in the comments if you come up with your own temperature monitoring projects.