There are several items in the community forum about reading a DHTxx sensor, but I couldnt find anything comprehensive in the Tutorials and Examples… so, I guess the task to write a practical Tutorial is up to me.
As for coding it is no use to re-invent the wheel, I fortunately found someone on this forum (@psyciknz ) had done most of the work, albeit for a DHT22, while I am using a DHT11 but that was easy to alter.
For the next steps I presume you already have python installed. Jessie already should have python 2.7 installed.
The Python program calls a few libraries that you most likely already have, maybe with exception of the paho.mqtt.client and the adafruit DHT library.
the mqtt client can be installed as follows:
pip install paho-mqtt
if for whatever reason this does not work for you, try:
git clone https://github.com/eclipse/paho.mqtt.python.git (=> download the client)
cd paho.mqtt.python (=> go to proper directory)
python setup.py install (=> install the client)
depending on how you are logged in you may need to add ‘sudo’ before the install commands
The mqtt client is now installed.
For the DHT library do the following:
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
=> download the library
cd Adafruit_Python_DHT
=> go to proper directory
Before you install the library, you need some dependencies that may or may not be on your system:
sudo apt-get update
sudo apt-get install build-essential python-dev python-openssl
If you already had modules installed those will be skipped.
Then install the library:
sudo python setup.py install
(=> install the library)
At this moment it is a good idea to restart your raspberry.
If you want -when your raspberry is back up and running- you could now try one of the example programs in the Adafruit DHT library, just to make sure everything is working.
Connect your DHT11 to e.g. GPIO4 of your raspberry.
Make sure you are still in directory: /home/openhabian/Adafruit_Python_DHT
do:
cd examples
sudo ./AdafruitDHT.py 11 4
You will get the temperature and Humidity reported in your terminal
Now download the DHT_sensor program.
If like me you want to use a DHT11 instead of a DHT22, change line 45:
DHT_TYPE = Adafruit_DHT.DHT22
into
DHT_TYPE = Adafruit_DHT.DHT11
And save the program on your raspberry. Be careful in editing though. Python is rather picky regarding indents and you could easily get error messages if you are not careful. I suggest you just change the number 22 into 11 if you are using a DHT11. Don’t change anything else unless you are confident in what you are doing.
I saved it directly in my home/openhabian directory, i.e. the directory that is availble directly after logon
Now have a look at the usage instructions in the program (lines 36-42), but beware:
Presuming you saved the program as “mqtt.dhtsensor.py”, that is the name you need to use instead of “mqtt.channel.py” (the name mentioned in the “Usage”).
make sure you have execution rights to the mqtt.dht.sensor.py file. I usually set priviliges to 0777, I do that direct through webmin but you could also do chmod +x mqtt.dhtsensor.py
For testing I started my program as follows:
./mqtt.dhtsensor.py 'cupboard/temperature1' 'cupboard/humidity1' 4
This says that as my mqtt topics I have cupboard/temperature1 and cupboard/humidity1 and my DHT11 sensor is attached to GPIO pin 4 while the refresh rate is 300 seconds (the default).
To make it a bit easier I created a file called “dht.sh” that contained the single line:
./mqtt.dhtsensor.py 'cupboard/temperature1' 'cupboard/humidity1' 4 50
That does the same as the earlier line, but now I set the refresh rate to 50 seconds. I put it in one file because that is easier to type than having to type the main program with the arguments. Again, make sure you have execution rights to this file by setting chmod +x dht.sh
If you want you can make the dht.sh file to automatically start at boot as I describe below.
The MQTT messages are subsequently sent out over the network.
And one could use definitions like below in your OpenHAB Items file to use them in OpenHAB
Number Cupboard_temp "Riser temperatuur [%.1f °C]" <temperature> (GF_Corridor) {mqtt="<[mosquitto:cupboard/temperature1:state:default]"}
Number Cupboard_humidity "Riser humidity [%.0f %%]" <line> (GF_Corridor) {mqtt="<[mosquitto:cupboard/humidity1:state:default]"}
Starting the script at boot
The script will run perfectly when started manually, but it will run in an instance of the SSH window and the moment you close that window, the script will stop. It is therefore better to have the script starting at boot. This is simple to do, but you have to be aware of the full PATH you are using.
Change the content of the dht.s file as follows:
Then in your preferred editor open the etc/rc.local file and add a line (the line at the red arrow):
The “&” behind the filename is to ensure the rc.local file does not wait for the (endless) script to finish.
Save the file and reboot. You will now no longer see the temperature and humidity in an ssh window, but the MQTT commands are being sent.
Though I do not want to say much about python programming (I am a beginner in that myself), just a few tips in case you start changing this file: python programs can be started by typing
python filename.py
They can also be made executable by the command chmod +x filename.py
after that they can be run with:
./filename.py
However, in that case you need to tell the ‘executer’ where to find the python interpreter.
you do that by adding #! /usr/bin/python
as the first line of your python file.
If you do not, the program will choke on the first ‘From’ and throw a can't read /var/mail/
error
Just a final remark: I am no real fan of the DHT11 or DHT22. They are not very accurate and prone to failure. If not for the Humidity I would prefer a DS18B20 and currently am looking into the BME280 as that measures humidity as well (Edit: after a short discussion on failing DHT sensors, it could very well be that they do a lot better/last longer in relatively dry climates)
Added: Errors
If you install the above on an openhabian system you are unlikely to get any errors. However, I tested it on a freshly installed raspian Stretch system (not sure anymore if it was ‘Lite’) and I came upon the following Errors that were easy to fix:
Error
-bash: git: command not found
Solution
sudo apt-get install git
Error
File “setup.py”, line 5, in
from setuptools import setup, find_packages
ImportError: No module named setuptools
Solution
sudo apt-get install python-setuptools
Error
no module named requests
Solution
sudo pip install requests
Error
No pip
Solution
apt install python-pip #python 2
Error
Append error, logging in again: [Errno -2] Name or service not known
Solution
Most likely you are running the file from another raspbery where your MQTT server is not located. Make sure you alter the variable ‘servername’ in the mqtt.dhtsensor.py file into th eservername or ip number where your mqtt broker is situated. Also make sure you do not have two clients running with the same name.
Alternative
If you want to do it completely different,look here.