1

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.

Noufal P
  • 67
  • 1
  • 7
  • 1
    `client.Publish` -- doesn't that do what you're asking for? – canton7 Feb 26 '21 at 13:24
  • hai. iam using the reference https://gist.github.com/cwschroeder/7b5117dca561c01def041e7d4c6d2771 – Noufal P Feb 26 '21 at 13:26
  • 1
    Right, and the call to `client.Publish` should publish the given message to the given topic. Isn't that the answer to your question? That's how you publish messages to MQTT without invoking mosquitto_pub – canton7 Feb 26 '21 at 13:29
  • That is iam asking. need solution for this. ie; How to publish messages to MQTT with mosquitto_pub in .net – Noufal P Feb 26 '21 at 14:03
  • 1
    I don't follow the question. You code does *both* using a proper .NET library to publish messages, and also shelling out to mosquitto_pub (which is bad). You do both. What's your question? – canton7 Feb 26 '21 at 14:20
  • You DON'T use `mosquitto_pub` from within another application. You use hte client library to do it. – hardillb Feb 26 '21 at 15:26

0 Answers0