M2Mqtt Documentation

repository·master·Indexed 19 days ago

https://github.com/eclipse-paho/paho.mqtt.m2mqtt

An MQTT client library for .NET and WinRT platforms, designed for Internet of Things (IoT) and Machine-to-Machine (M2M) communication. It supports .NET Framework, .NET Compact Framework, .NET Micro Framework, Mono, and various WinRT platforms including Windows 10. The library provides the MqttClient class for publishing and subscribing to topics, with optional SSL/TLS support.

Tokens
951
Snippets
3
Records
5
Agent score
19%

What's inside M2Mqtt

  1. Supported .NET and WinRT platforms

    master

    M2Mqtt is a lightweight MQTT client designed for constrained networks and embedded devices. It supports a wide range of .NET and WinRT platforms:

    ** .NET Platforms:**

    • .Net Framework (up to 4.5)
    • .Net Compact Framework 3.5 & 3.9 (for Windows Embedded Compact 7 / 2013)
    • .Net Micro Framework 4.2 & 4.3
    • Mono (for Linux O.S.)

    ** WinRT Platforms:**

    • Windows 8.1
    • Windows Phone 8.1
    • Windows 10
  2. Configure SSL/TLS support in M2Mqtt

    master

    M2Mqtt supports SSL/TLS. To enable this feature, the symbol SSL must be defined before compiling the project. The repository comes with this symbol pre-defined and all necessary assemblies referenced.

    To reduce memory footprint by disabling SSL/TLS:

    1. Remove the SSL symbol.
    2. Remove all assemblies referenced for SSL/TLS.
    3. Alternatively, you can keep the default configuration and simply set the secure parameter to false and cacert to null in the MqttClient constructor (these are the default values).

    Note: .Net Micro Framework supports up to TLSV1.

  3. Use MqttClient to publish messages

    master

    To send messages, use the MqttClient.Publish() method.

    1. Instantiate MqttClient with the broker's IP address.
    2. Call Connect(clientId).
    3. Call Publish(topic, payload, qos, retain) where payload is a byte array.
    // create client instance 
    MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS)); 
    string clientId = Guid.NewGuid().ToString(); 
    client.Connect(clientId); 
     
    string strValue = Convert.ToString(value); 
     
    // publish a message on "/home/temperature" topic with QoS 2 
    client.Publish("/home/temperature", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); 
  4. Use MqttClient to subscribe to topics

    master

    To receive messages, use the MqttClient class. You must register an event handler for MqttMsgPublishReceived to process incoming messages.

    1. Instantiate MqttClient with the broker's IP address.
    2. Subscribe to the MqttMsgPublishReceived event.
    3. Call Connect(clientId).
    4. Call Subscribe() specifying the topics and their Quality of Service (QoS) levels.
    // create client instance 
    MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS)); 
     
    // register to message received 
    client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; 
     
    string clientId = Guid.NewGuid().ToString(); 
    client.Connect(clientId); 
     
    // subscribe to the topic "/home/temperature" with QoS 2 
    client.Subscribe(new string[] { "/home/temperature" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); 
     
    static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) 
    {
        // handle message received 
    }