Internet of Things refers to a collection of physical devices that are connected to the internet. Typically for collecting data, sharing data, controlling and monitoring devices. Microsoft has developed a number of managed IoT services that provide users with an endpoint to send data, manage and output data to other services.

This blog will be specifically covering the Azure IoT Hub, which is a managed service that acts as a central message hub for bi-directional communication between IoT application and devices it manages.

Raspberry Pi IoT Sensor Demo

For the purpose of a demo, I have created a simple application in Python that sends a message of the current temperature and humidity to an Azure IoT Hub. This simple lab requires the following:

  • Raspberry Pi - I used a Raspberry Pi 3 for this. However, any of the Pi’s should work for this.
  • DHT11 Temperature and Humidity Sensor - Can be purchased from here: DHT11
  • 3 pin sensor cable - 3 pin sensor cable
  • SD Card for Raspberry Pi - I used the 32bit Raspberry Pi OS Operating system images – Raspberry Pi
  • Active Azure Subscription
  • Azure CLI - Cloud Shell, Windows Install or WSL Install

Hardware Setup

The sensor I am using is a WaveShare DHT11 3 pin sensor, the pin layout is as follows:

Pin NumbersSymbolDescriptionPi Pin
1DOUTCommunication Port11 (GPIO17)
2GRNGround6 or 9
3VCCPositive Power Supply (3.3v-5.5v)1

Software Setup (Raspberry PI)

To start, update the package list and install the following python libraries:

sudo apt-get update
sudo apt-get install build-essential python-dev

Clone the Adafruit Library from their repository:

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

Then install the Adafruit library for Python 2 and Python 3:

sudo python setup.py install
sudo python3 setup.py install

Now test the sensor is working correctly, by running the below command:

cd examples
python AdafruitDHT.py 11 17

This should output Temp={Temperature reading} Humidity={Humidity Reading}

Once the sensor reading has been verified, we will require additional Python Libraries for the Azure IoT Hub:

sudo pip3 install azure-iot-device  
sudo pip3 install azure-iot-hub  
sudo pip3 install azure-iothub-service-client  
sudo pip3 install azure-iothub-device-client  

Azure IoT Hub Deployment

Within Azure CLI run the following commands:

az extension add --name azure-iot

az group create -n IOTHUBRG -l westeurope

az iot hub create --resource-group IOTHUBRG --name raspberrypiblogtemp --location westeurope

az iot hub device-identity create -n raspberrypiblogtemp -d RaspberryPi --ee

az iot hub device-identity connection-string show --hub-name raspberrypiblogtemp --device-id RaspberryPi

Make a note of the connection string (output of last command).

Sensor Application

On the Raspberry Pi, Clone my blog repo:

git clone https://github.com/ryanmstephens/blog.git

cd blog

cd IoT

cd temperature_sensor/

nano IOTHub.py

Modify the CONNECTION_STRING = “”, with the Connection String from the previous step:


import random  
import Adafruit_DHT
import time
from azure.iot.device import IoTHubDeviceClient, Message  
sensor = Adafruit_DHT.DHT11
pin = 17


CONNECTION_STRING = "" 

MSG = '{{"temperature": {temperature},"humidity": {humidity}}}'

 
while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    def iothub_client_init():  
        client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)  
        return client  
    def iothub_client_telemetry_sample_run():  
        try:  
            client = iothub_client_init()  
            print ( "Sending data to IoT Hub, press Ctrl-C to exit" )  
            while True:  
                msg_txt_formatted = MSG.format(temperature=temperature, humidity=humidity)  
                message = Message(msg_txt_formatted)  
                print( "Sending message: {}".format(message) )  
                client.send_message(message)  
                print ( "Message successfully sent" )  
                time.sleep(3)
                  
        except KeyboardInterrupt:  
            print ( "IoTHubClient stopped" )  
    if __name__ == '__main__':  
        print ( "Press Ctrl-C to exit" )  
        iothub_client_telemetry_sample_run()
        

Exit and save from nano, then run the following command:

python3 IOTHub.py

Example output:

Run the following command to monitor the messages being received by the IoT Hub:

az iot hub monitor-events --hub-name raspberrypiblogtemp --device-id RaspberryPi

I hope you find this introduction to the world IoT devices and Azure IoT services informative. If you have any questions or ideas for any future IoT posts, please reach out in the comments.