How to make use of Function Apps for Azure IoT Hub? - node.js

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

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

IoT Hub message routing with DeviceLifecycleEvents source does not work

I am already using an IoT Hub and have configured message routing to route TwinChangeEvents to an Event Hub. An Azure function with an EventHubTrigger processes the messages. This works perfectly fine.
Now, I wanted to configure message routing to route DeviceLifecycleEvents to another Event Hub. Again, another Azure function with an EventHubTrigger should process the messages.
However, the function is not triggered when I connect or disconnect devices.
When I change the data source of the message routing to TwinChangeEvents, the function is triggered as expected (with the wrong messages of course). That said, I am pretty confident that my configuration of the Event Hub and the Function is correct.
Also, I have tried to configure an Event Subscription in the IoT Hub with for the lifecycle events to the Event Hub. I think this option uses an Event Grid. Anyways, the function gets triggered as expected with this configuration. The problem only occurs when using message routing for the lifecycle events.
Can anyone point me towards a solution why the message routing does not work? Am I missing a configuration in the IoT Hub?
Unfortunately, Event hub doesn't support device connected and disconnected events. This has been confirmed by Microsoft support too. Below is the reply I got from them:-
IoT Device Lifecycle events designed only for device create and delete events. From the description you want your IoT Hub to trigger following events to your Event Hub: Device Created Device Deleted Device Connected Device Disconnected With this requirement you should create a new Event Subscription with Event Grid instead of configuring the IoThub message routing with Device Lifecycle Events as data source. Please go to the IoT Hub and create a new Event Subscription by this path: IoT Hub->Events->+Event Subscription
At the end of the day, I created a event subscription for all device life cycle events and routed them to my custom event hub endpoint. I think you can also do the same, If you want to only subscribe to the Event hub but not Event Grid.
EDIT:- Message routing now supports Device connection state events. Non telemetry events supported by Event hub message routing.
Finally, if a route is created with data source set to device
connection state events, IoT Hub sends a message indicating whether
the device was connected or disconnected.

Read device to cloud messages without CLI, device explorer

I am trying to read D2C messages in android application. I have successfully implemented it using signalR services. But now i want to remove signalR services and read messages like device explorer or cli.
Is there a way to use sockets or any APIs to read D2C messages ?
Your IoT Hub exposes an Event Hub endpoint. You can read messages from the built-in Event Hub endpoint with an Event Hub client. Here is an example using Java

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.

Resources