Last active
May 26, 2016 14:24
-
-
Save bertt/e7f30456a44e4e138ebf784bcddefad9 to your computer and use it in GitHub Desktop.
Gost and MQTT in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using uPLibrary.Networking.M2Mqtt; | |
using uPLibrary.Networking.M2Mqtt.Messages; | |
namespace MqttClient_Net | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var server = "gost.geodan.nl"; | |
var topic = "Datastreams(1)/Observations"; | |
var message = "{\"phenomenonTime\": \"2016 - 05 - 09T11: 04:15.790Z\", \"result\": 20 }"; | |
Console.WriteLine("Gost - MQTT Client"); | |
var client = new MqttClient(server); | |
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; | |
var clientId = Guid.NewGuid().ToString(); | |
client.Connect(clientId); | |
Console.WriteLine("Listening...."); | |
// subscribe | |
client.Subscribe(new[] { topic },new[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); | |
// and publish something... | |
client.Publish("GOST/"+topic, Encoding.Default.GetBytes(message)); | |
} | |
private static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) | |
{ | |
var str = Encoding.Default.GetString(e.Message); | |
Console.WriteLine("result: " + str); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment