Connect your Azure Sphere device to Azure IoT Central – using Visual Studio 2019

Steps Overview

Note: that the following exercise assumes you are already set up your Azure Sphere Tenant and configured your device to connect to IoT Hub as described in my previous Blog Post.

  1. Download the sample code from Github repository
  2. Set up an Azure IoT Central application
  3. Configure Azure IoT Central to work with your Azure Sphere Tenant
  4. Configure the sample application to work with your Azure IoT Central Application

Detailed Steps

  1. If you have not done so already, download or clone the Azure Sphere Samples from the Github repository
  2. Create an Azure IoT Central trial application. 
    IoT Central Home Page
  3. Under the Build sub-menu pick Custom Apps
    IoT Central Start Page
  4. In the New application page, enter an Application Name and a unique URL.
    IoT Central Create New Application
  5. Pick 7-day free trial (supports 5 devices).
  6.  Open your new application
    IoT Central Dashboard
  7. Click on Device Templates and then click “+New” to add a new template
  8. Select IoT Device and click on “Next: Custom”
    IoT Central - select IoT Device template
  9. Click on “Next: Review” button:
    IoT Central Device Template Review
  10. Then click on Create button
    IoT Central - Device Template - Create
  11. Enter a Name for the new Device Template
    IoT Central - Device Template - enter name
  12. Create a Custom Capability Model
    IoT Central - Create Custom Capability Model
  13. Click on Add Interface and Choose Custom interface
  14. Choose the Interface (in the left hand column) and
  15. Click on Add Capability: enter Temperature, temperature. Choose Capability Type Telemetry and Semantic Type Temperature and Choose Degrees Centigrade for Units. Finally, click Save button.
    IoT Central - Add Capability
  16. Then click Customize in left hand column and click on Down Arrow (right-hand-side) to expand the fields.
    IoT Central - Customize Capability
  17. Now you can enter a Min value,a Max value and decimal places. You can also customize the Color on the chart.
    IoT Central - Enter Capability Custom Details
  18. Click on “+Add Capability” and enter “Button Press” as the name
    IoT Central - Add Button Press Capability
  19. Publish the Device Template to your application
    IoT Central Publish Device Template
  20. Click on Devices (left hand side column), pick your device template from the list, then click +New to create a REAL device (not a simulated device).
    IoT Central - Create New Device
  21. For the device ID enter the Azure Sphere device ID (in lower cases letters with the help of PowerShell)
    > azshpere device show-attached
    > powershell -Command ((azsphere device show-attached)[0] -split ': ')[1].ToLower()

    Azure Sphere Show Attached lower case
  22. Configure Azure IoT Central to work with your Azure Sphere Tenant
    > azsphere tenant download-CA-certificate --output CAcertificate.cer
  23. In IoT Central’s dashboard, go to Administration, click on Device Connection and then X509 Certificates
  24. Under the Primary field – click on the folder icon and choose the .cer file created above (choose *.* in the filter field to see the .cer file)
    IoT Central X509 certificate
  25. Then click the Refresh button to generate a Verification Code
  26. Use the verification code to generate a validation certificate
    > azsphere tenant download-validation-certificate --output ValidationCertification.cer --verificationcode <code>
  27. Go back to IoT Central and click on Verify
  28. When prompted, navigate to the validation certificate that you downloaded in step #26 and select the .cer file.
  29. Click Close. This completes the step and verifies that you own the Azure Sphere Tenant.
  30. Configure the sample application to work with your Azure IoT Central Application
    Start Visual Studio 2019
  31. Choose File-Open-CMake
  32. Choose the CMakeLists.txt file in the sample you downloaded from Github
  33. Open the app_manifest.json file
  34. Get the Tenant ID for your Azure Sphere device and enter it under DeviceAuthentication in app_manifest file:
    > azsphere tenant show-selected
  35. On the Azure Sphere command prompt type:
    > ShowIoTCentralConfig
    This command will provide the following information needed in app_manifest.json:
    a) The IoT hub URL for your Azure IoT Central application
    b) The Azure DPS global endpoint address
  36. Power up your Azure Sphere device and make sure it has network access.
  37. Build and Run your application
    IoT Central Analytics - View Data 3
    1IoT Central Analytics - View Data 1
  38. Press button A a few times and see the events in IoT Central (as diamonds below the chart)
    IoT Central Analytics - view event
  39. You can even view the data points in table format:
    IoT Central Analytics - view table of data

Lessons Learned

  • Much of the Microsoft documentation gets outdated quickly because IoT Central and Azure Sphere are being updated and changed continuously. With some trial and error, I am sure you will be able to figure out the right steps.
  • I faced an issue where the device was not connecting to IoT Central. However, I redid the tenant verification step and it worked again.