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.
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}");
}
}
}