I am using this link => How to test the `Mosquitto` server? . for communicating data between two command prompt. it is worked perfectly. Now I am using windows application for communication between device and server. i am using the reference https://gist.github.com/cwschroeder/7b5117dca561c01def041e7d4c6d2771
I am using the code in command prompt for subscribe is given below
mosquitto_sub -v -t 'test/topic'
Iam using the following code to publish(mqtt)
public Form1()
{
InitializeComponent();
// string BrokerAddress = "test.mosquitto.org";
string BrokerAddress = "localhost";
client = new MqttClient(BrokerAddress);
// register a callback-function (we have to implement, see below) which is called by the library when a message was received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
// use a unique id as client id, each time we start the application
clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
}
private void btnPublish_Click(object sender, EventArgs e)
{
if (txtTopicPublish.Text != "")
{
// whole topic
string Topic = "/ElektorMyJourneyIoT/" + txtTopicPublish.Text + "/test";
MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
// publish a message with QoS 2
client.Publish(Topic, Encoding.UTF8.GetBytes(txtPublish.Text), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("mosquitto_pub -t 'test/topic' -m 'xyz' ");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
//cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
//Console.ReadKey();
}
else
{
MessageBox.Show("You have to enter a topic to publish!");
}
}
From the above code, you can see manually written commands for passing commands to command prompt. which is cmd.StandardInput.WriteLine("mosquitto_pub -t 'test/topic' -m 'xyz' ");
I need to publish message without manual code. need to write publish through .net.