【问题标题】:Azure IoT Hub Framework 3.5Azure IoT 中心框架 3.5
【发布时间】:2018-11-13 01:16:09
【问题描述】:

是否有任何好的教程或指南通过使用带有框架 3.5 的设备的 MQTT 协议来使用 Azure IoT Hub?我找到了 M2MQTT 客户端,但它不适用于 Azure IoT Hub。

【问题讨论】:

    标签: azure .net-3.5 mqtt azure-iot-hub


    【解决方案1】:

    IoT Hub 使设备能够直接使用 MQTT v3.1.1 协议与 IoT Hub 设备端点进行通信。你可以看看这个document。在文档中,教程是用 python 编写的,以下代码是使用 uPLibrary.Networking.M2Mqtt 的 C# 的完整示例。

    C# 示例:

        private static string hostName = "<iothub-hosename>";
        private static int port = 8883;
        private static string deviceId = "<deviceid>";
        private static string userName = "";
        private static string password = "";
        private static string certBase64 = "<DigiCert Baltimore Root Certificate>";
    
        static void Main(string[] args)
        {
    
            try
            {
                userName = $"{hostName}/{deviceId}/api-version=2016-11-14";
                password = $"SharedAccessSignature sr=<SAS Token>";
    
                byte[] certBytes = Convert.FromBase64String(certBase64);
                X509Certificate caCert = new X509Certificate(certBytes);
    
                MqttClient client = new MqttClient(hostName, port, true, caCert, null , MqttSslProtocols.TLSv1_0);
                client.ProtocolVersion = MqttProtocolVersion.Version_3_1_1;
    
                client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
                client.MqttMsgPublished += Client_MqttMsgPublished;
                client.ConnectionClosed += Client_ConnectionClosed;
                client.Connect(deviceId, userName, password);
                if(client.IsConnected)
                {
                    //To receive messages from IoT Hub, a device should subscribe using devices/{device_id}/messages/devicebound/# as a Topic Filter. 
                    client.Subscribe(new string[] { $"devices/{deviceId}/messages/devicebound/#" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
    
                    //After making a successful connection, a device can send messages to IoT Hub using devices/{device_id}/messages/events/ or devices/{device_id}/messages/events/{property_bag} as a Topic Name. 
                    client.Publish($"devices/{deviceid}/messages/events/", System.Text.Encoding.ASCII.GetBytes("{id=123}"), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
                }
            }
            catch(Exception ex)
            {
                Console.Write(ex.Message);
            }
    
            Console.Read();
        }
    
        private static void Client_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e)
        {
            Console.WriteLine("Mqtt Published Message-[MsgId:{0}]:{1}", e.MessageId, e.IsPublished ? "Success": "Failure");
        }
    
        private static void Client_ConnectionClosed(object sender, EventArgs e)
        {
            Console.WriteLine("ConnectionClosed");
        }
    
        private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            Console.WriteLine(System.Text.Encoding.ASCII.GetString(e.Message));
        }
    

    在代码中,您可能需要将 DigiCert Baltimore 根证书作为 base64 字符串从 certs.c 复制到 certBase64(删除行 -----BEGIN CERTIFICATE----------END CERTIFICATE-----,并删除 @ 987654331@)。

    更新:

    如何获得 SAS 令牌?

    您可以使用Device Explorer生成SAS令牌,请参阅Using IoT Hub security tokens的设备部分。

    【讨论】:

    • 在我看来,巴尔的摩证书不再适用于物联网。我在上面发布了我的实际代码,但我得到了 CONNACK = 5
    • 我想您可能复制了错误的证书字符串。我用上面的代码测试过,它工作正常。你复制了certs.c文件中的所有字符串吗? DigiCert Baltimore Root Certificate 只是文件的第一部分。请看我的更新。
    • @elur,你解决了这个问题吗?如果有任何问题,请随时告诉我。
    【解决方案2】:

    您指的是 Azure IoT Hub device SDK for .NET,它使用 .NET Framework 3.5 吗?根据 GitHub documentation,适用于 .NET 的 IoT Hub SDK 似乎仅支持 .NET Framework 4.5.1 及更高版本。

    或者,只需使用 Azure IoT Hub Rest API - 然后您就可以从旧的 .NET Framework 3.5 代码发出 HTTP 请求

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2017-09-15
    • 2018-11-11
    相关资源
    最近更新 更多