Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using uPLibrary.Networking.M2Mqtt;
- namespace m2mqtt
- {
- class Program
- {
- private const int BrokerPort = 15240;
- private const string Host = "farmer.cloudmqtt.com";
- private const string Username = "cizoylax";
- private const string Password = "TlpiHKqgu2Rd";
- static void Main(string[] args)
- {
- var topicName = "myFunTopic";
- var client = new MqttClient(Host, BrokerPort, false, MqttSslProtocols.None, null, null);
- client.Connect(Guid.NewGuid().ToString(), Username, Password);
- client.Publish(topicName, UTF8Encoding.UTF8.GetBytes("Hello from console!"), 1, true);
- client.Subscribe(new[] { topicName }, new byte[] { 1});
- client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
- Console.ReadKey();
- }
- private static void Client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
- {
- Console.WriteLine(sender.ToString());
- Console.WriteLine($"{UTF8Encoding.UTF8.GetString(e.Message)} received from {e.Topic}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement