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
  1. ... 
  2.  
  3. // create client instance 
  4. MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS)); 
  5.  
  6. // register to message received 
  7. client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; 
  8.  
  9. string clientId = Guid.NewGuid().ToString(); 
  10. client.Connect(clientId); 
  11.  
  12. // subscribe to the topic "/home/temperature" with QoS 2 
  13. client.Subscribe(new string[] { "/home/temperature" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); 
  14.  
  15. ... 
  16.  
  17. static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) 
  18. { 
  19. // handle message received 
  20. } 

Following an example of client publisher to a topic : 

C#
Edit|Remove
  1. ... 
  2.  
  3. // create client instance 
  4. MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS)); 
  5.  
  6. string clientId = Guid.NewGuid().ToString(); 
  7. client.Connect(clientId); 
  8.  
  9. string strValue = Convert.ToString(value); 
  10.  
  11. // publish a message on "/home/temperature" topic with QoS 2 
  12. client.Publish("/home/temperature", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE); 
  13.  

M2Mqtt is a MQTT client available for all .Net platform的更多相关文章

  1. python mqtt client publish操作

    使用Python库paho.mqtt.client 模拟mqtt client 连接broker,publish topic. #-*-coding:utf-8-*- import paho.mqtt ...

  2. mqtt client python example

    This is a simple example showing how to use the [Paho MQTT Python client](https://eclipse.org/paho/c ...

  3. go ---MQTT client

    Paho GO Client   语言 GO 协议 EPL AND EDL 官网地址 http://www.eclipse.org/paho/ API类型 Asynchronous  描述 Paho ...

  4. mqtt client api: 阻塞API

    fusesource版本:mqtt-client-1.11.jar下载地址:https://github.com/fusesource/mqtt-client fusesource提供三种mqtt c ...

  5. MQTT Client library for C (MQTT客户端C语言库-paho)

    原文:http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/index.html 来自我的CSDN博客   最近在使用Paho的MQTT客 ...

  6. MQTT Server搭建(apache-apollo)和MQtt Client搭建

    目标 本文就MQTT server和client搭建做以下总结,方便测试及开发使用,能基于MQTT软件发送和接收消息. 介绍 MQTT是基于tcp的消息发送,目前JAVA方面有两种实现,分别是mqtt ...

  7. php mqtt client

    <?php /* phpMQTT */ class phpMQTT { private $socket; /* holds the socket */ private $msgid = 1; / ...

  8. Asynchronous MQTT client library for C (MQTT异步客户端C语言库-paho)

    原文:http://www.eclipse.org/paho/files/mqttdoc/MQTTAsync/html/index.html MQTT异步客户端C语言库   用于C的异步 MQTT 客 ...

  9. 一套权威的 MQTT Client 库

    主流的语言都支持,可链接到 github ,亲测golang client 简单好用 http://www.eclipse.org/paho/downloads.php

随机推荐

  1. BZOJ 3172 [Tjoi2013]单词 AC自动机Fail树

    题目链接:[http://www.lydsy.com/JudgeOnline/problem.php?id=3172] 题意:给出一个文章的所有单词,然后找出每个单词在文章中出现的次数,单词用标点符号 ...

  2. php最简单最基础入门笔记

    偶然翻到之前刚学php时记录的笔记,特此分享给大家,希望对初学者有所帮助. php网页命名不支持中文 isset($abc)   判断变量是否被定义 empty($abc)    判断变量是否为空 u ...

  3. IIS Server Farms入门

    概念了解 IIS Server Farms,实际上应该叫“Microsoft Web Farm Framework (WFF)”,依赖于“Web Platform Installer”才能安装,依赖于 ...

  4. 【10.10校内测试】【线段树维护第k小+删除】【lca+主席树维护前驱后驱】

    贪心思想.将a排序后,对于每一个a,找到对应的删除m个后最小的b,每次更新答案即可. 如何删除才是合法并且最优的?首先,对于排了序的a,第$i$个那么之前就应该删除前$i-1$个a对应的b.剩下$m- ...

  5. HDU 5715 XOR 游戏 二分+字典树

    XOR 游戏 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5715 Description 众所周知,度度熊喜欢XOR运算(XOR百科). 今天,它 ...

  6. 如何让access空值变成0?(确切的说是让access Null值变成0)

    方法一 if  IsNull(Me.新_退休费) = True Then Me.新_退休费 = 0 方法二 if Nz(Me.原_退休费) = Me.原_退休费 Then Me.原_退休费 = 0 有 ...

  7. Fine Uploader + Spring3.2.2(Java+html5上传) SpringMVC+jquery-fineuploader 文件上传

    需求:要实现多文件上传,且要支持手机等移动设备... springmvc文件上传真头疼,网上搜了半天没发现都是TMD的用submit按钮提交到后台的,就没有插件的吗?最后发现了fineUploader ...

  8. java执行ping命令

    public static void get() throws IOException{ String address="10.132.118.110"; Process proc ...

  9. 《深入理解C指针》

    <深入理解C指针> 基本信息 原书名:Understanding and using C pointers 作者: (美)Richard Reese 译者: 陈晓亮 丛书名: 图灵程序设计 ...

  10. [Git] An efficient GIT workflow for mid/long term projects

    reference : http://fle.github.io/an-efficient-git-workflow-for-midlong-term-projects.html Our full-w ...