Communication between Azure IoTHub and Azure IoTEdge? - azure

I have cloned Azure IoT Edge from https://github.com/Azure/iot-edge. And build this using build.sh script resides in tools directory.
Now I run sample application "simulated device" and it communicates with the Azure IoT Hub. But now I want to send messages from IoTHub to this Simulated device app, it has Receive function which receive data from IoTHub. So I used device explorer to send messages, but every time it does not deliver to simulated device at instatnly, but when I restart simulated device program again, then that previous message has received. Could you please tell me, what I have to do for message receiving from IoT Hub to Azure IoT Edge?

If the simulated device is connecting to IoT Hub using HTTPS, it checks for cloud-to-device messages when it starts up, and then polls for cloud-to-device messages. The recommended polling time is to check every 25 minutes. If the simulated device uses AMQP or MQTT, cloud-to-device messages are pushed to the device. For more information see:
https://learn.microsoft.com/azure/iot-hub/iot-hub-devguide-protocols
https://learn.microsoft.com/azure/iot-hub/iot-hub-devguide-c2d-guidance

Related

Simulated device with Azure Function on IoT Hub is not able to trigger

Currently I have simulated device that receives message from IoT Hub and processes it and after it is done, device sends message to IoT Hub. The device is working but it is running as .exe file on computer and Im trying to transform it and upload on Azure as function so I can avoid it not working when computer turns off or freezes while Im not around it.
I wanted to make it function triggered by events on IoT Hub but Im facing problem. Currently the function that sends message, sends it directly to device on IoTHub(my device) but in case like this, message is not "displayed" on IoT Hub so Im unable to use it as trigger. I'm unable to change function that sends message to device. Are there any options how to catch those messages on IoTHub or possibility to trigger it any other way?
There is no direct way to send messages from Azure function to Azure IoT Hub. You can probably utilize Device event Azure IoT Hub Rest API end point from the Azure Function and send the message to the Azure IoT Hub. Here is the request you can make to send the message to Azure IoT Hub.
POST https://fully-qualified-iothubname.azure-devices.net/devices/{id}/messages/events?api-version=2020-03-13
Please find the following reference to all the Supported bindings for Azure Functions runtime
You can trigger the Azure Function by using the IoT Hub trigger. Refer the following resource that runs you through the steps needed to set up the Azure IoT Hub trigger for Azure Functions

Customization in OTA flow between IOT device and IOT HUB service

I am working on device update or OTA implementation with azure. Azure has device update feature with IOT hub.
I am utilizing same service for my task.
I want to customized the IOT device update code for OTA operation.
Normally IOT HUB Service initiate the all action(Download, Install, Apply) and based on request device will be acted, once requested action done ,device will update the state to IOT hub service and go ideal state where wait for next action..
I want to add one additional layer or confirmation layer for each action[Download, Install, Apply)] , mean when IOT hub Service request for any action,
device will received as twin properties update. Instead executing the respective process(e.g. downloading the Firmware), device will wait for confirmation/ approval (which is customization part to be implemented ) from external/manual resource.
Once confirmation/approval received, device will resume with flow and execute the respective process( device will start downloading the firmware and update response to IOT HUB Service).
My Question based on above scenario as follow
Does it possible to implement above follow with current IOT HUB
device update implementation from azure.
What will happen if device not reply to IOT hub service for requested action ? Does it discard
the whole OTA process ?
What is time period of waiting for IOT hub service when it request for any action and waiting for reply ? My target to make wait for 30 days and if no approval or confirmation
receive the, reply back to IOT hub service with fail/Discard
response.
Can I make 30 days waiting period for IOT hub service API
response ?
Please answer me as soon as possible. As most of development depend on it.
Regards,
Gaurav Choubey
The Device Update Agent (installed on your device) will be responsible to set the "update status" as started succeeded , failed or idle.
As per your scenario and since Device Update Agent is open-sourced you can customize the Interface and Platform Layers to achieve it. IoT Hub should not be impacted by 30 days or more waiting period for the update to be completed.

What is the behavior of IoT Hub when the device send telemetry message without registered in IoT Hub?

