-
-
Save KeesCBakker/d8e093c237bf939004f830406cb35c09 to your computer and use it in GitHub Desktop.
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; } | |
} |
Surprising how few examples are present on the web for the m2mqtt client library. Thanks a LOT!
"client = new MqttClient(settings.BrokerHostName, port, true, null, null, MqttSslProtocols.TLSv1_2);"
MANY THANKS! <3
Thanks! It worked for me!
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
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
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
Thanks!! it worked with hivemq!
URL: .s2.eu.hivemq.cloud
Port: 8883
new =MqttClient(brokerIpAddressVariable.Value,8883,true,null,null,MqttSslProtocols.TLSv1_2)
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?
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");
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.
Excellent ... many thanks