zoukankan      html  css  js  c++  java
  • M2Mqtt is a MQTT client available for all .Net platform

    Introduction

    M2Mqtt is a MQTT client available for all .Net platform (.Net Framework, .Net Compact Framework and .Net Micro Framework) for Internet of Things and M2M communication.

    MQTT, short for Message Queue Telemetry Transport, is a light weight messaging protocol that enables embedded devices with limited resources to perform asynchronous communication on a constrained network.

    MQTT protocol is based on publish/subscribe pattern so that a client can subscribe to one or more topics and receive messages that other clients publish on these topics.

    This sample is a library contains an MQTT client that you can use to connect to any MQTT broker. It is developed in C# language and works on all the following .Net platforms :

    • .Net Framework (up to 4.5)
    • .Net Compact Framework 3.5 & 3.9 (for Windows Embedded Compact 7 / 2013)
    • .Net Micro Framework 4.2 & 4.3
    • Mono (for Linux O.S.)

    It can be used on Windows O.S, Windows Embedded Compact 7 / 2013 and Linux O.S. (thanks to Mono Project).

    Official web site on CodePlex : https://m2mqtt.codeplex.com/

    Building the Sample

    The library is available for the following solution and project files :

    • M2MqttVS2012.sln : solution for Visual Studio 2012 that contains projects file for .Net Framework, .Net Compact Framework 3.9, .Net Micro Framework 4.2 and .Net Micro Framework 4.3;
    • M2MqttVS2010.sln : solution for Visual Studio 2010 that contains projects file for .Net Framework and .Net Micro Framework 4.2;
    • M2MqttVS2008.sln : solution for Visual Studio 2008 that contains project file for .Net Compact Framework 3.5;
    • M2MqttMono.sln : solution for MonoDevelop for building project under Linux O.S. with Mono Project;

    In this sample there is only the Visual Studio 2012 solution but on the CodePlex web site https://m2mqtt.codeplex.com/ all other versions are available.

    To build sample based on .Net Micro Framework (4.2 and 4.3) you need to download .Net Micro Framework SDK from the official CodePlex web site : https://netmf.codeplex.com/

    To build sample based on .Net Compact Framework 3.9 you need to download Application Builder for Windows Embedded Compact 2013 from here : http://www.microsoft.com/en-us/download/details.aspx?id=38819

    Description

    The M2Mqtt library provides a main class MqttClient that represents the MQTT client to connect to a broker. You can connect to the broker providing its IP address or host name and optionally some parameters related to MQTT protocol.

    After connecting to the broker you can use Publish() method to publish a message to a topic and Subscribe() method to subscribe to a topic and receive message published on it. The MqttClient class is events based so that you receive an event when a message is published to a topic you subscribed to. You can receive event when a message publishing is complete, you have subscribed or unsubscribed to a topic.

    Following an example of client subscriber to a topic :

    C#
    Edit|Remove
    ... 
     
    // create client instance 
    MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS)); 
     
    // register to message received 
    client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; 
     
    string clientId = Guid.NewGuid().ToString(); 
    client.Connect(clientId); 
     
    // subscribe to the topic "/home/temperature" with QoS 2 
    client.Subscribe(new string[] { "/home/temperature" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); 
     
    ... 
     
    static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) 
    { 
    // handle message received 
    } 
    

    Following an example of client publisher to a topic : 

    C#
    Edit|Remove
    ... 
     
    // create client instance 
    MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS)); 
     
    string clientId = Guid.NewGuid().ToString(); 
    client.Connect(clientId); 
     
    string strValue = Convert.ToString(value); 
     
    // publish a message on "/home/temperature" topic with QoS 2 
    client.Publish("/home/temperature", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE); 
     
    
  • 相关阅读:
    深度学习python的配置(Windows)
    D02-R语言基础学习
    D01-R语言基础学习
    D17——C语言基础学PYTHON
    D16——C语言基础学PYTHON
    D15——C语言基础学PYTHON
    python packaging
    D14——C语言基础学PYTHON
    D13——C语言基础学PYTHON
    python-----多进程笔记
  • 原文地址:https://www.cnblogs.com/endv/p/7499588.html
Copyright © 2011-2022 走看看