【发布时间】:2018-11-13 01:16:09
【问题描述】:
是否有任何好的教程或指南通过使用带有框架 3.5 的设备的 MQTT 协议来使用 Azure IoT Hub?我找到了 M2MQTT 客户端,但它不适用于 Azure IoT Hub。
【问题讨论】:
标签: azure .net-3.5 mqtt azure-iot-hub
是否有任何好的教程或指南通过使用带有框架 3.5 的设备的 MQTT 协议来使用 Azure IoT Hub?我找到了 M2MQTT 客户端,但它不适用于 Azure IoT Hub。
【问题讨论】:
标签: azure .net-3.5 mqtt azure-iot-hub
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的设备部分。
【讨论】:
certs.c文件中的所有字符串吗? DigiCert Baltimore Root Certificate 只是文件的第一部分。请看我的更新。
您指的是 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 请求
【讨论】: