In this article I will show you how to create an IoT Hub and connect your IoT Device. This article is part of the IoT Virtual Bootcamp – Predict Temperature using Azure IoT & Raspberry Pi3.
- Execution Time: 30 minutes.
- Required Hardware:
- Windows 10 PC
- IoT Hardware kit: https://www.adafruit.com/product/3605 or similar hardware.
- Access to a WiFi network (without a captive portal aka web page login)
- Required Operating System:
- Windows 10
- Other Requirements:
- Azure Subscription
- Required Software:
- IoT Device Explorer, download : https://github.com/Azure/azure-iot-sdk-csharp/releases/download/2017-1023/SetupDeviceExplorer.msi
Lets Create an IoT Hub and connect your IoT Device!
- Verify you properly setup your IoT Device – please follow this instructions: https://wikiazure.com/wp-content/uploads/2018/03/Lab1-HuzzahDevice.pdf
- Now you will create an IoT Hub in the Azure Portal. Go to the Azure Portal, click on Create new and type “iot hub”:
- Now provide the following parameters to create your IoT Hub:
4. Now verify the IoT Hub is successfully created:
5. Now go the Shared Access Policies
6. Then click Shared Access Policies, select “iothubowner” and enable “registry write” and copy the Shared access keys:
7. Now go back to your desktop and open the Device Explorer Twin, If not already installed, install (https://github.com/Azure/azure-iot-sdkcsharp/releases/download/2017-10-23/SetupDeviceExplorer.msi ) and run the Device Explorer:
8. Now select the Management label and Click on Create Device, provide a name for your IoT Device and then paste the Shared Access Credentials you previously copied:
You will see a notification when the device is successfully created:
9. Now in the Management label, right click on the device you just created and click on “Copy connection string for the selected device”
10. Now Launch Arduino. You can download it from here:
Now add the Microsoft IoT and Arduino libraries by clicking Sketch -> Include Library -> Manage Libraries and search for “AzureIoT”. Install the library AzureIoTHub by Arduino, the AzureIoTProtocol_MQTT by Microsoft and AzureIoTUtility by Microsoft, as shown below.
11. Starting with a new sketch and copy the following code: Download code
Now modify the ssid, pass, and connectionString on lines 18 – 20 as shown below and click the “Compile and Upload” button:
String ssid = "iot"; // your network SSID (name) String pass = "microsoft"; // your network password (use for WPA, or use as key for WEP) static const char* connectionString = "HostName=kevinsayiotdemo1.azure-devices.net;DeviceId=kevinsay;SharedAccessKey=GDTkiREMOVEDVC458="; #define DHTPIN 2 // what digital pin we're connected to #define DHTTYPE DHT22 // DHT11 or DHT22
12. Now go back to the Device Explorer, Click Message To Device. Type a message add any desired properties and click send, as shown below:
Tried to take a closer picture of my device in case you want to validate your sensor connectivity:
Using Serial Monitor, you should be able to look the Humidity Percentage, temperature in °C and °F:
You should also be able to monitor the messages through Serial Monitor as shown below:
This completes the Lab on how to Create an IoT Hub and connect your IoT Device. Please refer to the IoT Virtual Bootcamp – Predict Temperature using Azure IoT & Raspberry Pi3.