在https://www.cloudmqtt.com/上创建一个帐户
转到右上角的控制面板
点击“创建”按钮
sudo pip install paho-mqtt
注:不加·sudo
可会有如下一些错误
Exception:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 353, in run
wb.build(autobuilding=True)
在树莓派上运行如下代码:
import paho.mqtt.client as mqtt
import time
data="Hello from Raspberry Pi!"
while True:
print data
try:
client=mqtt.Client()
#修改下一行中的用户名和密码
client.username_pw_set("dspwoncw","3s9YYyRmL2l0")
#修改下一行中的client_id
client.connect("m11.cloudmqtt.com",16868,60)
client.publish("pi",data)
time.sleep(1)
except KeyboardInterrupt:
print("end")
client.disconnect()
在websocket UI 观察到
https://pypi.org/project/paho-mqtt/#connect-reconnect-disconnect
手机扫一扫
移动阅读更方便
你可能感兴趣的文章