通过使用带有框架3.5的设备的MQTT协议,是否有任何使用Azure IoT Hub的好教程或指南?我找到了M2MQTT客户端,但它无法与Azure IoT Hub一起使用 .
IoT Hub使设备能够直接使用MQTT v3.1.1协议与IoT Hub设备 endpoints 进行通信 . 你可以看一下这个document . 在文档中,教程是用python编写的,下面的代码是使用uPLibrary.Networking.M2Mqtt的C#的完整示例 .
C# Example:
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 Root Certificate 从certs.c复制为 certBase64 作为base64字符串(删除行 -----BEGIN CERTIFICATE----- 和 -----END CERTIFICATE----- ,并删除 \r\n\ ) .
certBase64
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
\r\n\
Update:
How to get SAS token?
您可以使用Device Explorer生成SAS令牌,请参阅Using IoT Hub security tokens的设备部分 .
你指的是 Azure IoT Hub device SDK for .NET ,它是否与 .NET Framework 3.5 一起使用?根据GitHub documentation,似乎IoT Hub SDK for .NET仅支持.NET Framework 4.5.1及更高版本 .
或者,只需使用Azure IoT Hub Rest API - 然后您就可以从旧的.NET Framework 3.5代码发出HTTP请求
2 回答
IoT Hub使设备能够直接使用MQTT v3.1.1协议与IoT Hub设备 endpoints 进行通信 . 你可以看一下这个document . 在文档中,教程是用python编写的,下面的代码是使用uPLibrary.Networking.M2Mqtt的C#的完整示例 .
C# Example:
在代码中,您可能需要将 DigiCert Baltimore Root Certificate 从certs.c复制为
certBase64
作为base64字符串(删除行-----BEGIN CERTIFICATE-----
和-----END CERTIFICATE-----
,并删除\r\n\
) .Update:
How to get SAS token?
您可以使用Device Explorer生成SAS令牌,请参阅Using IoT Hub security tokens的设备部分 .
你指的是 Azure IoT Hub device SDK for .NET ,它是否与 .NET Framework 3.5 一起使用?根据GitHub documentation,似乎IoT Hub SDK for .NET仅支持.NET Framework 4.5.1及更高版本 .
或者,只需使用Azure IoT Hub Rest API - 然后您就可以从旧的.NET Framework 3.5代码发出HTTP请求