Create an IoT Hub and connect your IoT Device

IoT-Hub-wikiazure

Create an IoT Hub and connect your IoT Device

Dave Rendón Follow
1 Star2 Stars3 Stars4 Stars5 Stars

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.

Pre-requisites.

  • 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!

  1. Verify you properly setup your IoT Device – please follow this instructions: https://wikiazure.com/wp-content/uploads/2018/03/Lab1-HuzzahDevice.pdf
  2. Now you will create an IoT Hub in the Azure Portal. Go to the Azure Portal, click on Create new and type “iot hub”:Create IoT Hub
  3. Now provide the following parameters to create your IoT Hub:

 

IoT Hub Parameters4. Now verify the IoT Hub is successfully created:

 

IoT Hub created

5. Now go the Shared Access Policies

 

IoT 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:

 

IoT Device Explorer Twin - wikiazure

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:

Create IoT Device on Device Explorer Twin - wikiazure

You will see a notification when the device is successfully created:

 

Iot Device created - wikiazure

 

9. Now in the Management label, right click on the device you just created and click on “Copy connection string for the selected device”

 

Device Explorer copy connection - wikiazure

10. Now Launch Arduino. You can download it from here:
https://www.arduino.cc/download_handler.php?f=/arduino-1.8.5-windows.exe .

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.

Add Microsoft IoT library to Arduino - wikiazure

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:

Iot-Message-to-device-wikiazure

sensor-connected-IoT-Device-wikiazure

Tried to take a closer picture of my device in case you want to validate your sensor connectivity:

sensor IoT Device - wikiazure

Using Serial Monitor, you should be able to look the Humidity Percentage, temperature in °C and °F:

Monitoring-Temperature-IoT-Device-wikiazure

You should also be able to monitor the messages through Serial Monitor as shown below:

IoT Device Serial Monitor - wikiazure

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.

Leave a Reply