Implementation of Azure IoT Hub
Tags: webdev, beginners, iot, tutorial
Published: 2022-06-16 04:09:34

**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. You can connect millions of devices and their backend solutions reliably and securely. 

With these few steps, we will configure a new Azure IoT Hub in Azure Portal, and then authenticate a connection to an IoT device using the online Raspberry Pi device simulator.

## First Step: create an IOT Hub

- Sign in to Azure portal

- Search for and Select **IOT HUB**

- On the Iot Hub page click **+ Create** 

- On the **Basics** tab of the IoT hub page, fill in the fields with the following details
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/il1jfvu7j3gjgwgu9p56.png)

- Go to the **Management** tab, and use the dropdown to set the **Pricing and scale tier** to **S1: Standard tier**.

- Click the **Review + create** button and Select **Create** to begin creating your new Azure IoT Hub instance.

- Wait until the Azure IoT Hub instance is **deployed**.


## Second step: Add an IoT device

- When the Deployment is done, click **Go to resources**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p716hk0xkzf3e8v9h6hx.png)

- To add a new IoT device, scroll down to the **Device management** section and click **Devices**. Then, click **+ Add Device**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bs9oip84w3stq1txqjbi.png)

- Provide a name for your new IoT device, **Azdevice**, and click the **Save** button. This will create a new IoT device identity in your Azure IoT Hub.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55ebpqg4sgsg6z6brp4o.png) 

- If you do not see your new **device**, Refresh the IoT Devices page.

- Select **Azdevice** and copy **the Primary Connection String value.** You will use this key in the next task to authenticate a connection to the Raspberry Pi simulator.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dqkpc85nqi91leqw6n0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/co8fwampv23m4j7hz1go.png)
 

## Last Step: Test the device using a Raspberry Pi Simulator

we will test our device using the Raspberry Pi Simulator.

- Open a new tab in the web browser and type this shortcut link (https://aka.ms/RaspPi.) It will take you to a Raspberry Pi Simulator site. If you have time, read about the Raspberry Pi simulator. When done select “X” to close the pop-up window

- In the code area on the right side, locate the line with ‘const connectionString =’. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pz1itnk4ejt1dah4f24.png) 

- Replace it with the connection string you copied from the Azure portal. **Note** that the connection sting includes the DeviceId **(Azdevice)** and SharedAccessKey entries.  
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzi177gspu6roxcq7cpe.png) 

- Click **Run** (below the code area) to run the application. The console output should show the sensor data and messages that are sent from the Raspberry Pi simulator to your Azure IoT Hub. Data and messages are sent each time the Raspberry Pi simulator LED flashes.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbzfcrvrs9j0rq6r8fdx.png)

- Click **Stop** to stop sending data.
 

- Return to the Azure portal.

- Switch the IoT Hub **Overview** page 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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/md23ypiel7jgn4xurri9.png)

Congratulations! You have set up Azure IoT Hub to collect sensor data from an IoT device.

**Note:** To avoid additional costs, you can optionally remove this resource group. Search for resource groups, click your resource group, and then click **Delete resource group**. Verify the name of the resource group and then click **Delete**. 

Thank you.