Azure IoT HUB. cloud-to-device messages (MQTT, custom topics)

佐手、 提交于 2020-06-29 05:09:09

问题


Azure IoT HUB. cloud-to-device messages (MQTT, custom topic)

I have an Azure IoT Hub. Here I created a custom device. This device is connected successfully with the Azure IoT Hub. I can also receive the data from this device (device-to -cloud).

But I also want to send a message to this device.

This device uses the "MQTT protocol". I cannot change the subscribe topic and the publish topic in this device, so I have to be able to set this "customtopics" in Azure (Function App).

For this, I have created a function App (IoT Hub (Event Hub)), but I do not know how to implement the "publish and/or subscribe topic" here. All examples are about "messages/events".

run.csx

public static async void Run(EventData myIoTHubMessage, TraceWriter log)
{
    log.Info($"{myIoTHubMessage.SystemProperties["iothub-connection-device-id"]}");
    var deviceId = myIoTHubMessage.SystemProperties["iothub-connection-device-id"].ToString();
    var msg = JsonConvert.SerializeObject("{\"Values\": {\"Slave 8.Channel 1.Output\": false,");
    var c2dmsg = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes(msg));

    await client.SendAsync(deviceId, c2dmsg);
}

Device configuration


回答1:


The Azure IOT Hub is not a generic MQTT Broker. There are predefined topics for device-facing side, see more in details here.

Sending a C2D message to the device is via the service-facing endpoint based on the AMQP protocol. You should use a ServiceClient proxy from the Microsoft Azure IoT Service Client SDK (Microsoft.Azure.Devices). The following code snippet shows this part:

// create proxy
string connectionString = ConfigurationManager.AppSettings["myIoTHub"];
var client = ServiceClient.CreateFromConnectionString(connectionString);

// send AMQP message
await client.SendAsync(deviceId, c2dmsg);

On the device-facing side, a device should subscribe on the following topic filter:

devices/{device_id}/messages/devicebound/#


来源:https://stackoverflow.com/questions/52112397/azure-iot-hub-cloud-to-device-messages-mqtt-custom-topics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!