C#: Failed to subscribe to MQTT server

  • Thread starter Thread starter YigalB
  • Start date Start date
Y

YigalB

Guest
The MQTT server (Mosquitto) is up and running (on Ubuntu machine). I can publish messages to the MQTT server from C# program from Win 10 Machine, yet it fails on runtime error:


uPLibrary.Networking.M2Mqtt.Exceptions.MqttClientException

The code is taken from an example.

Any idea?

The code is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
//using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace mqtt1
{
public partial class Form1 : Form
{

string IP_ADDR = "192.168.1.85";
int global_counter = 0;
string MQTT_TOPIC = "test";

public Form1()
{
InitializeComponent();
}

private void startMqtt_button_Click(object sender, EventArgs e)
{

MqttClient client = new MqttClient(IP_ADDR);

byte code = client.Connect(new Guid().ToString(), null, null, true, 10);
if (code == 0)
{
Console.WriteLine("port numbr : " + client.Settings.Port.ToString());
Console.WriteLine("ClientID : " + client.ClientId.ToString());
Console.WriteLine("Is connected? : " + client.IsConnected.ToString());
Console.WriteLine("Protocol version: " + client.ProtocolVersion.ToString());
}
else
{
Console.WriteLine("Failed to connect with " + code.ToString());
Console.WriteLine("Code is: " + code.ToString());
return;
}



// try to subscribe
// from Using MqttClient
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

string[] topic = { MQTT_TOPIC }; // list of topins to subscribe to
byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE };
client.Subscribe(topic, qosLevels);

int counter = 0;
Boolean stop_counting = false;

for (int i=0; i<1000 && stop_counting==false; i++)
{
ushort msgId = client.Publish(MQTT_TOPIC, Encoding.UTF8.GetBytes(i.ToString()), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
Console.WriteLine("msgId is: " + msgId.ToString() + "counter is: " + i.ToString());
Thread.Sleep(1000);

mqtt_msg_box.Text = counter.ToString();

global_counter += 1;

mqtt_msg_cnt_box.Text = global_counter.ToString();
Application.DoEvents();

if (stop_checkBox.Checked)
{
stop_counting = true;
}

}

Console.WriteLine("Finished. Numbe of iterations: " + global_counter.ToString());

client.Unsubscribe(topic);
client.Disconnect();
client = null;
}

private void MQtt_button2_Click(object sender, EventArgs e)
{

MqttClient client = new MqttClient(IP_ADDR);
byte code = client.Connect(new Guid().ToString(), null, null, true, 10);
client.Subscribe(new string[] { MQTT_TOPIC }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
Console.WriteLine("port numbr: " + client.Settings.Port.ToString());

}

private void exit_button_Click(object sender, EventArgs e)
{
this.Close();
}

private void publish_to_mqtt(string _in_str)
{

}

void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
Console.WriteLine("message recieved: " + e.Message.ToString() );
Console.WriteLine("message recieved: " + e.Topic.ToString());

// access data bytes throug e.Message
}

}
}

Continue reading...
 

Similar threads

P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
Back
Top