What is the behavior of IoT Hub when the device send telemetry message without registered in IoT Hub? - azure

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

Related

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.

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 connect IoT hub to Kafka enabled event hub?

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

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

Resources