Test device connectivity and offline support

Completed

In this exercise, you will monitor events from the ChildIoTDevice are being sent to Azure IoT Hub through the IoTEdgeGateway IoT Edge Transparent Gateway. You will then interrupt connectivity between the IoTEdgeGateway and Azure IoT Hub to see that telemetry is still sent from the child IoT Device to the IoT Edge Gateway. After this step, you will resume connectivity with Azure IoT Hub and monitor that the IoT Edge Gateway resumes sending telemetry to Azure IoT Hub.

Testing device connectivity and offline support

  1. Log in to your Azure portal using your Azure account credentials.

  2. Open the Azure Cloud Shell by clicking the Terminal icon within the top header bar of the Azure portal, and select the Bash shell option.

  3. Run the following command within the Azure Cloud Shell to start monitoring the Events being received by the Azure IoT Hub:

        az iot hub monitor-events --hub-name AZ-220-HUB-_{YOUR-ID}_
    

    Be sure to replace the {Your-ID} placeholder with your unique suffix for our Azure IoT Hub instance.

  4. The az iot hub monitor-events command will start outputting the telemetry from the ChildDevice1 that is getting sent to Azure IoT Hub.

    Keep in mind that the ChildDevice1 simulated device application is configured to send telemetry to the IoTEdgeGateway IoT Edge Transparent Gateway virtual machine, which then is sending the telemetry on to Azure IoT Hub.

        Starting event monitor, use ctrl-c to stop...
        {
            "event": {
                "origin": "ChildDevice1",
                "payload": "{\"temperature\":20.30307372114764,\"humidity\":72.6844747889249}"
            }
        }
        {
            "event": {
                "origin": "ChildDevice1",
                "payload": "{\"temperature\":31.73955729079412,\"humidity\":78.56052768349673}"
            }
        }
    
  5. The next step to test the Offline capabilities is to make the IoTEdgeGateway device go offline. Since the Virtual Machine is running in Azure, the Virtual Machine can be simulated by adding an Outbound rule to the Network security group for the VM.

  6. Within the Azure portal, navigate to the AZ-220-IoTEdgeResources resource group.

  7. In the list of resources, to open the Network Security Group for the AZ220EdgeVM{YOUR-ID} virtual machine, click AZ220EdgeVM{YOUR-ID}NSG.

  8. On the Network security group blade, click on the Outbound security rules link under the Settings section.

  9. Click the +Add button at the top.

  10. On the Add outbound security rule pane, set the following field values:

    • Destination port ranges: *
    • Action: Deny
    • Name: DenyAll

    A Destination port range of “*” will apply the rule to all ports.

  11. Click Add.

  12. Go back to the Azure Cloud Shell. If the az iot hub monitor-events` command is still running, end it by pressing Ctrl + C.

  13. Within the Azure Cloud Shell connect to the IoTEdgeGateway VM using ssh with the following command:

      ssh <username>@<ip-address>
    

    Be sure to replace the placeholders with the required values for the ssh command:

    Placeholder Value to replace
    username The admin Username for the IoTEdgeGateaway virtual machine.
    IP address The Public IP Address for the IoTEdgeGateway virtual machine.

    Enter the Password for the IoTEdgeGateway username when prompted.

  14. Once connected to the IoTEdgeGateway VM via ssh, run the following command to reset the IoT Edge Runtime.

        sudo systemctl restart iotedge
    

    This script will force the IoT Edge Runtime to disconnect from the Azure IoT Hub service, and then attempt to reconnect.

  15. Within the IoTEdgeGateway, run the exit command to end the ssh session.

  16. Run the az iot hub monitor-events command again within the Azure Cloud Shell to start monitoring the Events being received by the Azure IoT Hub:

        az iot hub monitor-events --hub-name AZ-220-HUB-_{YOUR-ID}_
    

    Be sure to replace the {Your-ID} placeholder with your unique suffix for our Azure IoT Hub instance.

  17. Notice there are no longer any events being received by the Azure IoT Hub.

  18. Go look at the Terminal where the ChildIoTDevice simulated device application is running, and notice that it’s still sending device telemetry to the IoTEdgeGateway.

  19. Now the IoTEdgeGateway is disconnected from the Azure IoT Hub. It will continue to authenticate connections by the ChildIoTDevice, and receiving device telemetry from child device(s). During this time, the IoT Edge Gateway will be storing the event telemetry from the child devices on the IoT Edge Gateway device storage as configured.

  20. In the Azure portal, navigate back to the Network security group blade for the IoTEdgeGateway, and click on Outbound security rules under the Settings section.

  21. On the Outbound security rules pane, click on the DenyAll rule at the top of the list.

  22. On the DenyAll rule pane, click the Delete button to remove this deny rule from the NSG.

  23. On the Delete security rule prompt, click Yes.

  24. The IoTEdgeGateway IoT Edge Transparent Gateway can now connect with Azure IoT Hub. The IoTEdgeGateway IoT Edge Transparent Gateway will sync the event telemetry from all connected child devices including the saved telemetry that couldn’t be sent while disconnected, and all telemetry still being sent to the gateway.

    Note

    The IoT Edge Gateway device will take a couple minutes to reconnect to Azure IoT Hub and resume sending telemetry. After waiting a couple minutes, you will see events showing up in the az iot hub monitor-events command output again.

Next steps

To finish the module, pass the following knowledge check.