Skip to main content

MQTT

MQTT is a lightweight publish/subscribe messaging protocol designed for resource-constrained devices and unreliable networks. It's the standard transport for major cloud IoT services — see Azure and AWS for higher-level integrations on top of it.

NuGet package: GHIElectronics.TinyCLR.Networking.Mqtt.

note

TinyCLR's MQTT driver is client-only — for hosting a broker, run one on a server and connect to it from the device.

Connect, subscribe, publish

The example below opens a TLS-protected connection to an MQTT broker on the standard port 8883, subscribes to a topic, and publishes one message. Received messages are logged to the debug output.

using System;
using System.Diagnostics;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using GHIElectronics.TinyCLR.Networking.Mqtt;

var caCertificate = new X509Certificate(Encoding.UTF8.GetBytes("Your CA certificate"));

var mqttHost = "your_host";
var mqttPort = 8883; // Standard MQTT-over-TLS port.
var deviceId = "your_device_id";

var username = "your_username";
var password = "your_password";
var topic = "your_topic";

try{
var clientSetting = new MqttClientSetting{
BrokerName = mqttHost,
BrokerPort = mqttPort,
ClientCertificate = null,
CaCertificate = caCertificate,
SslProtocol = SslProtocols.Tls12,
};

var client = new Mqtt(clientSetting);

var connectSetting = new MqttConnectionSetting{
ClientId = deviceId,
UserName = username,
Password = password,
};

// Connect to the broker.
var returnCode = client.Connect(connectSetting);

ushort packetId = 1;

// Subscribe to a topic.
client.Subscribe(new string[] { topic }, new QoSLevel[] { QoSLevel.ExactlyOnce }, packetId++);

// Publish a message.
client.Publish(topic, Encoding.UTF8.GetBytes("your message"), QoSLevel.AtMostOnce, retain: false, packetId);

// Handler runs for every message arriving on a subscribed topic.
client.PublishReceivedChanged += (sender, receivedTopic, data, duplicate, qosLevel, retain) => {
Debug.WriteLine("Received on " + receivedTopic + ": " + Encoding.UTF8.GetString(data));
};
}
catch (Exception ex){
Debug.WriteLine("MQTT setup failed: " + ex.Message);
}

Event handlers

The Mqtt client exposes five events for lifecycle and message activity:

EventFires when
ConnectedChangedConnection to the broker is established or lost.
SubscribedChangedA subscription has been acknowledged by the broker.
UnsubscribedChangedAn unsubscribe has been acknowledged by the broker.
PublishedChangedA locally-sent publish has been acknowledged.
PublishReceivedChangedA message arrived on one of the subscribed topics.
client.ConnectedChanged += (connected) =>
Debug.WriteLine("Connection state changed");

client.SubscribedChanged += (sender, packetId, qos) =>
Debug.WriteLine("Subscribe acknowledged, packet " + packetId);

client.UnsubscribedChanged += (sender, packetId) =>
Debug.WriteLine("Unsubscribe acknowledged, packet " + packetId);

client.PublishedChanged += (sender, packetId, qos) =>
Debug.WriteLine("Publish acknowledged, packet " + packetId);

client.PublishReceivedChanged += (sender, topic, data, duplicate, qos, retain) =>
Debug.WriteLine("Received on " + topic + ": " + Encoding.UTF8.GetString(data));

API reference

NamespaceDescription
GHIElectronics.TinyCLR.Networking.MqttMQTT client classes