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 state issue. I'm seeing a problem where one device will look like it is down about once every hour (or 1 hour and 5 minutes). But the device is not down and there is no intermittent connection issue. I currently have 3 devices, all connected through different internet ISPs. One system started having this issue every so often, then another did, and now the 3rd one is having this issue.
My web job code is pretty simple and runs every 5 minutes to see if there are any devices that are down. Here's my code
public static async Task CheckDeviceAvailability([TimerTrigger("00:05:00", RunOnStartup = true)] TimerInfo timer)
{
string connectionString = CloudConfigurationManager.GetSetting("IotHubConnectionString");
try
{
RegistryManager manager = RegistryManager.CreateFromConnectionString(connectionString);
IQuery query = manager.CreateQuery("SELECT * FROM devices where ConnectionState = 'Disconnected'", 10);
while (query.HasMoreResults)
{
var page = await query.GetNextAsTwinAsync();
foreach (Twin twin in page)
{
await SendNotification(twin.DeviceId);
}
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
I've just added a check (if (twin.ConnectionState == DeviceConnectionState.Disconnected)) above the Await SendNotification line in the above code just to see if this helps out, but it seems like the query shouldn't be returning any devices that are connected.
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
- asergaz
Microsoft
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
- asergaz
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_doorBrass 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_doorBrass 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?
- smart_doorBrass ContributorThanks, I will take a look and make changes accordingly.