将 MQTTnet 与 Azure IoT 中心连接

Connect MQTTnet with Azure IoT Hub

我创建了一个传送消息的设备。现在我想使用 MQTTnet 版本 2.4.0 将它们发送到 Azure IoT Hub,因为 .NET 目标框架在版本 4.5 上并且它不是我决定改变它。

我的问题:

我已经尝试了 ClientId/UserName/Password 的几乎所有值组合,如下所述:https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#using-the-mqtt-protocol-directly-as-a-device

但其中 none 对我有用

我在项目之外尝试过,并在当前框架上构建了一个类似的设备,它与更新版本的 MQTTnet 完美配合。

遗憾的是,大约 10 秒后我没有收到任何类型的错误消息,只有 MqttCommunicationTimedOutException。

感谢您的帮助,我已经被这个问题困了将近一个星期了。

以下代码片段是模拟 device1 通过 MQTTnet 版本 2.4.0[ 使用 MQTT 协议直接连接到 Azure IoT 中心的工作示例图书馆:

using MQTTnet;
using MQTTnet.Core;
using MQTTnet.Core.Client;
using MQTTnet.Core.Packets;
using MQTTnet.Core.Protocol;
using System;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var options = new MqttClientTcpOptions()
            {                
                Server = "myIoTHub.azure-devices.net",
                Port = 8883,
                ClientId = "device1",
                UserName = "myIoTHub.azure-devices.net/device1/api-version=2018-06-30",              
                Password = "SharedAccessSignature sr=myIoTHub.azure-devices.net%2Fdevices%2Fdevice1&sig=****&se=1592830262",               
                ProtocolVersion = MQTTnet.Core.Serializer.MqttProtocolVersion.V311,
                TlsOptions = new MqttClientTlsOptions() { UseTls = true },
                CleanSession = true
            };

            var factory = new MqttClientFactory();
            var mqttClient = factory.CreateMqttClient();

            // handlers
            mqttClient.Connected += delegate (object sender, EventArgs e)
            {
                Console.WriteLine("Connected");
            };
            mqttClient.Disconnected += delegate (object sender, EventArgs e)
            {
                Console.WriteLine("Disconnected");
            };
            mqttClient.ApplicationMessageReceived += delegate (object sender, MqttApplicationMessageReceivedEventArgs e)
            {
                Console.WriteLine(Encoding.ASCII.GetString(e.ApplicationMessage.Payload));
            };

            mqttClient.ConnectAsync(options).Wait();

            // subscribe on the topics
            var topicFilters = new[] {
                new TopicFilter("devices/device1/messages/devicebound/#", MqttQualityOfServiceLevel.AtLeastOnce),
                new TopicFilter("$iothub/twin/PATCH/properties/desired/#", MqttQualityOfServiceLevel.AtLeastOnce),
                new TopicFilter("$iothub/methods/POST/#", MqttQualityOfServiceLevel.AtLeastOnce)
            };
            mqttClient.SubscribeAsync(topicFilters).Wait();


            // publish message 
            var topic = $"devices/device1/messages/events/$.ct=application%2Fjson&$.ce=utf-8";
            var payload = Encoding.ASCII.GetBytes("Hello IoT Hub");
            var message = new MqttApplicationMessage(topic, payload, MqttQualityOfServiceLevel.AtLeastOnce, false);
            mqttClient.PublishAsync(message);

            Console.Read();
        }       
    }
}

以下屏幕片段显示了更新所需双胞胎 属性 color 和接收 C2D 消息[=22] 的输出示例=]: