Resolved MQTT Not working in a WPS

dreamer76

Member
Joined
Mar 26, 2024
Messages
9
Programming Experience
1-3
I am new here,
I have been working on programming for a while.
I'm currently working on C#, but I already have some experience with PHP.
Now I'm trying to send something via MQTT.
I have a small script that runs through Visula Studio 2022 and a console app. this works and I see a topic in MQTT explorer that I am sending. now I want to send the mqtt topic via a WPFAPP. I don't get an error message, but I also don't see anything being sent in MQTT explorer. can anyone help me on my way?
Here is the code.



C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Mqtt();
            string brokerAddress = "Adrea server";
            string clientId = "client1";
            string topic = "topic";

            MqttClient client = new MqttClient(brokerAddress);
            client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived; // Event handler toevoegen

            client.Connect(clientId);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (RadioButton1.IsChecked == true)
            {
                Book book1 = new Book("Ik druk op de knop titel en er werkt iets", "ik druk op de knop groep en er werkt iets", 200);
               // Console.WriteLine(book1.title);
                // book1.title
                MessageBox.Show(book1.title);
              //  MessageBox.Show("We gaan nou starten");
                //Process.Start("https://www.beveiligingswinkel.nl/");
                //Process.Start("notepad.exe");

            }
          
            else if (RadioButton2.IsChecked == true)
            {
                MessageBox.Show("Goodbye");

            }
            else
            {
                Mqtt();
             //   Client_MqttMsgPublishReceived();
                MessageBox.Show("Message BOX");

            }
        }


        //private void Client_MqttMsgPublishReceived()
        //{
        //    throw new NotImplementedException();

        //}

        private void True(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(book1.title);
            //Process.Start("notepad.exe");
            Book book1 = new Book("Ik druk op de knop titel en er werkt iets", "ik druk op de knop groep en er werkt iets", 200);
            // Console.WriteLine(book1.title);
            // book1.title
            MessageBox.Show(book1.title);
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            Book book1 = new Book("Ik druk op de knop titel en er werkt iets", "ik druk op de knop groep en er werkt iets", 200);
            // Console.WriteLine(book1.title);
            // book1.title
            MessageBox.Show(book1.author);

            // Process.Start("paint.exe");
            //Process.Start(@"C:\Users\Rein\Downloads\Diagram1.pdf");
        }

        private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
        {
            Book book1 = new Book("Ik druk op de knop titel en er werkt iets", "ik druk op de knop groep en er werkt iets", 200);
            // Console.WriteLine(book1.title);
            // book1.title
            MessageBox.Show(book1.pages.ToString());

            // Process.Start("paint.exe");
            //Process.Start(@"C:\Users\Rein\Downloads\Diagram1.pdf");

        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

      

        public void Mqtt()
        {
            MessageBox.Show("MQTT versturen gestart");
            // Plaats hier de logica van je MQTT-functionaliteit
            string brokerAddress = "192.168.4.33";
        string clientId = "client1";
        string topic = "onderwerp";

        MqttClient client = new MqttClient(brokerAddress);
        client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;

        client.Connect(clientId);
            MessageBox.Show("MQTT verstuurd");// + brokerAddress + topic + clientId + client);

            // Je kunt hier eventueel ook een bericht publiceren als je dat wilt
            // client.Publish(topic, Encoding.UTF8.GetBytes("Testbericht"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
        }

    // Event handler voor ontvangen berichten
    private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
    {
        // Ontvangen bericht weergeven
        string message = Encoding.UTF8.GetString(e.Message);
        MessageBox.Show($"Ontvangen bericht op onderwerp '{e.Topic}': {message}");
    }

}
}
 
As a quick aside. In C#, code is called "code", not "script".

If your console code was working and you just transplanted that code into WPF, it should still continue to work. Have you stepped through your code with a debugger to make sure things are being called and in the correct sequence? Just scanning through your code, it looks ilke you are trying to use MessageBox.Show() to use as a poor man's debugger.
 
thank you Skydiver for your response.
I added mesagebox.Show() to get a little more information. as I already indicated. I've done everything at console level so far.
when I do debugging I don't get an error message. but like I said I'm learning C# and visual studio
 
Okay. I would still suggest learning how to use the debugger. It is a better tool for the job and will give you a lot more information.

So which message boxes are you seeing show up? Did they show up in the correct order? Which message boxes were you expecting to see and did not show up?

(This may or may not be related to your problem: Doing a quick Google search, it looks like that MQTT client library you are using seems to be using multiple threads. WPF (like WinForms), usually expects any UI interaction to be done on its UI thread. Message boxes are a form of UI interaction, albeit, they often have less thread affinity.)

Why do you code which creates and uses a MQTT client in your method named Mqtt() and also have similar code in the constructor? Why are the addresses passed in different?

Why are you doing this at construction time instead of as a reactive to a button click or when the UI is first visible?
 
It looks like you aren't actually publishing anything. Line 130 seems to be commented out. What were you expecting to see in the MQTT Explorer?

Also, can you share with us your working console app?
 

Latest posts

Back
Top Bottom