I recently bought a Raspberry Pi to experiment with. The first things I did with it was connecting and reading out Maxim Integrated DS1820 one wire temperature sensor and the cheap Chinese DHT11 temperature / relative humidity sensor.
To connect the sensors, I soldered some female pin headers to a piece of PCB. For the one wire network I soldered a 4.7K resistor between Vcc on pin1 (3.3V) and GPIO4 on pin 7. GPIO4 is the data pin for one wire, I used GPIO2 as the data pin for the DHT11. DHT11 “modules” with a PCB and three pins have their own pull up resistor, the separate 4 pin sensors don’t. Shown below is the quick setup, I soldered the DS1820 directly to the PCB, and connected the DHT11 with some wires for 3.3V, data and ground.
GPIO pinouts differ between the Raspberry Pi versions, I found the following pinout to be correct for my Raspberry Pi, a model A version 2.
Reading out the sensors is easy. For the DS1820, load the required kernel modules which are included in the Raspbian Linux distribution:
sudo modprobe w1-gpio
sudo modprobe w1-therm
Better yet, add the modules to /etc/modules so that they are loaded at boot time 🙂
When the modules are loaded, the one wire sensors can be found as follows:
johan@raspberrypi ~ $ ls /sys/bus/w1/devices/
10-000801f58124 w1_bus_master1
Reading out the sensor is done by cat’ing the file w1_slave in the directory with the long number (this is the unique serial of each one wire device):
johan@raspberrypi ~ $ cat /sys/bus/w1/devices/10-000801f58124/w1_slave 34 00 4b 46 ff ff 0a 10 52 : crc=52 YES 34 00 4b 46 ff ff 0a 10 52 t=26125
The command returns some hexadecimal values which say whether the CRC of the data from the sensor is correct, and a raw temperature value. To get the temperature divide by 1000.
Since I have never used DHT11 modules I concentrated on getting data logging working with them first.
The DHT11 sensors can be read out using the Adafruit DHTxx script. Instructions on getting this script can be found here.
To test a sensor, run the script, specifying the type of sensor and the GPIO pin it’s on:
johan@raspberrypi ~ $ sudo scripts/Adafruit-Raspberry-Pi-Python-Code/Adafruit_DHT_Driver /Adafruit_DHT 11 2 Using pin #2 Data (39): 0x27 0x0 0x16 0x0 0x3d Temp = 22 *C, Hum = 39 %
As you can see the sensor returns a temperature and relative humidity value. This data is processed and logged with the following script:
#!/bin/sh
# Script for polling DHT11 values for passing to SNMP
# Johan Boonstra 29/04/2013
#
LOGFILE=/var/log/dht11_1.log
# check arguments
if [ “$1” = “log” ]
then
echo -n $(date) >> $LOGFILE
# check that data is in fact numeric and not something else (when readout fails)
while [ -z $(echo $temp | grep -E ‘^-?[0-9]*\.?[0-9]*$’) ]
do
# run readout script, tail last line, pipe to awk to extract data from column 3 or 7
temp=$(/home/johan/scripts/Adafruit-Raspberry-Pi-Python-Code/Adafruit_DHT_Driver/Adafruit_DHT 11 2 | tail -n1 | awk {‘ print $3’})
done
#echo label and temp/hum data to log file
echo -n ” temperature: ” >> $LOGFILE
echo -n $temp >> $LOGFILE
while [ -z $(echo $humidity | grep -E ‘^-?[0-9]*\.?[0-9]*$’) ]
do
humidity=$(/home/johan/scripts/Adafruit-Raspberry-Pi-Python-Code/Adafruit_DHT_Driver/Adafruit_DHT 11 2 | tail -n1 | awk {‘ print $7’})
done
echo -n ” humidity: ” >> $LOGFILE
echo $humidity >> $LOGFILE
exit
fi
# display section to present data to SNMP
# check if log is up to date, updated in last 6 minutes
if [ `find $LOGFILE -mmin -6` ]
then
# check arguments
if [ “$1” = “temp” ]
then
#print temperature
temp=$(tail -n1 $LOGFILE | awk {‘ print $8’})
echo $temp
exit
fi
# check arguments
if [ “$1” = “humidity” ]
then
#print humidity
humidity=$(tail -n1 $LOGFILE | awk {‘ print $10’})
echo $humidity
exit
fi
# give error message when log is not up to date
else
echo “Error – logfile too old”
fi
# output raw data for debugging when no argument is given
if [ “$1” = “” ]
then
/home/johan/scripts/Adafruit-Raspberry-Pi-Python-Code/Adafruit_DHT_Driver/Adafruit_DHT 11 2
fi
Update: watch out as this script does not check if something is already checking the sensor. I’m looking into a more robust script that runs continuously and checks if it’s already running.
Also: Compile the Adafruit code with BCM2835 libraries for better performance.
This script performs various functions. It can log, display temperature or humidity or display the raw output. Since polling the sensor sometimes fails, requires root and is slow, logging is done by calling the script with the log argument each 5 minutes using the following entry in /etc/crontab/:
*/5 * * * * root /home/johan/scripts/logDHT11_1 log
To interface with SNMP, the following lines are added to /etc/snmp/snmpd.conf :
extend .1.3.6.1.4.1.2021.2000.1 temp /home/johan/scripts/logDHT11_1 temp extend .1.3.6.1.4.1.2021.2000.2 humidity /home/johan/scripts/logDHT11_1 humidity
To graph a value in monitoring software such as cacti, you need the correct OID which displays the desired information. To find it out, run snmpwalk on the Raspberry Pi:
johan@raspberrypi ~ $ snmpwalk -v2c -c public localhost .1.3.6.1.4.1.2021.2000.2 | grep STRING iso.3.6.1.4.1.2021.2000.2.2.1.2.8.104.117.109.105.100.105.116.121 = STRING: "/home/johan /scripts/logDHT11_1" iso.3.6.1.4.1.2021.2000.2.2.1.3.8.104.117.109.105.100.105.116.121 = STRING: "humidity" iso.3.6.1.4.1.2021.2000.2.3.1.1.8.104.117.109.105.100.105.116.121 = STRING: "39" iso.3.6.1.4.1.2021.2000.2.3.1.2.8.104.117.109.105.100.105.116.121 = STRING: "39" iso.3.6.1.4.1.2021.2000.2.4.1.2.8.104.117.109.105.100.105.116.121.1 = STRING: "39"
I usually use the most unique OID from the list, never had any problems with it.
Following is the current graph of the relative humidity: