Home automation

To complete home automation, we will use our Raspberry Pi Zero W board and a DHT11 temperature and humidity sensor. The DHT11 sensor is able to test both temperature and humidity. It is pretty easy to set it up and we will do this using a library. First of all, let's start connecting things together. The DHT11 sensor consists of only three pins, one for the ground, one for the 5V and the last one for the signal. We have to carefully connect them to the appropriate pins in our Raspberry Pi Zero W board. In the following image, you can find the pinout of the board:

If this project is brand new and not an add-on, all pins should be available; so, connect the ground pin of the sensor to pin 9. Next, connect the 5V pin of the sensor to pin 2, and finally, connect the signal pin of the sensor to pin 7. It is important to be connected to pin 7, since we will later bind this pin with our Python code.

The following is an image of the temperature and humidity sensor that we used:

First of all, update your Pi board with the latest distribution:

sudo apt-get update  

Then, if git is not installed in your Pi board, install it so that we can later clone the appropriate repository from GitHub:

sudo apt-get install git-core  

Next, you have to install the Adafruit DHT11 library. We will use this library to connect our Pi sensor with the Python code and read the temperature and humidity:

git clone https://github.com/adafruit/Adafruit_Python_DHT.git  

Change your current directory to the directory you just downloaded:

cd Adafruit_Python_DHT  

To write our code and end this part, if you don't have them installed, install all the necessary libraries:

sudo apt-get install build-essential python-dev  

Finally, install the library with the following code:

sudo python setup.py install  

Now, we should be ready to run our code and output the temperature and humidity. If vim is not installed in your Pi run, you can skip this step and use your favorite editors, such as nano or GUI gedit:

sudo apt-get install vim
  

Once it is installed, open the vim editor by typing (you can also skip this step):

vim  

After pressing i to go into insert mode, type the following code:

#!/usr/bin/python 
import sys 
import Adafruit_DHT 
import time 
 
while True: 
   humidity, temperature = Adafruit_DHT.read_retry(11, 4) 
   print "Humidity: " + str(humidity) 
   print "Temperature: " + str(temperature) + "
" 
   time.sleep(1) 

The code is actually pretty simple; we have created a while loop, which means that the block inside it will run forever; then, you call the Adafruit_DHT.read_retry(11, 4) function to return the humidity and temperature at pin 7 that we have attached to our sensor. Lastly, we print the data and sleep for one second.

To close and save the file, press Esc, then type :wq! example.py, and hit Enter. You should see a file named example.py. For more information, take a look at http://vim.wikia.com/wiki/Saving_a_file. To run the code, just type:

python example.py  

You should see an output, as follows:

pi@raspberrypi:~/ch5-temp-project$ python temphum.py  
Humidity: 91.0 
Temperature: 25.0 
 
Humidity: 91.0 
Temperature: 26.0 
 
Humidity: 91.0 
Temperature: 25.0 

Now, it should be easy to create an if statement inside the Python code, and check if the temperature is over 25 degrees Celsius, then execute and call the slack code that we have developed before. You can easily add more automation here and make your bot smarter and able to understand more things in your environment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.22.107