Hi! Long time, no see. Itâs been a few months since I posted here. I wonât bore you with the many âIâve been busyâ excuses (but I have!). Anyway, enough assuaging my guilt over not having shared with you guys in a while. Letâs get to why you came here (since google.com probably sent you)
One of the frequent questions we get related to any IoT device, but especially IoT Edge is âhow do I know if my IoT device/edge is up and running and talking to IoT Hub?â I was recently researching this topic for a customer and ran across some cool stuff. I even did a quick little sample/demo Iâll share at the bottom involving IoT Edge and Microsoft Teams.
IoT Edge/Hub and Event Grid
As you may know, IoT Hub integrates with Azure Event Grid. Azure Event Grid is a fully managed event routing service that uses a publish-subscribe model. Event Grid integrates with a bunch of Azure Services (a pic is on that link above), including IoT Hub. As shown from the list below, stolen from that same link, IoT Hub publishes several event types to Event Grid.
- Microsoft.Devices.DeviceCreated – Published when a device is registered to an IoT hub.
- Microsoft.Devices.DeviceDeleted – Published when a device is deleted from an IoT hub.
- Microsoft.Devices.DeviceConnected – Published when a device is connected to an IoT hub.
- Microsoft.Devices.DeviceDisconnected – Published when a device is disconnected from an IoT hub.
- Microsoft.Devices.DeviceTelemetry – Published when a device telemetry message is sent to an IoT hub
Note that Device Connected and Device Disconnected events will be raised when the devices connect or disconnect. That can be an explicit connection and disconnection, or a disconnect after a certain number of missed âheartbeatsâ, meaning the device or network may have committed seppuku (not to be confused with Sudoku) . You can set Event Grid subscriptions to these events and then respond however you want to (Logic Apps, Azure Functions, Text, Email, etc)
So, what about IoT Edge?
Ok, Steve, but google sent me here because I searched for information about monitoring IoT *Edge*connectivity. If it pleases the court, are you going to talk about that any time soon?
Yes! The reason this is relevant to IoT Edge is that the connection/disconnection events not only work for IoT end-devices, but it also works for IoT Edge modules! So, leveraging that, you can tell if any of your IoT Edge modules disconnect and donât reconnect (did they crash?) or also if the IoT Edge runtime itself loses connectivity (crash? network issue?) by looking for those events for the $edgeHub and $edgeAgent modules.
Ok skippy, prove it!
Fine! I will⌠The IoT Hub/Event Grid teams have a nice little example of using the integration between IoT Hub and Event Grid to persistently track the status of your device connectivity in a CosmosDB database, via a LogicApps. It will have a record for all of your devices, and their last connect/disconnect time. Itâs a pretty nice little sample and could be the beginning of a connectivity monitoring solution You can build a UI, have it pull the status from CosmosDB, etc.
Check it out if you have the time or inclination.
However, I was too lazy busy to create a CosmosDB, stored procedure, UI, etc. So I took the quicker way out. I created a Channel in Microsoft Teams and, each time a device/module connected or disconnected, I just post a message to the channel.
I basically followed that article and all the pieces of it, except everywhere it referenced the stored procedure and CosmosDB, I just instead leveraged the built-in LogicApps connector for Microsoft Teams, which is super-easy to use.
The result is shown below. I have a raspberry pi running IoT Edge sitting on my desk. It has a SenseHat accessory in top of it and was running a module that I wrote that talks to it (uncreatively named âsensehatâ â they donât pay me enough for naming creativity). At about 10:55 local time, I reached over the yanked the power plug out of the raspberry pi. 2-3 minutes later (after some missed heartbeats), viola, I got a post to my Teams channel telling me that both my module (sensehat) as well as the Edge runtime ($edgeHub and $edgeAgent) had disconnected. (rpihat2 is the device name for my IoT Edge device)
(full disclosure â I made ZERO effort to make the message look pretty)
I could just as easily, via LogicApps again, have leveraged SendGrid, Twilio, etc to send me email alerts, TXTs, or whatever for these events. If you do use Microsoft Teams, you can set alerts on the Channel when new events are posted too. If you use the mobile app, you even get push notifications.
I didnât write step-by-step instructions for doing this, as I hope itâs not too hard to follow the Azure IoT teamâs cool instructions above and just substitute Teams/SendGrid/Twilio, etc for the output.. but feel free to let me know if I need to do step-by-step.
Leave a comment and let me know if this, or any of my other content, is useful (or if you need/want step-by-step instructions)
Enjoy,
–Steve