Are there any good tutorials or guides to using Azure IoT Hub via using the MQTT protocol from devices with framework 3.5? I found the M2MQTT client but it won't work with Azure IoT Hub.
2 Answers
IoT Hub enables devices to communicate with the IoT Hub device endpoints using MQTT v3.1.1 protocol directly. You can take a look at this document. In the document the tutorial is written in python, following code is a full sample for C# using uPLibrary.Networking.M2Mqtt.
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));
}
In the code, you may need to copy the DigiCert Baltimore Root Certificate to certBase64
from certs.c as base64 string(remove the lines -----BEGIN CERTIFICATE-----
and -----END CERTIFICATE-----
, and remove \r\n\
).
How to get SAS token?
You can use Device Explorer to generate SAS tokens,please see the device section of Using IoT Hub security tokens.
Are you referring to Azure IoT Hub device SDK for .NET, and it working with .NET Framework 3.5 ? As per the GitHub documentation, it seems IoT Hub SDK for .NET supports only .NET Framework 4.5.1 and above only.
Alternatively, Simply using Azure IoT Hub Rest API - then you can make HTTP requests from your legacy .NET Framework 3.5 code