You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
733 B

# MQTT订阅客户端
import paho.mqtt.client as mqtt
broker = "121.40.129.71"
# 连接成功回调
def on_connect(client, userdata, flags, rc,properties):
print('Connected with result code '+str(rc))
client.subscribe('tts/#')
# 消息接收回调
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, clean_session=True)
# username = "gd32"
# password = "educoder"
# client.username_pw_set(username, password)
# 指定回调函数
client.on_connect = on_connect
client.on_message = on_message
# 建立连接
client.connect(broker, 48835, 60)
# 发布消息
# client.publish('emqtt',payload='Hello World',qos=0)
client.loop_forever()