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

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

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.

How to check the source of sending message to IoT Hub in Azure?

I have an environment setup with multiple devices able to send message to IoT Hub and goes to my other Function App at the moment. However I found out there are some devices is sending message through IoT Hub but it does not registered under the IoT Hub.
I felt strange as the device that do not registered in IoT Hub should not be sending message successfully through the IoT Hub. But the message is sent and reach to my next Function App after IoT Hub. So I would like to check the source of the message. Like in what method it used to send message, via API or some other method.
May I know where can I find any information regarding the source? I searched through Azure documents but points me to nowhere like this. I would like to find out how it sends message without registration in IoT Hub.
Sample Message that go through without registration in IoT Hub:
{
"did": "TEST001",
"msg": null,
"pid": "0000-0001",
"pld": {
"data": "-0.015,41.895,7.199,23.49",
"error": "00000000"
},
"dts": "2020-03-26T07:39:21.8969490Z"
}
What I am expecting is, since the device had no record in IoT Hub device list, is that this message will not go through IoT Hub, it will be dropped instead. However I saw this message goes through IoT Hub and reach my next Function App after IoT Hub. I wonder why is that.
Thank you.

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.

How to send message to Azure IOT hub and display it on Client application using Azure function in portal

Am working on Azure resources like Azure Service Bus, Azure Functions, IOT Hub. Here am trying to send queue messages from Azure Service Bus to IOT Hub using Azure functions and then display that messages in my local device (Cloud-To-Device). Am able to read my messages in Azure function using Service Bus Queue Trigger and tried to send them to IOT Hub as output of function. Once, when I run the Azure function "Its can sending the messages to IOT Hub as output",but it unable to send them to client device. Can you please suggest me to "How to solve this situation"
As far as I know there is currently no way to select a Cloud to Device Message(C2D) as a Azure Functions Output.
You also cannot use the Event Hub Output as it does not support C2D messages as described here.
I can think of 2 methods to accomplish C2D messaging in Azure functions:
Use the Azure IoT SDK as described in this answer and shown in this channel9 video from 2017 (might be out of date).
Use the Azure IoT Hub REST API. You can find general configuration options here and the API endpoint to use would be senddevicecommand.
Unfortunately there is current no output binding for IoT Hub from Functions (you could write a new custom binding, though ;) )
To talk from a Function to your devices, you need the Azure Device Service SDK of the IoT Hub. Then you can either use Cloud-to-Device messages (asynchronous) or Direct Methods (synchronous). You can find a example of the latter in my GitHub repo here: https://github.com/sebader/iotedge-end2end/blob/master/CloudFunctions/DirectMethodCaller.cs
The important pieces being:
ServiceClient _iothubServiceClient = ServiceClient.CreateFromConnectionString(config["iothubowner_cs"]);
var methodRequest = new CloudToDeviceMethod("YourDirectMethodName", TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
var result = await _iothubServiceClient.InvokeDeviceMethodAsync(device, module, methodRequest).ConfigureAwait(false);
An implementation for C2D messages will look pretty much the same.

How to make use of Function Apps for Azure IoT Hub?

So I've been trying to generate a small function app (in JS) that responds to a message sent to the Azure IoT Hub. The output is a simple console log. However, I am unable to load the event hub compatible end point of the IoT Hub as the trigger (the only option available is to create a new Event Hub). How do I proceed? The code for the device to send to Azure' IoT Hub is working and I am able to view the messages via the Device Explorer tool.
You need to create new Event Hub Connection and use the Event Hub-Compatible endpoint from the Iot Hub Messaging section.
But you need to change it to match the service bus connection string format -
"Endpoint=[your iot hub compatible end point];SharedAccessKeyName=[your key name];SharedAccessKey=[your key];EntityPath=[your event hub compatible name]"
key name and key can be taken from the "Shared access policy" section.
Good Luck

Resources