Stream Data using an Azure IoT Hub
Tags: beginners, devops, azure, iothub
Published: 2024-01-05 16:59:44

Suppose you just bought a new car for your Uber business and will like to pick up signals from your new Uber car around the city to ensure it is not driven outside a predetermined zone, Azure IoT hub will be a great tool to this for you. 

But first, what is an **Azure IoT hub**?

An IoT (**Internet Of Things**) is a network of physical devices that connect to and exchange data with other devices and services over the Internet or other network. Azure IoT Hub is a managed service hosted in the cloud that acts as a central message hub for communication between an IoT application and its attached devices. You can connect millions of devices and their backend solutions reliably and securely. Just to note here too that almost any device can be connected to an IoT hub.


In today's exercise we will be configuring an Azure IoT hub to authenticate a connection to an IoT device using **Raspberry Pi simulator** found online.


**Step 1: Create an IoT Hub**

Log in to https://portal.azure.com/. Once signed in, type iot hub in the search bar and open


![Img1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/apm9dg6d4ufua49mp2t6.png)

Next click on **Create**

![Img2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swieejgn78hy68m09cy1.png)

Create a name for resource group and IoT hub name which should be globally unique, also for the rest of the instance details do not change the default settings.

![Img3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otwz480ap8wewbqj0psw.png)

Once done, click on **Review + create**. Then click **Create**.


![Img4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spfxlkfqvchl8ywe4shb.png)


![Img5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arhrvqwsw06yfko1w0on.png)

Azure IoT Hub has been deployed successfully!
 

![Img6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7a3ox5ihvhqszjgrk3r.png)
 


 **Step 2: Adding IoT device**

Once the deployment is complete, click **Go to resource**. Then click **Devices** to enable us add an IoT device.


![Img6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0s2fqiiw46um0os3ynjb.png)

Next click on **Add Device**, add the name of the device in the **Device ID** - let's call it **lawrieiot**, any name works provided it is globally unique


![Img7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5ot6m1nzh4g7tcc80wj.png)

Once that is filled click **Save**

![Img8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0mihye1m2pvuq6hezpq.png)

Great! the **IoT device** is set up successfully


![Img9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pynlvukz48yedo70pe88.png)



**Step 3:Testing the device using a Raspberry Pi Simulator**

Next we will click on the new device **lawrieiot**, locate the primary connection string and copy all the code or just click the **copy to clipboard** icon circled below

![Img10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88x9hyegh5695327fz2u.png)

Open a new tab in your web browser and paste this link https://aka.ms/RaspPi. Next locate the placeholder in line 15 and replace it with the primary connection string copied earlier.


![Img11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/llxvpyug6dylh9l0nqow.png)


Select **Run** in the integrated console window to run the application.


You should see the following outcome that shows the sensor data and messages are sent to your IoT hub


![Img12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvtqjs4lb4lw090chn9y.png)

The diode will be blinking and messages sent to the IoT hub


![Img13](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thdf5us4ssl49imxnvgr.png)


To access the messages, go back to IoT Hub Overview page, scroll down to **Show data for last:** and switch to **1 Hour**. Refresh from time to time to have the most recent number of messages received


![Img14](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23npishdk3g0vkhei2a9.png)

Good news! We just set up an Azure IoT Hub to collect sensor data from an IoT device.

NB: Ensure to remove the resource group to avoid additional costs.

Thanks for reading!!!