How to connect IoT hub to Kafka enabled event hub? - azure

If IoT hub can have built in service endpoints for Event hub service then can it send all its messages to Kafka enabled Event hub, if yes how?
Simple link of documentation would also be helpful.

You can route device-to-cloud messages sent to your IoT hub to one or more endpoints by configuring routes - in addition to the built-in endpoint, you can route messages to Event Hubs, Service Bus, or blob storage. To get started, take a look at: https://learn.microsoft.com/azure/iot-hub/iot-hub-devguide-messages-d2c. There's also a routing tutorial you can follow: https://learn.microsoft.com/azure/iot-hub/tutorial-routing

Related

Azure IoT Hub - IoTHubTrigger Azure function doesn't execute if a route is set up on the IoT Hub

I have an IoTHubTrigger Azure function that processes the events from the IoT Hub.
It works as expected until I add a new Route in "Message routing" setting of the IoT Hub to send events to another event hub. In which case, the events successfully get routed to the event hub but don't hit the IoTHubTrigger Azure function anymore. Worth mentioning that the only options I have as endpoints to for route are Event Hubs, Service Bus Queue, Service Bus Topic and Storage.
Is there a way for me to have it route to both, the azure function and the event hub?
The following route will forward all telemetry messages to the default endpoint such as a built-in iot hub endpoint events:

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

How to subscribes to topic on mqtt broker with Azure IOT HUB as i want the data to be stored in Azure iot hub when I published my topic

im new to mqtt and currently trying to setup a mqtt protocol to send data from a gateway devices to azure iot hub. The problem i facing was I couldn't figure out which way that I can received and store data on IoT Hub when i published my data on mqtt broker. The textbook way is to subscribe the mqtt broker using Azure IOT Hub but how should I do it?
Assuming I am doing testing using a laptop
Read data stored in json file -> published to topic "data/device1" -> Data stored in Azure IoT Hub
I tried reading the Azure IoT HUB MQTT Connections but it doesnt work out for me. PLease Help
By default Azure IoT Hub makes incoming telemetry messages available on its Event Hub-compatible endpoint: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-read-builtin It does not matter over which protocol (MQTT, AMQP or HTTPS) you sent in the messages to IoT Hub - they all will land in that endpoint.
From there you can read the information using HTTPS or AMQP. I would recommend to use the Event Hub SDK or use a stream processing service like Azure Stream Analytics or Spark Streaming, which supports Event Hub directly.

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.

Trigger azure functions based on deviceids

I have two devices registered in IoT Hub. say device1 and device2. I have two azure functions say azurefunction1 and azurefunction2.
When a message is sent to Iot Hub with device1 I want azurefunction1 to get triggered but not azurefunction2.
Similarly When a message is sent to Iot Hub with device2 I want azurefunction2 to get triggered but not azurefunction1.
I tried using consumer groups but both azure functions are getting triggered.
Any help appreciated.
This is not directly possible. Events from both devices go to the same Event Hub, and Event Hub is the level of granularity for Functions trigger.
Consumer groups are for independent consumers, which both will get all events.
I would suggest you having a single Function to receive events from all devices. This Function could then route the request based on Device ID to other Functions if needed, e.g. via HTTP calls or via separate Storage Queues.
Another option is to use IoT Hub routing. You can route events to different IoT Hub endpoints based in the deviceid. You can then have azurefunction1 connected to the endpoint for device1, and azurefunction2 connected to the endpoint for device2.
The Routing Messages tutorial describes how to configure and use routing.

Resources