Exercise 3 – Creating an IoT solution using an Azure IoT Hub
This section will look at the steps to create an IoT hub that can receive sensor data from a Raspberry Pi (Sensor-Enabled Device) simulator.
This could be the basis for any simple data collection solution from a sensor on any device.
In the following subsections, the process of creating a solution has been segregated into tasks for ease of understanding.
Task – Accessing the Azure portal
- Log into the Azure portal: https://portal.azure.com. Alternatively, you can use the Azure desktop app: https://portal.azure.com/App/Download.
Task – Creating an IoT hub
- In the search bar, type iot hub; click on IoT Hub from the results list.
- From the IoT Hub blade, click on the + Create button on the top toolbar.
- From the Basics tab, set the Subscription and Resource group settings as required.
- Set the region closest to you.
- Enter a unique name for your IoT hub.
- Click Next: Networking; leave the default settings as-is.
- Click Next: Management; set Pricing and set Scale tier to F1: Free tier.
- Click Review + create.
- On the Review + create tab, review your settings; you may go back to the previous tabs and make any edits if required. Once you have confirmed your settings, click Create.
- When the deployment is complete, you will receive a notification stating that the deployment succeeded. Now, click on Go to resource from the Deployment blade or navigate to the Azure IoT Hub instance.
Task – Creating an IoT device
- From the IoT Hub blade, go to the Explorers section from the left menu and click IoT devices.
- From the top toolbar, click + New.
- Provide an ID for your device and click Save:
Figure 5.37 – Create a device
- Click refresh from the top toolbar if your device does not appear. Then, click your device to open its respective blade.
- From the Device blade, copy the Primary Connection String value:
Tip
This will be used in the next task to authenticate to the Raspberry Pi simulator.
Figure 5.38 – IoT device connection string
Task – Configuring the Raspberry Pi simulator
- From a browser, enter the following URL: https://aka.ms/RaspPi.
- Once the simulator has loaded, click on the Next button shown as Step 1 on the Simulator Wizard screen.
- As shown on the Simulator Wizard screen, as Step 2, review the information shown regarding copying the device connection string from the Azure portal IoT hub you created in the previous task. Then, click the Next button.
- As shown on the Simulator Wizard screen, as Step 3, review the information shown regarding placing the placeholder on line 15 with the Azure IoT Hub device connection string, as well as the instruction to click Run or type npm start in the console window to run the application. Then, click the Got it button.
- From the top-right code area, locate line 15, which shows const connectionString =.
- Replace the following entry with the Azure IoT Hub primary key connection string you copied in the previous task; that is, ‘[Your IoT hub device connection string]’:
Figure 5.39 – Raspberry Pi simulator
Task – Testing the solution
- Click Run or type npm start to run the application from the bottom-right console area.
- You will now see messages stating that data is being sent from the device to the Azure IoT Hub; the red LED will also flash:
Figure 5.40 – Raspberry Pi simulator
- Click Stop; you will see a message stating that the sample has stopped. The red LED will also stop flashing.
- From the Azure portal, access the Azure IoT Hub blade. Then, from the Overview section, scroll down to view the messages that have been received:
Figure 5.41 – IoT Hub Usage
In this exercise, we looked at creating an Azure IoT hub and sent it messages from a device with a sensor using a simulator. In the following exercise, we will look at creating an IoT solution using a Bot Service.