安装cJSON库 【自行去空格】
wget https: //gith ub.com/DaveGamble/cJSON/archive/refs/tags/v1.7.16.zip
unzip v1.7.16.zip
cd cJSON-1.7.16/
make
make install
cd ..
安装mosquitto
wget https: //mosquitto .org/files/source/mosquitto-2.0.15.tar.gz
tar -zxvf mosquitto-2.0.15.tar.gz
cd mosquitto-2.0.15
make
make install
cd ..
运行:
mosquitto
mosquitto -d #后运行
测试
mosquitto_sub -d -t akin520 #订阅
mosquitto_pub -d -t akin520 -m "hello, world" #推送
py:
pip install paho-mqtt
-
import paho.mqtt.client as mqtt
-
import time
-
import sys
-
-
-
timeflag =0
-
HOST = "127.0.0.1"
-
PORT = 1883
-
def client_loop():
-
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
-
client = mqtt.Client(client_id) # ClientId不能重复,所以使用当前时间
-
client.on_connect = on_connect
-
client.on_message = on_message
-
client.connect(HOST, PORT, 15)
-
client.loop_forever()
-
-
def on_connect(client, userdata, flags, rc):
-
print("Connected with result code "+str(rc))
-
client.subscribe("ESP32_SENDER")
-
print("ready!")
-
-
def on_message(client, userdata, msg):
-
global timeflag
-
timeflag=timeflag+1
-
sys.stdout.write("\033[F")
-
print("\r"+str(timeflag)+" "*10)
-
print(msg.payload)
-
-
-
-
if __name__ == '__main__':
-
client_loop()
阅读(354) | 评论(0) | 转发(0) |