ElectronicsIOT

How to use Azure IOTHub – Part 2

Introduction

In the previous part we have seen how to configure Azure for IOTHub. In this post we will cover setting up the Raspberry Pi and connecting it to the sensor and sending the data to Azure IOTHub.

For more details on How to use Azure IOT Hub – Part 1 click here

Setup Raspberry Pi

Assumption – You already have raspberry Pi Installed with Raspian OS.

To know more on how to install the Raspian OS click here.

Enable SSH and I2C:  Go to Preferences -> Raspberry Pi configuration -> Interfaces Tab

Set I2C and SSH to enable.

Alternatively, you can use command line command raspi-config

sudo raspi-config

Refer the screenshot below.

 

Connect the Sensor to Pi

We will be using DHT22 humidity and temperature sensor.

  • Connect Pin 1 of DHT22 to Raspberry pi Pin 1 (3.3v)
  • Connect Pin 2 of DHT22 to Pin 3 (I2C1 SDA)
  • Connect Pin 3 of DHT22 to Ground pin
  • LED Vdd pin to GPIO Pin 24 (pin 18)
  • LED GND to GND pin 20

Note: you can connect your sensor to any GPIO pin. Accordingly update the config file.

Refer the Raspberry Pi Pinout diagram below

rapberry pi 2 pinout 2

Install DHT22 libraries to verify the hardware connections.

  1. To do so, connect to Raspberry Pi through putty (from Windows machine) or ssh ( linux or mac)
  2. Install DHT22 sensor libraries from Adafruit
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential python-dev python-openssl python-pip(get python libraries)
sudo apt-get install git (Install git if not present)
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python setup.py install 

Once the DHT libraries are installed, test the installation and configuration
cd /home/pi/Adafruit_Python_DHT/examples
sudo ./AdafruitDHT.py 22 2

DHT22 example

Now we are going to use one of the examples from Github. Run the following command to clone the sample app.

git clone https://github.com/Azure-Samples/iot-hub-python-raspberrypi-client-app.git
sudo chmod u+x setup.sh
sudo ./setup.sh

Open the config file by running the following commands to update config settings if required. You need to update pin number that you have used to connect the DHT22 sensor.

cd iot-hub-python-raspberrypi-client-app
nano config.py

Run the app with the following command.

python app.py '<your Azure IoT hub device connection string>'

Note: we can run the command in background or add this using cron job.

If you get the following screen with LED blinking then data is being sent to Azure IOT hub and all our connections are perfect.

MessageSentOutput

Verify the message count in Azure.

AzureMetrics

Image and video of the final prototype.

DHT22andRaspberrypi_azure_IOTHub

Trouble shooting

1. un comment following line from /etc/apt/sources.list if you get error in installing python-dev as shown in the screenshot

deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi

 

 

2. If you get stuck at 95% compiling the python sdk, increase the swap space.

3. Make sure to use the latest Raspbian version ( stretch). In the older versions I was facing issues related to gcc versions.

Do let me know if you have any queries.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.