Forum Discussion
IoT Hub Device twin returning connectionState = Disconnected, when it's actually connected
- Dec 31, 2018
Hi!
You should have a look here: Device heartbeat
"
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.
"
Hope that helps!
Thanks
Hi!
You should have a look here: Device heartbeat
"
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.
"
Hope that helps!
Thanks
- NagnathNov 26, 2019Copper Contributor
asergaz Hi, this is out of the subject, but how could we change the connection state programmatically. Also status of device.
- asergazNov 26, 2019
Microsoft
Nagnath hi. You can use the azure iot sdk and give a look at how to manipulate device twin here.
Open a new post if you have further questions. Thanks :)!
- smart_doorJan 03, 2019Brass Contributor
The tutorial seems overly complicated. I don't want to have a cosmos db to store connection state info just to send an email.
Seems like I could create a subscription on the IOT hub and have it put a disconnection/connection message into a queue, and have my webjob listen on this queue for connection state changes and then send out emails. I can't find much info on this, and only the connection message is listed. I tried this but my webjob (running locally never got triggered).
Any help on this would be greatly appreciated!
Thanks
- smart_doorJan 04, 2019Brass Contributor
I was able to create events on my Iot hub for device connected and device disconnected, and it sends them to a queue. I created a webjob that gets triggered by the queue and reads the id and the state from the message, but I only seem to get connected messages, not disconnected messages. Ie if I unplug my iot device, nothing happens, even if I wait 5+ minutes. When I plug it back in, I get a device connected message after a minute or so.
Any ideas what I might be doing wrong? Is it something on my device client code?
- asergazJan 04, 2019
Microsoft
Hi,
Can you share the snippet of the code? I am not sure how do you manage to send a "disconnected" message from your device to IoTHub if the device is disconnected?
Thanks.
- smart_doorJan 02, 2019Brass ContributorThanks, I will take a look and make changes accordingly.