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
Install DHT22 libraries to verify the hardware connections.
- To do so, connect to Raspberry Pi through putty (from Windows machine) or ssh ( linux or mac)
- 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
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.
Verify the message count in Azure.
Image and video of the final prototype.
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.
First thank you!
Nevertheless I am not getting the values from my DHT11 Sensor:
“IoTHubClient sending 3 messages
{“deviceId”: “Raspberry Pi – Python”,”temperature”: 0.000000,”humidity”: 0.000000}
IoTHubClient.send_event_async accepted message [3] for transmission to IoT Hub.
In config.py I have adjusted the GPIO_PIN_ADDRESS from 24 to 2 (because if I run sudo ./AdafruitDHT.py 11 2 I get the values)
Can you help me on this?
I assume you have used GPIO pin 2. My suggestion is to use some other GPIO pin like GPIO pin 17 or 27 or 23. GPIO pin 2 and 3 is i2c pin.
I followed this tutorial till the end, this app.py is written for bme280 sensor. How to change to be use for dht22 sensor??
Update line 15 with dht22 sensor library file name.
Howdy. I have study the instructions here and almost every other set of instructions. I create my Azure iot hub account. I wire up my pi. I can monitor the end point via vs code so I know data is going to azure. I CANNOT find anywhere in Azure that fpage with the graph on. Can you pelease tell me where that comes from? Thank you
The layout of the Azure portal has slightly changed. Search for the IOTHUB that you created and select it. Then select overview option.You will get the graphs and other details.You can also view activity logs and see the data coming.