Microsoft has its own SDK for interacting with IoT Hub (Microsoft.Azure.Devices)
It features a client called ServiceClient that as I understand it is client you are supposed to use when sending messages from your backend to a device through IoT hub.
When initializing the ServiceClient like below the only supported TransportTypes are Amqp and Amqp_websockets_only. My devices only support MQTT. How should I go about that?
public async Task SendMessage()
{
var serviceClient = ServiceClient.CreateFromConnectionString("", TransportType.Amqp);
var commandMessage = new Message(Encoding.ASCII.GetBytes("Cloud to device message."));
await serviceClient.SendAsync("myFirstDevice", commandMessage);
}
According to the c2d guidelines "all protocols" are supported.
I have read through this documentation, but it only documents device to cloud, not cloud to device.
Looking at the SDK documentation it only lists AMQP for key features under device-to-cloud
For device-to-cloud messaging, the device can use the MQTT, MQTT-WS, AMQP, AMQP-WS, or HTTP protocols. See the IoT Hub device SDK key features. A device communicates with IoT Hub for both d2c and c2d using the Microsoft Azure IoT device SDK for .NET. Note that there are also device SDKs available for Java, Node, Python and C.
If you have a back-end application that needs to send a c2d message to a device, then that back-end application should use one of the Azure IoT service SDKs such as
Microsoft Azure IoT service SDK for C# to connect to IoT Hub and send the c2d message. The back-end application must use AMQP or AMQP-WS.
The protocol used by the device to connect to IoT Hub is independent of the protocol used by the back-end application to connect to IoT Hub. For example, a back-end application could connect to IoT Hub using AMQP to send a c2d message to a device that's connected to the hub using MQTT.
Related
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
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
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.
I'm using iot edge modules. I need to send messages to the hub from the edge module.
Per my understanding, I need to send it first to the iot edge hub, the edge hub will take care of transferring it to the cloud iot hub. I can consume it from there.
If that's supported, I'm looking for a REST sample on how to do that (or just REST documentation)
To send data to the IoT Edge hub, a module calls the SendEventAsync method.
ModuleClient client = new ModuleClient.CreateFromEnvironmentAsync(transportSettings);
await client.OpenAsync();
await client.SendEventAsync(“output1”, message);
Checkout the below link for moduleclient class methods and properties.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.devices.client.moduleclient?view=azure-dotnet
You don't necessarily have to use the ModuleClient SDK if you want to sent messages through IoT Edge to the cloud. The alternative would be to use IoT Edge in a transparent gateway mode: https://learn.microsoft.com/en-us/azure/iot-edge/how-to-connect-downstream-device
In this way your (virtual) device could connect to the Edge Hub just as it would connect directly to the IoT Hub - using AMQP, MQTT or - as you want to - HTTP.
I am just reading up on the new IoT Central, currently in preview.
Our customers seldom let their devices connect to cloud by themselves,
but are rather connected to some kind of management system that in turn connects to Azure.
We however consider using IoT Central for demo purposes and would like to know if devices defined in IoT Central sends their data to an IoT Hub of choice or if is handled in any other way behind the scenes?
Microsoft IoT Central does use an IoT Hub internally. If you look here you can see that when you add a physical device to an IoT Central application it generates an IoT Hub device connection string: https://learn.microsoft.com/microsoft-iot-central/tutorial-add-device#get-connection-string-for-real-device-from-application.
However, you cannot get admin access to the IoT Hub or use an IoT Hub of your own choosing - all you have is the device connection string.