Reading from azure IoT endpoint - azure

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".

Related

Azure function missing IoT hub trigger messages

I have created an Azure function to route messages from an IoT hub to an Azure SQL DB using the IoTHubTrigger following mostly the instructions in this link Azure Functions - how to set up IoTHubTrigger for my IoTHub messages?.
Each IoT device captures data every 8 minutes. When capturing is done, the device streams the data in 4 different messages. Then the azure function takes over to write these 4 different messages to the database.
When only one device was streaming, I had no issues with the data, which where written in the db and I could also see/monitor events/messages using az iot hub monitor-events.
When a second device started streaming in the same IoT hub, I started missing messages, meaning that from each device only one message is being stored in the db. Also when using iot hub monitor-events only one message appears from each device. I was also expecting that if I disable the 2nd device, then the 1st one will go back to normal. Unfortynately the issue remains the same.
So my question is: how is it possible a 2nd device screwing up the way that the 1st one interacts with the hub?
If that's not the case, then how we are supposed to figure out what causes the problem at this stage?
Thanks :)
Difficult to say without more details. Are you routing messages in IoT Hub somewhere else? I would go back to a clean IoT Hub with one device and create a consumer group on the IoT Hub for the function. Before running the function I would monitor that consumer group (I like to use the Azure IoT Explorer application) to see if data is coming through as expected, then add another device and keep monitoring the same consumer group. If data is coming through then start the function (consuming data from the consumer group).
If telemetry was not getting read from the IoT Hub consumer group then you will need to look at your device code for any issues.

IoT Monitoring: How to send only one e-mail in a certain timeframe using logic app?

I'm trying to set up an IoT scenario with Azure.
For demo purpose I've used this temperature monitoring scenario:
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-monitoring-notifications-with-azure-logic-apps
An IoT device is sending temperature data to IoT Hub
IoT Hub is routing messages with a temperature > 30 to a service bus
A logic app is reading that service bus and sends a warning e-mail
Now I have the problem that the logic app sends an e-mail for every message that exceeds the temperature limit (every message that is in my service bus). So my inbox will be spammed as soon as I heat up my IoT device.
In reality I would only want to receive one e-mail if my IoT Device overheats, then wait a certain time. How would I handle that in my logic app?
You can achieve this by simply configuring the concurrency control (you can find this in the Settings of your LA trigger) of your Logic App trigger. By default, the concurrency value will be set to 25 and you have to change it to one.
It allows only one message to be processed in your queue (irrespective to the number of messages in your queue) and you can define the interval in the UI of the Service Bus Queue trigger itself, (the time interval at which you need to get another email)

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.

Communication between Azure IoTHub and Azure IoTEdge?

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

Do devices reconnect if IoT Hub restarts?

I'm wondering if devices connected to an IoT Hub reconnect to it (or if they just crash) if IoT Hub for some reason stops and starts again after a while. E.g. if the subscription runs out.
I've been trying to test this but can't find out how to stop my IoT Hub and restart it.
There is no way to stop an IoT Hub. As mentioned here, the IoT Hub will accept messages beyond the limits for a while, but at some point it will refuse the messages (i.e. returns an error). This is something you can detect in your device's code that is sending the events.
You may try to setup a test account with small balance, when this account is overdue the IoT hub status is stopped. You can test under this situation.
And for test purpose to stop IoT hub for a while you can vote up this suggestion.

Resources