This blog is intended to help developers who are having issues connecting to an IoT Hub, or an IoT Central application using one of the Avnet OOB example projects. My personal experience is that I've always been able to connect to my IoT Hub. I've done my best to identify and capture things that I think can go wrong. My list is not comprehensive at all, if you find another debugging tip, please post it in the comments for others to use!
There are three failure points when connecting to an IoT Hub from the Avnet Azure Sphere OOB example application.
- Is the Azure Sphere application properly configured?
- Is the network passing data from the Azure Sphere Device to the IoT Hub and other Azure services?
- Are the Azure resources correctly configured?
- Is the device actually connected to the internet and working properly?
Let’s look at each of these categories and determine how to prove to ourselves that each one is working correctly.
Is the application properly configured?
There are three code changes required for the Avnet Azure Sphere OOB application to connect to an IoT Hub, or an IoT Central application (which is using an IoT Hub under the covers)
- connection_strings.h
- Verify that you added a connection string to the header file
- Verify that you’re using the correct connection string
- IoT Hub: Copy the "primary connection string" from the IoT Hub --> <your device> --> primary connection string
- IoT Central: Generate a connection string using the method documented here: https://docs.microsoft.com/en-us/azure/iot-central/howto-generate-connection-string
- build_options.h
- Modify the header file to include only code required for either IOT_CENTRAL_APPLICATION or IOT_HUB_APPLICATION
- app_manifest.json
- Verify that the hostname for the IoT Hub has been added to the “AllowedConnections” entry. This entry tells the OS to allow the application to send traffic to all FQDNs/IP Address' in the list.
- Verify that the hostname is correct!
Is the network passing data from the Azure Sphere Device to the IoT Hub and other Azure services?
This is the tricky part to validate. If we could send a ping from the Azure Sphere device to the IoT Hub that would be great. But, we don’t have a shell on the Azure Sphere device to send a ping, and even if we could, the IoT Hub does not respond to pings. So what can we do to validate that we have a clear network path from the Azure Sphere device to the IoT Hub?
The first thing we need to do is verify that our Azure Sphere device is connected to a Wi-Fi network. From the Azure Sphere command line tool execute the following command.
- azsphere device wifi show-status
If the device is connected then you’ll see a status printed out similar to the graphic below
The network requirements for Azure Sphere are documented here: https://docs.microsoft.com/en-us/azure-sphere/network/ports-protocols-domains. If your ISP or internet connection blocks any of these ports you’re connection likely won’t work. We can use Windows Powershell and the tnc (Test NetConnection) utility to probe ports on our IoT Hub and other azure resources to verify if they are reachable. If your network blocks any of these ports, try using a different network, for example the network on your mobile phone’s wifi hotspot. For the test below your PC should be connected to the same wifi network as your Azure Sphere device.
A successful tnc execution will look like the graphic below:
- Launch windows power shell
- Copy paste in each tnc * line below to check network connectivity. Note that if the command shows <Your IoT Hub Hostname> you need to replace that text with your IoT Hub hostname
- Device provisioning and communication with IoT Hub
- tnc <Your IoT Hub Hostname> -port 8883
- Device provisioning and communication with IoT Hub
- tnc <Your IoT Hub Hostname> -port 443
- Communication with web services, network time (NTP) server, and Azure Sphere Security service
- tnc prod.device.core.sphere.azure.net -port 443
- Internet connection checks, certificate file downloads, and similar tasks
- tnc www.msftconnecttest.com -port 80
- Device provisioning and communication with IoT Hub
Are the Azure resources correctly configured?
We can use a simulated device running in a web browser to connect to the device we provisioned on the IoT Hub (or IoT Central). While this does not validate that we can send data from the actual Azure Sphere device, it will confirm if we can connect to our IoT Hub and that the IoT Hub is correctly configured. If the web browser and the Azure Sphere device are on the same network, then we can assume that the data path from our local network to the IoT Hub is good.
There is a Raspberry Pi simulator that will connect to the IoT device on your IoT Hub.
- Open the simulator: online Raspberry Pi simulator
- Using your IoT device connection string modify the source code at line #15
- Note this connection string can be from your IoT Hub --> IoT device, or the connection string you generated for your IoT Central application
- Click on the run link under the source code
- You should see the device send data to the IoT Hub
Is the device actually connected to the internet and working properly?
One way to determine if your device is working properly is to run one of the Microsoft Azure Sphere examples called "HTTPS_Curl_Easy.sln" that can be found here: https://github.com/Azure/azure-sphere-samples/tree/master/Samples/HTTPS/HTTPS_Curl_Easy . This application demonstrates how to use the cURL "easy" API with Azure Sphere over a secure HTTPS connection. The application periodically downloads the index web page at example.com.
A working system will display output similar to the graphic below . . .
A non-working system will display output similar to the graphic below. Note, I turned off my cable modem and left my wifi router on-line to capture this state. The application just hung waiting for a connection.
I hope this you blog helps you find issues with the OOB application connecting to your IoT Hub. Please share things that you find by commenting on this blog. Hopefully we can help others get past similar roadblocks!
Top Comments