标签:ons bss localtime 下载 print 客户端 分享图片 payload 目录
-----------------------------------------------------1-----------------------------------------------------------------------------------
更新软件源:sudo apt-get update
下载g++编译器:sudo
apt-get
install
g++
安装:sudo
apt-get
install
libssl-dev
安装:sudo apt-get install uuid-dev
下载客户端文件:sudo wget http://mosquitto.org/files/source/mosquitto-1.5.tar.gz
也可以直接网页下载:
解压:sudo tar zxfv mosquitto-1.5.tar.gz
进入解压目录:cd mosquitto-1.5
编译:make
安装:sudo make install
-----------------------------------------------------2-----------------------------------------------------------------------------------
下载:git clone https://github.com/eclipse/paho.mqtt.python
打开:cd paho.mqtt.python
安装:python setup.py install
编写实现代码:
#************************************************************************************
import paho.mqtt.client as mqtt
import time
HOST = "服务器IP地址-----"
PORT = 61613#端口号----
def client_loop():
client_id = time.strftime(‘%Y%m%d%H%M%S‘,time.localtime(time.time()))# ClientId不能重复,所以使用当前时间
client = mqtt.Client(client_id)
client.username_pw_set("用户名---", "密码---")
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, PORT, 60)
client.publish("消息号-----", "MQTT start", qos=0, retain=False) # 发布消息
client.loop_forever()
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("消息号------")
def on_message(client, userdata, msg):#打印订阅消息
print(msg.topic+" "+msg.payload.decode("utf-8"))
if __name__ == ‘__main__‘:
client_loop()
#********************************************************************************
运行效果:
云端java自编客户端捕获和推送消息:
成功,接下来是手机编程连接,然后树莓派接入GPIO口,实现初级控制。部分内容参考https://blog.csdn.net/itas109/article/details/78873257,感谢。
标签:ons bss localtime 下载 print 客户端 分享图片 payload 目录
原文地址:https://www.cnblogs.com/dongxiaodong/p/9678452.html