Message Azure from an Internet of Things Device

Connected appliances, baby monitors, smart smoke detectors, and home security systems are just a few examples of the burgeoning “Internet of Things”, usually abbreviated just “IoT”. Of course, there must be centralized servers to collect the data from these devices, make some sense of it, and put that information to good use. Not surprisingly, Microsoft wants those servers to be running on Azure.

We shall take a look at the tools Microsoft Azure provides for deploying and managing IoT devices, not to mention collecting and analyzing the data. There are two distinct tasks we need to consider.

1) Sending data collected by an IoT device for storage and analysis in Azure servers. This is the easy one. Virtually any device can be programmed to upload data to Azure, generally with very little effort.

2) Managing thousands or even millions of IoT devices from Azure. A bit harder. If we were a manufacturer and we wanted to use Azure to monitor machines on the factory floor, it wouldn’t be too bad to have to craft some custom code to do so. But what if we wanted to sell millions of IoT devices, say perhaps smart fire alarms. We want our customers to be able to turn the device on and then forget about it until the battery needs changing. Our Azure servers will have to discover, configure, and track these devices automatically.

In this blog we will start by considering the simpler case. We will use for our example a Raspberry Pi. The Raspberry Pi serves as a good example for two reasons. It can be programmed to perform a wide variety of different tasks and collect information from a wide variety of different sensors. Secondly, it is a very popular device, and any reader who wishes to experiment with Azure IoT will have no difficulty obtaining one and learning to employ its capabilities.

Creating an Azure IoT Hub

There are no tricks or traps creating an IoT hub. Choose “Internet of Things” the IoT Hub in the New Resource page.

Once the new IoT hub has been created, go to its resource page and select IoT devices. If you are doing this for the first time, you will select “New” at the top of the page. (Of course, if you have sold millions of devices to your customers, you will probably not want to register devices in this way.)

Once you have created your new device, open the page for that device. You will need the connection string so that the physical device can speak to the server using the device definition you have just created.

Some Simple Python Code

Of course, you could create a physical device using many different hardware platforms, and you could craft your device code using node.js, C#, or a variety of other development environments. Our example will use Python code running on the free RaspiOS operating system provided by the Raspberry Pi folks themselves. In the most recent RaspiOS Python 2.7 is the default, but Python 3.7.3 is installed. This is what we shall use. You must use at least Python 3.7.

Microsoft provides a Python module specifically for communicating with Azure IoT. This can be easily installed using pip.

Microsoft provides sample code for different programming languages and platforms. Here is simple Python using the azure-iot-device module.

In a real deployment, a message from an IoT device will trigger some sort of activity or function to record the data and respond accordingly. For our example, we will simply use an Azure commandline to demonstrate that a message has indeed been received.

Note that once a received message includes the “origin” of the message, which is nothing more than the registered Azure Device ID of the physical Raspberry Pi that has sent the message.


Virtually all the Azure APIs require only relatively simple programming from the client side, so it is hardly surprising that the Azure Iot Hub is no different. In an upcoming blog we will look at a more complex scenario, the configuration of an IoT device from the Azure hub, requiring no custom programming of the IoT device itself.

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.