# Connect to Deployments with C# SDK

C# (opens new window) is an object-oriented programming language provided by Microsoft that runs on .NET Framework.

This article mainly introduces how to use the paho.mqtt.m2mqtt client library in a C# project to connect, subscribe, publish and receive messages from the client to the MQTT server.

# Project initialization

This project uses .NET 5.0 to develop and test. You can use the following commend to confirm the .NET version.

~ dotnet --version            
5.0.301
1
2

# Choose the MQTT client

paho.mqtt.m2mqtt (opens new window) is a MQTT client available for all .NET platforms with support for both MQTT v3.1 and v3.1.1.

# .NET CLI install M2Mqtt

Use the following command in the root of the project to install M2Mqtt.

dotnet add package M2Mqtt --version 4.3.0
1

# The use of C# MQTT

# Connect to the MQTT broker

This article will use the free public MQTT broker (opens new window) provided by EMQ X. This service is created based on MQTT IoT cloud platform (opens new window) to create. The accessing information of the broker is as follows:

  • Broker: broker.emqx.io
  • TCP Port: 1883
  • Websocket Port: 8083

# Import the M2Mqtt

using uPLibrary.Networking.M2Mqtt;
1

# Set the parameter of MQTT Broker connection

Set the address, port and topic of MQTT Broker connection. At the same time, we call the C# Guid.NewGuid() to randomly generate uid as the MQTT client id.

string broker = "broker.emqx.io";
int port = 1883;
string topic = "Csharp/mqtt";
string clientId = Guid.NewGuid().ToString();
// If the broker requires authentication, set the username and password
string username = "emqx";
string password = "public";
1
2
3
4
5
6
7

# Write the MQTT connect method

Write static class method ConnectMQTT to create an MQTT client and connect it to the specified broker. We can determine whether the client is connected successfully according to the client's property of IsConnected. In the end, the client is returned.

static MqttClient ConnectMQTT(string broker, int port, string clientId, string username, string password)
{
    MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null);
    client.Connect(clientId, username, password);
    if (client.IsConnected)
    {
        Console.WriteLine("Connected to MQTT Broker");
    }
    else
    {
        Console.WriteLine("Failed to connect");
    }
    return client;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Publish messages

We define a while loop. In this loop we will set the MQTT client Publish method to publish messages to the specified topic every second.

static void Publish(MqttClient client, string topic)
{
    int msg_count = 0;
    while (true)
    {
        System.Threading.Thread.Sleep(1*1000);
        string msg = "messages: " + msg_count.ToString();
        client.Publish(topic, System.Text.Encoding.UTF8.GetBytes(msg));
        Console.WriteLine("Send `{0}` to topic `{1}`", msg, topic);
        msg_count++;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# Subscribe to a topic

Write the static method client_MqttMsgPublishReceived. This method will be called after the client received messages from the MQTT Broker. In this method, we will print the topic and payload of the messages.

static void Subscribe(MqttClient client, string topic)
{
    client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
    client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
}
static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
    string payload = System.Text.Encoding.Default.GetString(e.Message);
    Console.WriteLine("Received `{0}` from `{1}` topic", payload, e.Topic.ToString());
}
1
2
3
4
5
6
7
8
9
10

# The complete code

using System;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace csharpMQTT
{
    class Program
    {
        static MqttClient ConnectMQTT(string broker, int port, string clientId, string username, string password)
        {
            MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null);
            client.Connect(clientId, username, password);
            if (client.IsConnected)
            {
                Console.WriteLine("Connected to MQTT Broker");
            }
            else
            {
                Console.WriteLine("Failed to connect");
            }
            return client;
        }

        static void Publish(MqttClient client, string topic)
        {
            int msg_count = 0;
            while (true)
            {
                System.Threading.Thread.Sleep(1*1000);
                string msg = "messages: " + msg_count.ToString();
                client.Publish(topic, System.Text.Encoding.UTF8.GetBytes(msg));
                Console.WriteLine("Send `{0}` to topic `{1}`", msg, topic);
                msg_count++;
            }
        }

        static void Subscribe(MqttClient client, string topic)
        {
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
        }
        static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            string payload = System.Text.Encoding.Default.GetString(e.Message);
            Console.WriteLine("Received `{0}` from `{1}` topic", payload, e.Topic.ToString());
        }

        static void Main(string[] args)
        {
            string broker = "broker.emqx.io";
            int port = 1883;
            string topic = "Csharp/mqtt";
            string clientId = Guid.NewGuid().ToString();
            string username = "emqx";
            string password = "public";
            MqttClient client = ConnectMQTT(broker, port, clientId, username, password);
            Subscribe(client, topic);
            Publish(client, topic);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

# Test

Run the code, the console output is as follows. c_sharp_connect