question

MohammadKhajaMoinuddin-5426 avatar image
0 Votes"
MohammadKhajaMoinuddin-5426 asked ZiadDiab-1274 commented

Azure IoT central Device connection status read from REST API

Hi team,

I am trying to read the DEVICE CONNECTION STATUS through REST API to implement a PLC switching condition and also to show the device status on my webpage. I have searched almost all the REST APIs which can support IoT Central. But sadly, I didn't find any of it. Can you please tell me there is a REST API that I missed?

When I tried to get it from the Azure CLI, then I am able to generate the Device Status. But how can I implement it on my code to read status? I tried using Powershell in Azure Functions for enabling "az login" and "az iot central show device twin" commands, but it doesn't work at all. There is no proper documentation of adding Azure CLI in Azure Functions - Powershell.

Can you please help me with reading the Connection Status of each and every device to automate my system? It's really important and urgent for me.

azure-iot-centralazure-iot-sdkazure-iot-dps
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @MohammadKhajaMoinuddin-5426 Welcome to Microsoft Q&A Platform and thanks for your query. Community SME's on this topic or our team will review your scenario and circle back at the possible earliest time.

0 Votes 0 ·

1 Answer

rkiss avatar image
1 Vote"
rkiss answered ZiadDiab-1274 commented

Hi,

based on the az iot central show device twin you can use an undocumented REST GET request to obtain a device twin with an extended info, where is a connectionState property, such as:


https://<appId>.azureiotcentral.com/system/iothub/devices/<deviceId>/get-twin?api-version=1.1-preview&extendedInfo=true

for Authorization header use the <apiToken>


Another way how to obtain a device connection state is to use an eventing, where the Device connectivity events are exported to the Azure Event Grid in the PUSH manner.

The following code snippet shows an example of the events transformation query in the Data Export:

 {
   specversion:"1.0",
   id:.applicationId,
   source: .device.id,
   subject:.messageSource,
   type:.messageType,
   time:.enqueuedTime,
   dataschema:"#"
 }

For destination is used a Webhook to forward the cloudevent message to the custom topic of the Azure Event Grid:

187145-image.png



Note, that there are some limitations for device connection state/events, see more details here.

Thanks
Roman




image.png (88.7 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi all,
I thought the Twin APIs are deprecated and we have to use the updated REST APIs now.
Can we rely in this call for our applications in the long run

Thanks,
Ziad

0 Votes 0 ·