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
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 04, 2019Brass Contributor
I don't send any messages from my device to the iot hub, I want to monitor my devices from the cloud so I can send an alert email when one of them is no longer connected, hence the original webjob that queried the IOT hub for disconnected devices.
Since that is supposedly not a proper way, I created event subscriptions in Iot hub to subscribe to the device disconnected and device connected events that are available, and send those messages to a queue. I created a new webjob that listens on that queue for those messages and then intern, send out an email to the appropriate email address based on the device and the connection state in the event. The problem with this is that I don't ever seem to get any disconnected events and only get connected events. Also, I ran this overnight at got many connected events and only one disconnected event, I would expect to get a one to one ratio....and I really should have not gotten any connect/disconnect events.