Skip to content

Instantly share code, notes, and snippets.

@KeesCBakker
Last active February 15, 2024 12:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeesCBakker/d8e093c237bf939004f830406cb35c09 to your computer and use it in GitHub Desktop.
Save KeesCBakker/d8e093c237bf939004f830406cb35c09 to your computer and use it in GitHub Desktop.
Settings to connect to MQTT using M2Mqtt (can be used with CloudMqtt)
using System.ComponentModel.DataAnnotations;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
var settings = AppSettingsProvider.Create<MqttSettings>();
var port = Convert.ToInt32(settings.Port);
MqttClient client = null;
if (settings.UseSecureConnection)
{
client = new MqttClient(settings.BrokerHostName, port, true, null, null, MqttSslProtocols.TLSv1_2);
}
else
{
client = new MqttClient(settings.BrokerHostName, port, false, null, null, MqttSslProtocols.None);
}
client.Connect(Guid.NewGuid().ToString(), settings.UserName, settings.Password);
client.Subscribe(new string[] { settings.Topic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
class MqttSettings
{
[Required]
public string BrokerHostName { get; set; }
[Required]
public uint Port { get; set; }
[Required]
public bool UseSecureConnection { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
[Required]
public string Topic { get; set; }
}
@ayman-metwally-developer

Excellent ... many thanks

@akashom53
Copy link

Surprising how few examples are present on the web for the m2mqtt client library. Thanks a LOT!

@ARKbr
Copy link

ARKbr commented Jan 6, 2019

"client = new MqttClient(settings.BrokerHostName, port, true, null, null, MqttSslProtocols.TLSv1_2);"

MANY THANKS! <3

@saritapp16
Copy link

Thanks! It worked for me!

@Ryota7101
Copy link

Thanks for the great information.

But I am in trouble.
I made mosquitto in docker a broker.

Suppose my PC IP address is "" 10.0.0.11 ".

I wrote code like this.

MqttClient client = new MqttClient ("10.0.0.11", 1883, false, null, null, MqttSslProtocols.TLSv1_2);

However, when the Connect method was executed, "isConnect" was false.

client.Connect (clientId);

If you specify the host name of HIVE MQ, it works.
MqttClient client = new MqttClient ("broker.mqttdashboard.com");

Please tell me how to enable connection by specifying IP address.

Thank you

@Wizzzo
Copy link

Wizzzo commented Jan 10, 2021

Hey, thank you very much for your solution!
I only wonder how do I retrieve the value that I receive from the subscribe I created?
mqttClient.Subscribe(subs, qoss);
now I want to get the values

@Chiranjeevi-J
Copy link

Thanks for the great information.

But I am in trouble.
I made mosquitto in docker a broker.

Suppose my PC IP address is "" 10.0.0.11 ".

I wrote code like this.

MqttClient client = new MqttClient ("10.0.0.11", 1883, false, null, null, MqttSslProtocols.TLSv1_2);

However, when the Connect method was executed, "isConnect" was false.

client.Connect (clientId);

If you specify the host name of HIVE MQ, it works.
MqttClient client = new MqttClient ("broker.mqttdashboard.com");

Please tell me how to enable connection by specifying IP address.

Thank you

IP Address can be used as followed
MqttClient client = new MqttClient (System.Net.IPAddress.Parse("10.0.0.11"), 1883, false, null, null, MqttSslProtocols.TLSv1_2);

Hope this helps

@cgtamayo
Copy link

Thanks!! it worked with hivemq!
URL: .s2.eu.hivemq.cloud
Port: 8883
new =MqttClient(brokerIpAddressVariable.Value,8883,true,null,null,MqttSslProtocols.TLSv1_2)

@YC-Michael
Copy link

Thanks!! it worked with hivemq! URL: .s2.eu.hivemq.cloud Port: 8883 new =MqttClient(brokerIpAddressVariable.Value,8883,true,null,null,MqttSslProtocols.TLSv1_2)

Hi, I'm unable to connect to hivemq cloud... did u simply parse in the URL?

@cgtamayo
Copy link

Thanks!! it worked with hivemq! URL: .s2.eu.hivemq.cloud Port: 8883 new =MqttClient(brokerIpAddressVariable.Value,8883,true,null,null,MqttSslProtocols.TLSv1_2)

Hi, I'm unable to connect to hivemq cloud... did u simply parse in the URL?

Yes, set the URL without port. Each console have an unique url you should access to your cluster and copy the one it's asigned to you.
https://console.hivemq.cloud/

MqttClient client = new MqttClient ("##########.s2.eu.hivemq.cloud");

@YC-Michael
Copy link

Thanks!! it worked with hivemq! URL: .s2.eu.hivemq.cloud Port: 8883 new =MqttClient(brokerIpAddressVariable.Value,8883,true,null,null,MqttSslProtocols.TLSv1_2)

Hi, I'm unable to connect to hivemq cloud... did u simply parse in the URL?

Yes, set the URL without port. Each console have an unique url you should access to your cluster and copy the one it's asigned to you. https://console.hivemq.cloud/

MqttClient client = new MqttClient ("##########.s2.eu.hivemq.cloud");

Oh, I have the url. I was referring to running M2Mqtt on Unity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment