MQTT消息协议、服务器及其客户端

    MQTT是一个轻量级的消息协议。从2014年12月IOIT大会上得到的消息,该协议已经被OASIS标准组织接收,成立了专门的工作组,以意味着该规范正式走向了标准化之路。

    目前MQTT的标准组织官网:http://www.mqtt.org,里面列出了很多支持的软件相关资源。

    一个轻量级的MQTT服务器是:http://www.mosquitto.org,可以运行ARM/MIPS的嵌入式linux系统上,如OpenWRT。

    很多客户端模块现在被Eclipse基金会接管,发展很快。所有的语言支持客户端在这里:http://git.eclipse.org/c/paho/

    下面介绍基于python的MQTT的客户端的安装:

1git clone http://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.python.git 2cd org.eclipse.paho.mqtt.python 3sudo python setup.py install

    注意,如果使用Sublime,最好到里面的控制台进行安装,缺省运行在独立的环境中,跟系统的控制台安装的程序不一样。

    然后,发送消息:

1#!/usr/bin/python 2# This shows a service of an MQTT subscriber. 3# Copyright (c) 2010-2015, By openthings@163.com. 4 5import sys 6import datetime 7import socket, sys 8import paho.mqtt.publish as publish 9 10def transmitMQTT(strMsg): 11    #strMqttBroker = "localhost" 12    strBroker = "112.124.67.178" 13    strMqttChannel = "/inode/mychannel" 14    print(strMsg) 15    publish.single(strMqttChannel, strMsg, hostname = strMqttBroker) 16 17if __name__ == '__main__': 18    transmitMQTT("Hello,MQTT Proxy, I am client inside python.") 19    print "Send msg ok."

创建一个MQTT服务器程序:

1#!/usr/bin/python 2# This shows a service of an MQTT subscriber. 3# Copyright (c) 2010-2015, By openthings@163.com. 4 5import sys 6import datetime 7import socket, sys 8 9#======================================================         10#MQTT Initialize.-------------------------------------- 11try: 12    import paho.mqtt.client as mqtt 13except ImportError: 14    print("MQTT client not find. Please install as follow:") 15    print("git clone http://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.python.git") 16    print("cd org.eclipse.paho.mqtt.python") 17    print("sudo python setup.py install") 18 19#====================================================== 20def on_connect(mqttc, obj, rc): 21    print("OnConnetc, rc: "+str(rc)) 22 23def on_publish(mqttc, obj, mid): 24    print("OnPublish, mid: "+str(mid)) 25 26def on_subscribe(mqttc, obj, mid, granted_qos): 27    print("Subscribed: "+str(mid)+" "+str(granted_qos)) 28 29def on_log(mqttc, obj, level, string): 30    print("Log:"+string) 31 32def on_message(mqttc, obj, msg): 33    curtime = datetime.datetime.now() 34    strcurtime = curtime.strftime("%Y-%m-%d %H:%M:%S") 35    print(strcurtime + ": " + msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) 36    on_exec(str(msg.payload)) 37 38def on_exec(strcmd): 39    print "Exec:",strcmd 40    strExec = strcmd 41     42#===================================================== 43if __name__ == '__main__':  44    mqttc = mqtt.Client("mynodeserver") 45    mqttc.on_message = on_message 46    mqttc.on_connect = on_connect 47    mqttc.on_publish = on_publish 48    mqttc.on_subscribe = on_subscribe 49    mqttc.on_log = on_log 50 51    #strBroker = "localhost" 52    strBroker = "112.124.67.178" 53 54    mqttc.connect(strBroker, 1883, 60) 55    mqttc.subscribe("/inode/mychannel", 0) 56    mqttc.loop_forever()
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )