Forum Discussion
smart_door
Dec 26, 2018Brass Contributor
IoT Hub Device twin returning connectionState = Disconnected, when it's actually connected
I have a Monitoring web job that querys my iot devices for any that have a connection state = disconnected, and then sends an email out to the user of that device to let them know it has a connection...
- 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
smart_door
Jan 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_door
Jan 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.
- smart_doorJan 04, 2019Brass Contributor
The goal of this is to monitor for disconnected devices and notify the user of that device that it is offline (or back online). I cleared out the queue this morning and already have 15 device connected events for 3 different iot devices. This is problematic as it will send false notifications to something that has not disconnected.
I just want to know when a device goes offline, and when it comes back up so that I can notify users of the device in a timely manner and at the same time, not spam them with messages that don't offer any benefit.
Does this make sense at what I'm trying to accomplish? the device twin query that ran every 5 minutes seem to work better than this, only that once in a while a device would report offline once every 1 hour & 5 minutes for what ever reason, giving false issues. the connected events are showing up way more frequently, and I don't even seem to get a disconnected event even when I pull the network cable for 5 minutes from a test device.