07 - Implement an Azure IoT Hub (10 min)
In this walkthrough, 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. Sensor data and messages are passed from the Raspberry Pi simulator to your Azure IoT Hub, and you view metrics for the messaging activity in Azure Portal.
Task 1: Create an IoT hub
In this task, we will create an IoT hub.
-
Sign in to the Azure portal.
-
From the All services blade, search for and select IoT Hub and then click + Add, + Create, + New.
-
On the Basics tab of the IoT hub blade, fill in the fields with the following details (replace xxxx in the name of the storage account with letters and digits such that the name is globally unique):
Settings Value Subscription Keep default supplied Resource Group Create new resource group Region East US IoT Hub Name my-hub-groupxxxxx Note - Remember to change the xxxxx so that it makes a unique IoT Hub Name.
-
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.
-
Click the Create button to begin creating your new Azure IoT Hub instance.
-
Wait until the Azure IoT Hub instance is deployed.
Task 2: Add an IoT device
In this task, we will add an IoT device to the IoT hub.
-
When the deployment has completed, click Go to resource from the deployment blade. Alternatively, from the All services blade, search for and select IoT Hub and locate your new IoT Hub instance
-
To add a new IoT device, scroll down to the Explorers section and click IoT Devices. Then, click + Add, + Create, + New.
-
Provide a name for your new IoT device, myRaspberryPi, and click the Save button. This will create a new IoT device identity in your Azure IoT Hub.
-
If you do not see your new device, Refresh the IoT Devices page.
-
Select myRaspberryPi 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.
Task 3: Test the device using a Raspberry Pi Simulator
In this task, 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 =’. Replace it with the connection string you copied from the Azure portal. Note that the connection sting includes the DeviceId (myRaspberryPi) and SharedAccessKey entries.
-
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.
-
Click Stop to stop sending data.
-
Return to the Azure portal.
-
Switch 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.
Congratulations! You have set up Azure IoT Hub to collect sensor data from an IoT device.
Note: To avoid additional costs, you can 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. Monitor the Notifications to see how the delete is proceeding.