MQTT-消息协议

网友投稿 761 2022-11-06

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

MQTT-消息协议

1、简介

MQTT(消息队列遥测传输)是ISO 标准下基于发布/订阅范式的消息协议。它工作在 TCP/IP协议族上,是为硬件性能低下的远程设备以及网络状况糟糕的情况下而设计的发布/订阅型消息协议。

Mosquitto是一款实现了消息推送协议 MQTT v3.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功耗传感器,手机、嵌入式计算机、微型控制器等移动设备。它具有强大的社区支持,并且易于安装和配置。

2、安装安装Mosquitto

CentOS 7 默认没有mosquitto包。要安装它,首先我们将安装一个额外的软件软件包,即 EPEL的额外软件存储库。该存储库中充满了可在CentOS,Red Hat和其他面向企业的Linux发行版上的软件。

1.使用yum软件包管理器安装epel-release软件包

[root@iZ2zeir6vcnpz8qw3t455tZ ~]# yum -y install epel-release

2.这会将EPEL存储库信息添加到我们的系统中。在整个过程中,-y选项会对几个问题自动回答“是”。现在我们可以安装mosquitto包了

[root@iZ2zeir6vcnpz8qw3t455tZ ~]# yum -y install mosquitto

3.启动服务

[root@iZ2zeir6vcnpz8qw3t455tZ ~]# systemctl start mosquitto [root@iZ2zeir6vcnpz8qw3t455tZ ~]# systemctl enable mosquitto Created symlink from etc/systemd/system/multi-user.target.wants/mosquitto.service to usr/lib/systemd/system/mosquitto.service.

3、使用mosquitto_sub订阅测试主题

[root@iZ2zeir6vcnpz8qw3t455tZ ~]# mosquitto_sub -h localhost -t test -t:是主题名称 -h:用于指定MQTT服务器的主机名 # 按ENTER后你将看不到输出,因为mosquitto_sub正在等待消息到达。

4、切换第一个终端并发布消息

[root@iZ2zeir6vcnpz8qw3t455tZ ~]# mosquitto_pub -h localhost -t test -m "hello world" -m:选项来指定我们的消息

5、使用Python 进行基于MQTT的物联网开发

1.使用pip安装另外一个库 paho-mqtt , 官网https://eclipse.org/paho/

The Eclipse Paho project provides open-source client implementations of MQTT and MQTT-SN messaging protocols aimed at new, existing, and emerging applications for the Internet of Things (IoT).

[root@iZ2zeir6vcnpz8qw3t455tZ ~]# pip install paho-mqtt DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/ Collecting paho-mqtt Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/59/11/1dd5c70f0f27a88a3a05772cd95f6087ac479fac66d9c7752ee5e16ddbbc/paho-mqtt-1.5.0.tar.gz (99kB)     |████████████████████████████████| 102kB 13.9MB/s  Building wheels for collected packages: paho-mqtt Building wheel for paho-mqtt (setup.py) ... done Stored in directory: root/.cache/pip/wheels/a4/f8/76/4460f078d0fb4689bc6dcd51b2a88ca491a83cb21cf74f46ca Successfully built paho-mqtt Installing collected packages: paho-mqtt Successfully installed paho-mqtt-1.5.0 WARNING: You are using pip version 19.1.1, however version 20.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.

2.使用paho-mqtt实现接收者

import paho.mqtt.client as mqtt  def on_message(client, userdata, msg):    '''处理message回调'''   print('topic: {}'.format(msg.topic))   print('message: {}'.format(str(msg.payload)))  # 建立一个MQTT的客户端 client =mqtt.Client() # 绑定数据接收回调函数 client.on_message =on_message  HOST_IP ='localhost'# Server的IP地址 HOST_PORT =1883# mosquitto 默认打开端口 TOPIC_ID ='pyespcar_basic_control'# TOPIC的ID  # 连接MQTT服务器 client.connect(HOST_IP, HOST_PORT, 60) # 订阅主题 client.subscribe(TOPIC_ID)  # 阻塞式, 循环往复,一直处理网络数据,断开重连 client.loop_forever()

3.使用paho-mqtt实现发布者

import paho.mqtt.client as mqtt import time  HOST_IP ='localhost'# Server的IP地址 HOST_PORT =1883# mosquitto 默认打开端口 TOPIC_ID ='pyespcar_basic_control'# TOPIC的ID  # 创建一个客户端 client =mqtt.Client() # 连接到服务器(本机) client.connect(HOST_IP, HOST_PORT, 60)  count =0 whileTrue:   count +=1    # 待发送的数据   message ='MOVE FRORWORD,{}'.format(count)       # 通过mqtt协议发布数据给server   client.publish(TOPIC_ID, message)    # 打印日志   print('SEND: {}'.format(message))    # 延时1s   time.sleep(1)

参考文献:https://cloud.tencent.com/developer/article/1180072

参考文献:http://1zlab.com/wiki/micropython-esp32/mqtt/

上一篇:软件测试培训之谈谈软件兼容性测试
下一篇:软件测试培训之八种状态增加测试用例状态的精确度
相关文章

 发表评论

暂时没有评论,来抢沙发吧~