Azure Iot Device check connection

long dao thanh 81 Reputation points
2020-12-03T16:10:55.663+00:00

Hi, i am planing to check the connection between the iot hub and iot device. The iot device do some works such as send telemetry to iot hub every 2s. I assume the main problem cause the connection lost because of there is no internet on the iot device. So i write a function to ping to specific host every 2s ( ex: 8.8.8.8(google) ), if it response false, i start the timer to calculate the time its lost connection later. When it can pin to google dns, the internet is back and i will start the iot device again to do it thing.
So i want to ask is my method reliable to check the connection with out send d2c to iot hub to check (because its will cost a lost of messages if i send every 2s/day for let's say 1000 device).
And one more question, i think if i ping to the microsoft dns, it will more correct than the google dns, so what is the microsoft dns i can use to do this method
Thank for your support

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,272 questions
0 comments No comments
{count} votes

Accepted answer
  1. AshokPeddakotla-MSFT 35,971 Reputation points Moderator
    2020-12-04T13:16:14.93+00:00

    @long dao thanh Thanks for reaching out!

    You haven't shared any piece of code to check whether your method is reliable or not. You have some options to check the device connections.
    I would suggest you start with the below documentations:

    The IoT Hub identity registry contains a field called connectionState. Only use the connectionState field during development and debugging. IoT solutions should not query the field at run time. For example, do not query the connectionState field to check if a device is connected before you send a cloud-to-device message or an SMS. We recommend subscribing to the device disconnected event on Event Grid to get alerts and monitor the device connection state. Use this tutorial to learn how to integrate Device Connected and Device Disconnected events from IoT Hub in your IoT solution.

    If your IoT solution needs to know if a device is connected, you can implement the heartbeat pattern. In the heartbeat pattern, the device sends device-to-cloud messages at least once every fixed amount of time (for example, at least once every hour). Therefore, even if a device does not have any data to send, it still sends an empty device-to-cloud message (usually with a property that identifies it as a heartbeat). On the service side, the solution maintains a map with the last heartbeat received for each device. If the solution does not receive a heartbeat message within the expected time from the device, it assumes that there is a problem with the device.

    Regarding your specific questions, please share more details incase if that doesn't help. Kindly check the above details and let us know if you have further queries.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.