Sending Telemetry to Azure IoT Hub

As I promised in my previous article, I will show you how to connect a temperature sensor to a Raspberry Pi, read real telemetry data, and send it to the Azure IoT Hub:

I am using a Raspberry Pi 4, a DHT11 temperature sensor, and a GPIO Extension Board to connect the sensor to the Raspberry. We’ll be using Azure IoT Python SDK enabling connection to the Azure IoT Hub. And we need DHT11 Python library for reading temperature and humidity from DHT11 sensor on Raspberry Pi.

First, we need to create an IoT Hub and to set up a Raspberry Pi. Repeat the steps described in my article “Connect Raspberry Pi 4 to Azure IoT Hub“:

Connect the sensor to Raspberry Pi

The DHT11 sensor can collect temperature and humidity data. Use the following wiring to connect the sensor to GPIO pins:

DHT11 Sensor pinsGPIO pins
Vcc (+)5V (pin 2)
Ground (-)GND (pin 6)
DataGPIO17 (pin 11)

Turn on your Raspberry Pi and connect it to your network.

Run the application

When it is done, clone the azure-iot-blog GitHub repository into a folder in your Raspberry Pi:

git clone

Find the Python application in the raspberry-to-iot-hub/send-telemetry folder:

import asyncio
import time
import board
import RPi.GPIO as GPIO
import dht11
from azure.iot.device import Message
from azure.iot.device.aio import IoTHubDeviceClient


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

async def main():

        # Create instance of the device client
        client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)

        # Initialize GPIO

        # Read data using pin GPIO17
        dhtDevice = dht11.DHT11(pin=17)

        print("Simulated device started. Press Ctrl-C to exit")
        while True:

                result =
                if result.is_valid():
                    temperature = result.temperature
                    humidity = result.humidity

                    data = PAYLOAD.format(temperature=temperature, humidity=humidity)
                    message = Message(data)

                    # Send a message to the IoT hub
                    print(f"Sending message: {message}")
                    await client.send_message(message)
                    print("Message successfully sent")
                    # print("Error: %d" % result.error_code)

                await asyncio.sleep(DELAY)

            except KeyboardInterrupt:
                print("Simulated device stopped")

    except Exception as error:

if __name__ == '__main__':


Make sure you copy-paste the Primary Connection String you saved when you created your IoT device, into the quotes in the Line 9.

Now, open your Raspberry Pi terminal and install Python packages:

pip3 install azure-iot-device
pip3 install asyncio
pip2 install dht11

Run the application:


If everything is configured and connected correctly, the program will be sending sensor data every 5 seconds to your IoT hub:

Open Azure Portal, find your IoT hub, and select Overview in the left navigation menu. You should see number of messages received by your IoT hub from your device:

Stay tuned. I’ll continue the “Azure IoT” series.

Temperature Sensor for Windows 10 IoT Core

I know it is not a rocket science to connect a sensor to your computer and read its measurements. But if you haven’t been doing it before, I will show you a simple experiment with a Raspberry Pi and a temperature sensor.


I used a Raspberry Pi 3 Model B, though Pi 2 would be of use as well. If you already have Windows 10 IoT Core installed on your Raspberry Pi – that’s fine. If not, there is a good guide how to install it: How to install Windows 10 IoT Core on Raspberry Pi 3. The only remark I would add is my advice to format your MicroSD card with an SD Memory Card Formatter before installing any OS on it.


Next, we will need a sensor. I bought a cheap (and not too accurate) Humidity and Temperature Sensor DHT11. It has three pins:

  1. Power supply 3 – 5.5 V DC (+)
  2. Serial data output
  3. Ground (-)

Having everything in hand, connect the sensor to the Raspberry Pi as shown below:


When everything is set up, connected and switched on, open Windows 10 IoT Core Dashboard and connect to your Raspberry Pi (see instructions if something went wrong).

Find your device in the list, right-click on it and select Open in Device Portal. This will launch the Windows Device Portal. The Windows Device Portal lets you manage your device remotely over a network. In order to enable remote display functionality on your Raspberry, choose Remote from the options on the left, and Enable Window IoT Remote Server.

win-core-screenInstall the Windows IoT Remote Client on your workstation and run it. Connect to your Raspberry Pi device through the client. You should see a window like this one on the left.

Now, you can download a source code of the TemperatureSensor project from GitHub:

Open the project in the Visual Studio 2017 and build it. You will find a separate library Sensors.Dht, written by Daniel Porrey. I used his code to read sensor’s data.

Set the solution platform to ARM and the target as Remote Machine in the tool bar:


Open the TemperatureSensor project properties and choose Debug page. Check that Remote machine field contains your Raspberry Pi device name or IP address:


Press F5 to build and deploy the application to your Raspberry Pi. The sensor should start sending temperature and humidity values every second, and the Windows IoT Remote Client will show a screen like this one:



The source code of the Visual Studio project can be found there download