Implementing an Azure IoT Hub
Tags: azure, beginners, iothub, comment
Published: 2022-05-22 15:17:01

The Internet of Things (IoT) 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.

Here, we will be configuring an Azure IoT Hub in Azure portal and authenticating a connection to an IoT device using online **Raspberry Pi device simulator**. If you have time, you can read more about the Raspberry Pi simulator.

**- Creating an IoT Hub**

1. Sign in to [Azure portal](https://portal.azure.com/)
2. From the search box, type IoT Hub and select **IoT Hub** under **Services**
![IoTHub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfpnn525ua37fh1ahyic.png)
3. On the IoT hub page, **select + Create**
![IoT Hub create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pym6mvp041vm7sl9uus.png)
4. On the **Basics** tab of the IoT hub, fill in the with the following details
![Iothub basics tab field](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48304jjpgambp8n4p8b2.png)
![Iothub basics tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbp2gp356m4ce2pblllk.png)
5. Go to the **Management** tab and use the dropdown to set the **Pricing and scale tier** to **S1:Standard tier**
6. Click the **Review + Create** button and select the **Create** button when validation passes to begin creating your new Azure IoT Hub instance
![Create IoT Hub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgos3b0lkoj1oe1tiifw.png)
7. Wait until the Azure IoT Hub instance is deployed

**- Add an IoT device**

1. When deployment is completed, Click **Go to resource** from the deployment blade
![Iot hub deployment blade](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djc0lspp9bmoifuk8gx5.png)
2. To add a new IoT device, scroll down to the **Device Management** section and select **Devices**. Then Click **+ Add device**
![Add IoT device](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8ll744h2wuct8yodptv.png)
3. Provide a name for your new IoT device, **myRaspberryPi**, and click **Save**. This will create a new IoT device identity in your Azure IoT Hub
![Device ID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ll1uojld00s7y0bqhzy9.png)
4. If you do not see the new device, **Refresh** the IoT devices page.
5. Select **myRaspberryPi** and copy the **Primary Connection String** value. The key will be needed to authenticate a connection to the Raspberry Pi simulator.
![IPrimary connection string](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8q3x4j09k0ws7msy6g57.png)

**- Testing the device using a Raspberry Pi Sumulator**

Open a new tab in the web browser and type or copy this shortcut link **https://aka.ms/RaspPi**. It will take you to a Raspberry Pi Simulator site. When done select '**X**' to close the pop-up window.
2. In the code area on the right side, locate the line with '**const connectionString =**' 
![const connectionString](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvwsta174jznma3f2g3d.png)
3. Replace it with the connection string copied from the Azure portal. Note that the connection sting includes the DeviceId (myRaspberryPi) and SharedAccessKey entries.
![Run sample code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cavanp8cep7zy7oj5k0s.png)
4. Click **Run** to run the application. The console output should show the sensor data and messages that are sent from the Raspberry Pi simulator to the Azure IoT Hub. Data and messages are sent each time the Raspberry Pi simulator LED flashes.
![Console data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1ua696xa2m6bd24qwxd.png)
5. Select **Stop** to stop sending data and return to the Azure portal
6. Switch to the IoT Hub **Overview** blade and scroll down to the **IoT Hub Usage** information to view usage. Change your timeframe in the **show data for last** to see data in the last hour
![IoT Hub Usage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tz0ecbkfqvc2b45j8pe.png)

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

Note: To avoid additional costs, you can optionally remove the resource group. Search for resource groups in the search box, select your resource group and click **Delete resource group**.
















****