树莓派物联网应用基础(1):跨平台测试古德微MQTT物联网通信
学习完古德微花老师的讲课,实践课程内容,梳理一下学习的思路实践过程记录下来。我把程序代码在两个不同系统运行,体验“跨平台Windows—树莓派(linux内核)测试古德微MQTT服务器实现远程通信”,记录一下自己的实验过程。https://upload-images.jianshu.io/upload_images/21365601-9eab8f1f47631ffd.gif?imageMogr2/auto-orient/striphttps://upload-images.jianshu.io/upload_images/21365601-9eab8f1f47631ffd.gif?imageMogr2/auto-orient/strip
一、安装paho-mqtt第三库,下载了安装代码在windows系统用离线安装,在树莓派用在线安装。paho-mqtt · PyPI
官网下载
paho-mqtt 1.5.0下载https://files.pythonhosted.org/packages/59/11/1dd5c70f0f27a88a3a05772cd95f6087ac479fac66d9c7752ee5e16ddbbc/paho-mqtt-1.5.0.tar.gz
下载链接
下载完后,把paho-mqtt-1.5.0.tar.gz解压,命令行CMD进入解压后的文件夹命令行输入以下命令安装
python setup.py install
树莓派命令行在线安装输入命令:sudo pip3 install paho-mqtt
在线安装paho-mqtt
在python中分别写发送和接收代码
#mqtt发送端代码wlw_sent.py
import paho.mqtt.client as mqtt
import time
client=mqtt.Client()
client.connect("www.gdwrobot.top",1883,60)
time.sleep(1)
nData=1
while 1:
time.sleep(0.5)
nData+=1
nData%=100
client.publish('testtopic',nData)
#client.disconnect()
print("success sent:",nData)
#mqtt接收端代码wlw_recv.py
#!/usr/bin/python
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("testtopic")
def on_message(client, userdata, msg):
print(msg.topic+" " + ":" + str(msg.payload,encoding="uft-8"))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("www.gdwrobot.top", 1883, 60)
client.loop_forever()
分别在两个命令行下运行Python程序同在windows环境,MQTT通讯windows命令行执行:sudo python wlw_recv.pywindows Anaconda命令行执行:python wlw_recv.py
windows环境执行物联网通讯
分别在windows和树莓派执行物联网通讯,如下图树莓派命令行执行:sudo python wlw_recv.py
windows命令行执行:python wlw_recv.py
分别在windows和树莓派执行物联网通讯
命令行下MQTT测试成功……接下来可以远程控制硬件(实现物联网)
好贴,收藏学习中 DFrJ5KYVQaH 发表于 2020-4-5 17:11
好贴,收藏学习中
{:5_190:}谢谢老师鼓励,继续学习中
页:
[1]