I seems to cannot get this information anywhere. I would like to know what will happen to the telemetry message if the device that send this message is not registered in IoT Hub.
This is because I found a few messages from an unregistered device in IoT Hub being processed to the built-in event hub and progress to my next Function App after the built-in event hub.
Thus, I would like to know how is the behavior of IoT Hub if the message was sent without the device registered under the IoT Hub.
Thank you.
This is a really excellent question because it depends on how you connect to the IoT Hub. For my answer, I will take into account MQTT, AMQP, and HTTP. When you connect to an IoT Hub with a device ID that doesn't exist, you will see the following error codes:
MQTT: 401003 IotHubUnauthorized
AMQP: 404001 DeviceNotFound
HTTP: 404001 DeviceNotFound
Now if you have a registered a device but disabled it in IoT Hub, the errors are the same but you won't find the MQTT error in your trace. Instead client side you will receive a Server Unavailable error.
If you want, you can see these error codes for yourself by enabling tracing on the device connect/disconnect events.
So in closing: the behaviour of the IoT Hub is to not allow any devices to connect when unregistered.
In addition to Matthijs van der Veer answer, in the case of connection oriented direct protocol such as MQTT, when the registered device has been connected and the telemetry data is sent under the topic with unregistered device, for instance:
devices/UnregisteredDeviceId/messages/events/$.ct=application%2Fjson&$.ce=utf-8
then, the connected device is immediately disconnected from the IoT Hub.
I do recommend the following:
route your telemetry data messages to the blob storage
export ("excludeKeys":false) all device identities to the blob storage
find some messages which are not in the IoT hub identity registry entry
call the MS support and create the ticket for this issue

Is it possible to reuse Connections on Azure Functions when sending Device-to-Cloud messages to IoTHub?

I have an Azure IoTHub with thousands of devices registered. These devices communicate through a Telco provider who sends messages through an Azure Storage Queue. This Storage Queue triggers an Azure Function which needs to parse the messages and Send an Event to the IoTHub as below.
Currently, we use the Azure IoTHub SDK to create a DeviceClient for each payload and we send the event. Because the DeviceClient represents a device in the IoTHub and is carrying the context of the source of the events, we are having to recreate a device client for each event. This quickly exceeds the threshold of the number of Connections allowed on Azure Functions.
We have tried using the IoTHub Output bindings for Azure Functions, but could not get to work and I do not think it would work because we need to make sure that the events get to the IoTHub with the right context (messages are sent by the right device).
What's the right way to solve this? Can the connections to the IoTHub be reused? Should we abandon Azure Function in favour of something else?
I assume that Telco is some kind of custom device management solution(vendor lock solution), that can also communicate with the device and receive the device telemetry, and eventually forward it to the specified endpoint, correct?
If I may ask and if my assumption is correct, why do you need to deliver the events to IoT Hub, if you are not managing Telco devices through IoT Hub(the arrows on your diagram are only in one direction)?
Using the IoT Hub just as a message broker for essentially cloud-to-cloud communication is not beneficial if that is the only purpose. Also conceptually what you described is cloud-to-cloud communication, and IoT Hub is intended to be used for devices.
Here is what I would do. Setup the API Management(or http triggered Azure Function) as a front door for Telco and pass the messages to the Event Hub.
You can choose here to pass request body for example where your telemetry data is - I assume again.
Keep the IoT Hub, and setup the routing to previously created Event Hub.
Now, in case you have devices that are not vendor locked and that can talk directly to IoT Hub, messages will be re-routed to Event Hub. Also Telco device messages will be routed to exactly the same Event Hub.
Now you can have for example Azure Stream Analytics that can analyze data stream just from the Event Hub, and for both, Telco devices and potentially non-Telco devices.
After trying a few things, I ended up moving away from using the SDK for pushing messages to IoT Hub. This is because the SDK uses AMQP, and creating a DeviceClient for each payload is not viable.
We switched to using HTTPS instead to push the messages to IoT Hub and using HttpClientFactory, we are able to do connection pooling.
I thought I would put this here in case someone has the same issue.
Here is an example of the Http request to send message to IoT Hub
Host: https://<iothubname>.azure-devices.net/devices/<deviceId>/messages/events?api-version=2018-06-30
Authorization: SharedAccessSignature sr=<iothubname>.azure-devices.net&sig=abc123;12344iweoippweruea=iothubowner&se=1570574220
Body: <normal Interval or alarms payloads> // example {"deviceid": "abc", "hello": "world"}
Lastly, thanks #kgalic for the answer but your suggestion would not work. This is not pure B2B integration. Our implementation have to allow for both devices connecting directly to the IoT Hub and devices connecting through the Telco. This is why every device needs to have its own identity and digital twin.

Reading from azure IoT endpoint

I have a little IoT project with one device. Arduino sends some values to azure where function application processes them and sends instructions for arduino to the endpoint in IoT hub. (/devices/MKR1000/messages/devicebound?api-version=2016-02-03)
I need to get data from this endpoint in real time so I want arduino to read only the last (the newest) message every time, but it starts from the oldest.
It's possible to make the arduino read all the messages from the endpoint and than show the last, but I'm looking for a more efficient way.
Thank you.
You receive old messages because they are still queued in Azure IoT Hub due to the device not "complete" these messages. IoT Hub supports the option to complete/reject/abandon C2D messages over HTTPS and AMQP only at the moment.
Another option is setting ExpiryTimeUtc(in function application?) to release older messages faster(Minimum 1 minute. Default: 1 hour.).
More information you can reference "Send cloud-to-device messages from IoT Hub".

Resources