Network & Protocols
  • Introduction
  • Chapter1: protocol for industrial
    • 1.1.modbus
  • Chapter2: Protocol for IOT
    • 2.1.MQTT
      • 2.1.1.Introduction: What is MQTT?
      • 2.1.2.Install mosquitto on Windows 7
      • 2.1.3.Install M2Mqtt by Nuget, Example code
  • Chapter3: Protocol for network
    • 3.1.網路模型與協定
    • 3.2.HTTP
      • 3.2.1.Basic concept
      • 3.2.2.Session & Cookie
    • 3.3.TCP/IP
    • 3.4.UDP
    • 3.5.子網路(subnet)
    • 3.6.子網路間的通訊
    • 3.7.路由
  • Chapter4: Protocol for Wireless network
    • 4.1.BLE, IBeacon
  • Chapter5: Cellular and Wifi
    • 5.1.Cellular module
      • 5.1.1.AT Command
      • 5.1.2.各家常用模組
        • 5.1.2.1.Cinterion
          • 5.1.2.1.1.PLS8-E
          • 5.1.2.1.2.PLS8-X
          • 5.1.2.1.3.PLS8-US
        • 5.1.2.2.Sierra
          • 5.1.2.2.1.MC-7354
        • 5.1.2.3.Telit
          • 5.1.2.3.1.LE910-EUG
          • 5.1.2.3.2.LE910-NAG
      • 5.1.3.用儀器測試Throughput
    • 5.2.MBIM for Windows
    • 5.3.Wireshark
      • 5.3.1.Capture filter
      • 5.3.2.Cli command
      • 5.3.3.Monitor mode
      • 5.3.4.Work with ssh
    • 5.4.LTE
      • 5.4.1.Introduction
      • 5.4.2.Types of networks
      • 5.4.3.Elements of access and core networks
      • 5.4.4.What's LTE different?
      • 5.4.5.LTE訊息傳輸
      • 5.4.6.3GPP (Generation Partnership Project)
      • 5.4.7.FDD, TDD, Half Duplex FDD
      • 5.4.8.Beared Types
Powered by GitBook
On this page

Was this helpful?

  1. Chapter2: Protocol for IOT
  2. 2.1.MQTT

2.1.3.Install M2Mqtt by Nuget, Example code

  • 1.Requirement: .NET Framework 4.5

  • 2.Open Nuget console and input "Install-Package M2Mqtt"

  • 3.Example code:

    • 1.Publisher:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading;
      using System.Threading.Tasks;
      using uPLibrary.Networking.M2Mqtt;
      using uPLibrary.Networking.M2Mqtt.Messages;
      
      namespace MQTTPublisher
      {
          class Program
          {
              static void Main(string[] args)
              {
                  var id = args[0];
                  var topic = args[1];
      
                  MqttClient client = new MqttClient("127.0.0.1");
                  //若有輸入user, password, 接收端也要一樣的user, password
                  client.Connect(id);
      
                  //若要知道有沒有發送出去可以增加監聽發送後的Event
                  client.MqttMsgPublished += new MqttClient.MqttMsgPublishedEventHandler(client_MqttMsgPublished);
                  Stopwatch sw = new Stopwatch();
                  sw.Start();
                  for (int i = 0; i < 10000; i++)
                  {
                      //publish 最後一的parameter 是retain, 會決定是否保留著最後一個message 
                      ushort t = client.Publish(topic, Encoding.UTF8.GetBytes("XXXXXtestmessage, "
            + Guid.NewGuid().ToString() + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + ", times: " + i),
                      MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, true);
                      //
                      Thread.Sleep(1000);
                  }
                  sw.Stop();
                  Console.WriteLine("花費時間..... {0}", sw.Elapsed);
                  //Console.ReadLine();
                  client.Disconnect();

              }
              private static void client_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e)
              {
                  Console.WriteLine("Message Published");
                  Console.WriteLine(e.IsPublished);
                  Console.WriteLine(e.MessageId);
              }
          }
      }
  • 2.Subscriber:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading;
        using System.Threading.Tasks;
        using uPLibrary.Networking.M2Mqtt;
        using uPLibrary.Networking.M2Mqtt.Messages;

        namespace MQTTSubscriber
        {
            class Program
            {
                static void Main(string[] args)
                {
                    var id = args[0];
                    var topic = args[1];

                    MqttClient client = new MqttClient("127.0.0.1");

                    //如果有出現相同的id 會把前一個id 給踢掉
                    //要與發送端相同
                    client.Connect(id);

                    //topic 可以使用 topic1/#, topic2/+ 去接收相似的topic ,有點類似 sql like 的功能但限制較多
                    string[] topic2 = { topic };

                    byte[] qoslevels = { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE };

                    client.Subscribe(topic2, qoslevels);

                        client.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(client_PublishArrived);


                    //client.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(client_PublishArrived);

                    //Thread.Sleep(100000);
                    Console.ReadLine();
                    client.Disconnect();
                }
                private static void client_PublishArrived(object sender, MqttMsgPublishEventArgs e)
                {
                    Console.WriteLine("Message Received");
                    Console.WriteLine(e.Topic);
                    Console.WriteLine(Encoding.UTF8.GetString(e.Message));
                }
            }
        }
  • 4.心得:

    • 1.Publisher與Subscriber的ID不要相同

    • 2.Publisher與Subscriber的Topic要相同

Previous2.1.2.Install mosquitto on Windows 7NextChapter3: Protocol for network

Last updated 5 years ago

Was this helpful?