CREATING AN IOT HUB IN AZURE PORTAL
Tags: azure, tutorial, iot, raspberrypi
Published: 2024-06-26 16:48:30

The aim of this walkthrough is to create an IoT hub in Azure portal and configure the hub to authenticate a connection to an IoT device using the Raspberry Pi device simulator.

## **PREREQUISITE**

- Working computer
- Internet connection
- Microsoft Azure account + active subscription

## **PROCEDURE**

### **LOCATE THE IOT HUB SERVICE**
Open the Azure portal and type “IoT” in the search bar at the top. Click on “IoT Hub” under services  as seen in the image below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/63taonmw5pbhw2bms5v8.jpeg)


### **CREATE AN IOT HUB**
On the IoT Hub service webpage that loads, click on the “Create” or “Create IoT hub” button as you deem fit.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/js6f9q2vludwmvup53au.jpeg)

You will be directed to the “Basics” page.

The first part of the “Basics” page is the “Project details” section where you are asked to select the subscription and resource group under which you want to create the IoT hub. 
_PS: In case you want a new resource group, creating a resource group just requires you to provide a name in the input box provided after clicking on “Create new” beneath the “Resource group” input box._
The next section is “Instance details” where you can input an IoT hub name of choice, select a region, tier and daily message limit as required.
Afterwards, click on the “Review + create” button. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p32yy8lzedsfd398635h.jpeg)

A page as shown should appear showing the pricing for the IoT hub specifications selected and the details of the hub.
Click on the “Create” button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igfly6lhxr57bjh3pt8p.jpeg)

There will be a pop-up at the top right showing the status of the deployment.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89xssk8m9midmppx04nu.jpeg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khge1owzoi5fd55dajdm.jpeg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqt52if66pgeo2csknpl.jpeg)

You will be directed to an IoT hub deployment page which goes through several phases that you might need to be patient for.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y62cw226mldw5h30xh5k.jpeg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oglvz33ui372ajwndh64.jpeg)

When deployment has been completed, click on “Go to resource”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1r6jy83sgz706omw6hp.jpeg)

The IoT hub resource page loads.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0bqzgy4vxhy9bez1gu2.jpeg)

Scroll down to the “Usage” section and take note of “IoT Hub Usage”, which contains messages used and IoT devices—_they should have values of 0 at the moment._

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ggj8mwnk8p6ctig2lcch.jpeg)


### **ADD IOT DEVICE**
On the side menu bar, click on “Device management” and then, “Devices”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bh13fog9pf14oy4p7epf.jpeg)

On the Devices webpage that loads, click on “Add Device”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fk3394rbyx82jg1jalmf.jpeg)

“Create a device” webpage loads, where a “Device ID” can be inputted. Then, click on the “Save” button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0z4dpxnfn5o1cijnpye.jpeg)

When that is completed, a notification is sent and the newly created device is listed on the “Devices” webpage.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9nii4mrl4s42vv3ok4s.jpeg)


### **TEST THE IOT DEVICE**
Open the new device and copy the “Primary connection string”—as it will be used to link the device to Raspberry Pi device simulator.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8750ps74cqovgr5h4fj7.jpeg)

Access the Raspberry Pi device simulator on another webpage using this link: `https://azure-samples.github.io/raspberry-pi-web-simulator/`
Take note of the unblinking LED which symbolises that a connection to a device has not been established.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27pp7o5kfwjh7om0tbno.jpeg)

In the sample code, locate the line that reads “`const connectionString = ‘[Your IoT hub device connection string]’`” (line 15).
Replace the quoted section with the primary connection string that was copied earlier and run the code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1szuuchj07xldlp03aop.jpeg)

The terminal should have alternating lines of “Sending message” and “Message sent to Azure IoT Hub”.
Also, take note of the blinking diode to depict device connection.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2driwg9sgdblka6j9tm1.jpeg)

Heading back to “IoT Hub Usage” on the Azure portal should give new details showing the number of connected IoT devices and a message count that should coincide with the “messageID” in the simulator terminal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q26j0at3fktyxrm0eh25.jpeg)

_That’s all folks!_