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

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.

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

Azure Hub IoT to Azure Function Pricing

I'm making a comparison between different Cloud vendors for IoT solutions.
I'm now on Azure IoT Hub, which will ingest data from IoT devices (we want only to send from devices to cloud, say through MQTT, and not receive anything back). My aim is to pass these data to an Azure Function, execute some computation, and save them in some DB (e.g.CosmosDB). Here my doubts:
Which MQTT messages Do I have to consider for the billing? (only those with telemetry or which others?)
I saw that there is the possibility of going from Hub IoT to Azure Function with a built-in endpoint. Is it free? I'm afraid of some hidden costs, like those of Event Hubs or the built-in endpoint.
The sending of data from IoT Hub to Azure Function is considered as a normal "cloud-to-device" messages (and consequently they're billed), or are they free? For example IoT Hub ingest 10 messages (<4KB) and forward them to Functions. Do I pay them as 10 messages or 20 messages?
Thanks in advance for your help.
Which MQTT messages - You have to consider the below list of messages for Azure IoTHub Billing, they are
Device to Cloud
Cloud to Device
Method execution
Jobs
Detailed break up of billing information by each operation
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-pricing#charges-per-operation
Nothing free in the cloud :)
Data read from IoTHub to any other service attracts the regular data transfer cost. Refer to the below link
https://azure.microsoft.com/en-in/pricing/details/bandwidth/
All the pricing information detailed here https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-pricing is specific to Azure IoTHub for D2C & C2D. Any data egress from IoTHub to other Azure Services is as per the regular data transfer charges.
https://azure.microsoft.com/en-in/pricing/details/bandwidth/

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.

Creating a workflow for an IoT project

I was using ThingSpeak for an IoT project. I've decided to move on with Azure IoT. My project consists Raspberry Pi 3's sending data to an IoT platform using MQTT protocol which will be displayed over mobile app.
While using ThingSpeak, things were easier. I was sending data to IoT platform, ThingSpeak was storing them without me configuring almost anything and mobile app that I wrote was sending HTTP request to IoT platform with an interval. Then, I was parsing JSON response on mobile app to display important values in real time.
So far I've managed to send datas to IoT hub using Azure IoT C SDK. However I am very confused about how I am going to implement these on Azure IoT, what my workflow should be like.
Azure IoT Hub ingests data from your devices into the cloud and then delivers that data to other destinations for storage or processing. By default, IoT Hub delivers data from devices to an Event Hubs compatible end point (these Quickstarts illustrate this process: https://learn.microsoft.com/azure/iot-hub/quickstart-send-telemetry-node). IoT Hub can also use routing rules to send data to other locations, such as storage or a Service Bus queue. The following tutorial illustrates these options for you: https://learn.microsoft.com/azure/iot-hub/tutorial-routing.

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