博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
paho-mqtt
阅读量:6889 次
发布时间:2019-06-27

本文共 2274 字,大约阅读时间需要 7 分钟。

mqtt 参考:

https://pypi.org/project/paho-mqtt/

https://github.com/eclipse/paho.mqtt.python

 

#服务端

[root@localhost ~]# cat mqtt_server.py

import paho.mqtt.client as mqttimport paho.mqtt.publish as publish idx = 0 #往paho/temperature 一直发送内容while True:    print("send success")    publish.single("paho/temperature",               payload="this is message:%s"%idx,               hostname="iot.eclipse.org",               client_id="lora1",               # qos = 0,               # tls=tls,               port=1883,               protocol=mqtt.MQTTv311)    idx += 1

#客户端

[root@localhost ~]# cat mqtt_client.py 

import paho.mqtt.client as mqtt # The callback for when the client receives a CONNACK response from the server.def on_connect(client, userdata, flags, rc):    print("Connected with result code "+str(rc))  # The callback for when a PUBLISH message is received from the server.def on_message(client, userdata, msg):    #在这里处理业务逻辑    print(msg.topic+" "+str(msg.payload)) client = mqtt.Client()client.on_connect = on_connectclient.on_message = on_message client.connect("iot.eclipse.org", 1883, 60) #订阅频道client.subscribe("paho/temperature") # Blocking call that processes network traffic, dispatches callbacks and# handles reconnecting.# Other loop*() functions are available that give a threaded interface and a# manual interface.client.loop_forever()

#启动服务端,再启动客户端

#服务端一直发送信息[root@localhost ~]# python3 mqtt_server.py send successsend successsend successsend successsend successsend successsend successsend successsend successsend successsend success#客户端接收信息[root@localhost ~]# python3 mqtt_client.py Connected with result code 0paho/temperature b'this is message:125'paho/temperature b'this is message:126'paho/temperature b'this is message:127'paho/temperature b'this is message:128'paho/temperature b'this is message:129'paho/temperature b'this is message:130'

备注:

MQTT服务器不负责存储数据,需要编写额外的接收客户端来接收数据、分析、入库等。

MQTT服务器用的是iot.eclipse.org,如果碰巧两个人在用同一个频道,那可能收到别人的消息哦~

 

 搭建mqtt服务器

参考:

http://emqtt.com/docs/v2/install.html

https://blog.csdn.net/qq_37258787/article/details/79776663

 

二、Mosquitto安装和使用

 https://lanseyujie.com/post/mosquitto-installation-and-usage.html

 

三、安装mqttfx(用于测试mqtt-服务器)

相当于部署一个mqtt-server

软件下载:

使用方法参考:

https://blog.csdn.net/nicholaszao/article/details/79211965

 

 

 

 

 

 

 

你可能感兴趣的文章
Xcode的代码块和代码块注释
查看>>
json.net使用说明四
查看>>
0.1:Why are We Addicted to Games
查看>>
Redis
查看>>
open jdk卸载
查看>>
js - AO链 与 function
查看>>
Java CLASSPATH 引发的问题
查看>>
数学运算类(三角函数,取整函数,指数函数,取最大值,最小值,绝对值)...
查看>>
Java并发编程的艺术(笔记)
查看>>
【PIC单片机】Pic单片机基础知识
查看>>
软件测试--实际项目的实际操作过程--免费资料
查看>>
go 编释流程
查看>>
agsxmpp 和 openfire 服务器 通信
查看>>
servlet 解决中午乱码
查看>>
ios项目绕过证书访问https程序
查看>>
几乎所有编程语言的hello, world程序(3)
查看>>
CentOs 设置静态IP 方法
查看>>
Ubuntu 16.04源码编译安装nginx 1.10.0
查看>>
2017"百度之星"程序设计大赛 - 资格赛-度度熊与邪恶大魔王(dp+后缀最小值)
查看>>
Node + vue 实现移动官网
查看>>