树莓派 - RasberryPi推送数据到cloudMQTT
创建用户
在https://www.cloudmqtt.com/上创建一个帐户
转到右上角的控制面板
点击“创建”按钮
安装lib
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)
运行python脚本
在树莓派上运行如下代码:
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 观察到
Reference
https://pypi.org/project/paho-mqtt/#connect-reconnect-disconnect