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):

  1. Gnd
  2. Data
  3. Vcc

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.

  1. Clone the Adafruit DHT11 library from Github:
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
  1. Navigate to the directory where you have cloned the Adafruit library:
cd Adafruit_Python_DHT
  1. Install the necessary packages:
sudo apt-get install build-essential python-dev
  1. Install the Adafruit package:
sudo python setup.py install

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:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
import sys
import Adafruit_DHT
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

if True:

    humidity, temperature = Adafruit_DHT.read_retry(11, 4)

    print 'Temp: {0:0.1f} C  Humidity: {1:0.1f} %'.format(temperature, humidity)

    fromaddr = "from@domain.com"
    toaddr = "to@domain.com"
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "Room temperature"

    body = """
    Hello,
    """ + "\nCurrent temperature: " + str(temperature) + "°C\nHumidity: " + str(humidity) + " %\n\nBest,\nRasPi TempBot"
    msg.attach(MIMEText(body, 'plain'))

    server = smtplib.SMTP('smtp.domain.com', 25)
    server.starttls()
    server.login(fromaddr, "password")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()
    print("Message sent.")

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:

# m h  dom mon dow   command
0 9 * * *  python /home/pi/dht11/temperature_email.py
0 16 * * *  python /home/pi/dht11/temperature_email.py

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.

Marko Marinković

Marko Marinković

My name is Marko Marinkovic. I've been a professional technical writer for 3 years.