MQTT Client for mbed LPC1768 and Application Board over Ethernet; publish only.
Dependencies: C12832 MMA7660 MQTT
Fork of HelloMQTT by
You are viewing an older revision! See the latest version
CspWpfClient
Ein C#-Projekt egal ob Forms oder Wpf benötigt die MQTT Software, die mittels Projekt -> NuGet-Paketmanager installiert werden kann:
Folgende Namespaces sind einzubinden:
using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages;
Ein globale Variable für den Client wird definiert:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } MqttClient client;
Die Verbindung mit dem Broker erfolgt z.B. im Button btnConnect:
private void btnConnect_Click(object sender, RoutedEventArgs e) { client = new MqttClient("broker.hivemq.com"); string cl_str = "SimpleClientCsp" + RandomString(4); // Client-Id mit Zuffalssstring client.Connect(cl_str); client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; // Messages handler ist zu erzeugen }
Erzeuge MQTT Messages handler für das Empfangen von Nachrichten:
private void client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e) { // handle message received Debug.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic); String str = Encoding.UTF8.GetString(e.Message); string[] entries = str.Split('/'); // Format: 25.4.2017/12:23/21 Debug.WriteLine("String splitted:" + entries[0] +" "+ entries[1] + " " + entries[2] + "\n"); // datum/zeit/wert https://stackoverflow.com/questions/11625208/accessing-ui-main-thread-safely-in-wpf Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { //Update UI here txtReceived.AppendText(str); txtReceived.AppendText("\n"); // txtReceived.AppendText(Encoding.UTF8.GetString(e.Message) + "\r\n"); txtReceived.ScrollToLine(txtReceived.LineCount - 1); })); }