Temperature and humidity will have a substantial effect on the lifespan of your hardware. Almost all computers and servers have several temperature sensors for gathering component or ambient temperatures. Humidity sensors though, are often overlooked. With very few and inexpensive components, we can monitor both in Zabbix.
Here we are going to hook up an DHT22/AM2302 sensor to a Raspberry Pi 3, load Raspberry Pi OS onto it, install the Zabbix Agent and configure it to pull temperature and humidity data through a small Python script.
Although this can seem difficult to accomplish, trust me, it isn’t!
Requirements
- Raspberry Pi 3
- Power Supply
- DHT22 / AM2303 Sensor Module
- MicroSD Card
I’m not going to go through the process of downloading Raspberry Pi OS and installing it in this guide, so I will assume you have a Pi all setup and running ready to go.
- Hook up your Raspberry Pi and connect via SSH
Connect your Raspberry Pi to your local network with an Ethernet cable and plug in the power supply. After about 30-60 seconds, the Raspberry Pi is booted.
Connect to the Pi using the Putty SSH client. The default username is “pi” and the password is “raspberry”.
If you are having problems SSH’ing into your Pi, you can find a guide on how to fix that here.
- Connect the sensor module
With the Pi booted and ready to go, we now have to connect the sensor module.
The sensor module has 3 pins. Looking at the front, they are:
Left: 3.3v
Middle: GPIO
Right: Ground
With the module, you also received a set of wires, hook those up making a note of what colours go to what pin on your sensor.
The pins now have to be connected to the Pi’s GPIO pins, which is the two large rows of pins at the edge of the Pi board. Note, Raspberry Pi 2 and 3 pin placements are identical.
Connect the wires in this order:
Red (3.3v) → Any orange pin
Brown (GPIO) → Pin number 2
Black (ground) → Any black pin
You can take a peak at the top image, to see how I’ve connected my module.
- Installing software on the Pi
Having earlier connected to the Pi via SSH, we now have to install the necessary software to pull temperature and humidity data from the sensor module. In the Putty SSH console, do the following:
sudo apt update && sudo apt -y install git python-dev python-setuptools
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT/
sudo python setup.py install
This ensures we have the necessary Python module compiled and installed, which enables us to pull data via the GPIO integrated circuit.
- Test your sensor!
We should now be able to pull data from the module with an already provided example script. To do so:
sudo /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 2
In your console, you should now receive output similar to: The first argument denotes the model we’re using, the DHT22. The second argument is the GPIO pin # we chose earlier. The temperature received is in Celcius and the humidity is in percent.
We’ve successfully pulled data from the sensor module. Let’s configure Zabbix Agent to do the same!
- Zabbix Agent configuration
We can run Python scripts manually, but luckily the Zabbix Agent can as well. First, install the Zabbix Agent on the Pi:
sudo apt -y install zabbix-agent
We need to slightly modify the Zabbix Agent configuration file. Open the configuration file:
sudo nano /etc/zabbix/zabbix_agentd.conf
To get stable polling at the server you will need to adjust the Timeout value to 10 in zabbix_agentd.conf
NOTE: The “Server” parameter is used for whitelisting IP addresses you wish to allow communication with the Zabbix Agent. For testing purposes, we’re using the default of “127.0.0.1” which means we can ONLY communicate with the agent locally. This parameter will need to be configured to the correct IP address of your Zabbix server when you wish for the server retrieve data from the agent.
Insert this line exactly as is, at the end of the configuration file:
UserParameter=dht.pull[*],sudo /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 2 | awk -F[=*%] '{print $1}'
Ctrl+X to quit, “Y” to save and then <Enter>.
NOTE: Some might experience longer than usual polling time when querying environment data from the sensor module. This can result in the Zabbix Agent UserParameter script timing out. To circumvent this issue, you can increase the configuration parameter ‘Timeout’ from the default of 3 seconds, to 5 seconds instead, or longer if necessary.
Restart the Zabbix Agent:
sudo service zabbix-agent restart
We also have to grant the “zabbix” user the correct permissions to pull GPIO data. We need to allow it to act as the root superuser, when executing the AdafruitDHT.py script. Raspbian uses the “sudo” program for this. Open the “sudo” configuration by typing:
sudo visudo
And the bottom of the file, insert:
zabbix ALL=(ALL) NOPASSWD: /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py
Ctrl+X to quit, “Y” to save and then <Enter>.
- Retrieving temperature and humidity with the Zabbix Agent
For testing purposes, we need the “zabbix_get” program. For Raspbian, it comes as part of the “zabbix-proxy-sqlite3” package which we then need to install:
sudo apt -y install zabbix-proxy-sqlite3
At last, we are now ready to perform the final test. The agent item key we wish to pull is “dht.pull”. It has two parameters we can choose from; “2” to retrieve temperature in celcius and “4” to retrieve humidity in percent.
Let’s test it:
zabbix_get -s 127.0.0.1 -k dht.pull[2]
zabbix_get -s 127.0.0.1 -k dht.pull[4]
We’re done! We can now easily pull data about environment humidity and ambient temperature!
The Raspberry Pi could now be placed at a remote site, to monitor the environment there. This would then enable you to receive an alert of any sudden fluctuations in temperature and humidity happens!
Additionally, I’ve created a small template that pulls temperature and humidity every 60 seconds. Get it here.
Leave a Reply