Home temperature bot with Raspberry Pi and DHT11 sensor
This is my very first attempt to make use of my Raspberry Pi and put it to a good use. I ordered a package of 16 different sensors, but DHT11 became my first choice as it is simple to connect and use. At the same time, as I live on the fifth floor of an apartment building with no plants, I wasn’t really able to find a suitable use for the soil moisture sensor, for example.
Anyway, as I initially found out there are versions of the DHT11 sensor with 3 and 4 pins and the pin layout might be different, depending on the version. Mine has 3 pins, laid out in the following order (when looking at the front of the PCB board, left to right):
The following diagram shows how DHT11 is connected to Raspberry Pi. I connected the sensor directly using jumper wires with Dupont connectors, without a breadboard.
Once you have connected the sensor, there is an LED on it that should turn on if the sensor is properly powered from the Raspberry Pi GPIO pins. The next step is to set up your Raspberry Pi to be able to access temperature and humidity information through SSH. The following steps are based on instructions originally found here, using Python and the Adafruit DHT11 library.
- Clone the Adafruit DHT11 library from Github:
- Navigate to the directory where you have cloned the Adafruit library:
- Install the necessary packages:
- Install the Adafruit package:
Now that this is done, we’re ready to implement the Python script that will actually be used to output the code. I created two versions of the script, one that outputs temperature and humidity values directly to the terminal, and the other one that can send this information via email. The latter version is especially convenient for automation - I added a Cron job on Raspberry Pi that executes every day at 9 AM and 4 PM. I use this as a reminder to turn the heating on if the temperature is too low.
The Python script that is used to get the temperature and humidity info, pack those and send them via email:
Make sure to replace email addresses, SMTP server address and credentials with the ones that will work for you.
Finally, use the
crontab -e command to edit the crontab file and set the desired times to run the script. My file looks like this:
As stated above, this setup will automatically run the script every day at 9 AM and 4 PM, sending you some useful temperature and humidity info.