Azure IoT HUB。云到设备消息(MQTT,自定义主题)

问题描述 投票:0回答:1

Azure IoT HUB。云到设备消息(MQTT,自定义主题)

我有一个Azure IoT Hub。这里我创建了一个自定义设备。此设备已与Azure IoT Hub成功连接。我也可以从这个设备(设备到云)接收数据。

但我也想向此设备发送消息。

该设备使用“MQTT协议”。我无法更改此设备中的订阅主题和发布主题,因此我必须能够在Azure(功能应用程序)中设置此“customtopics”。

为此,我创建了一个功能App(IoT Hub(Event Hub)),但我不知道如何在这里实现“发布和/或订阅主题”。所有示例都是关于“消息/事件”。

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

Device explorer twin test

mqtt azure-iot-hub
1个回答
0
投票

Azure IOT Hub不是通用的MQTT Broker。有面向设备的预定义主题,请参阅详细信息here

通过基于AMQP协议的面向服务的端点向设备发送C2D消息。您应该使用Microsoft Azure IoT服务客户端SDK(Microsoft.Azure.Devices)中的ServiceClient代理。以下代码段显示了此部分:

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

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

在面向设备的一侧,设备应订阅以下主题过滤器:

devices/{device_id}/messages/devicebound/#
© www.soinside.com 2019 - 2024. All rights reserved.