代码语言:javascript复制
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
MQTTHOST = "139.199.208.33"
MQTTPORT = 1883
USERNAME = ""
PASSWORD = ""
CLIENTID = "SXF_Python_GPSMap"
HEARTBEAT = 60
client = mqtt.Client(CLIENTID)
topic = '/ESP32@PAD'
def on_connect(client, userdata, flags, rc):
print("Connected with result code " str(rc))
print("订阅主题 -> %s" % topic)
client.subscribe(topic)
def on_message(client, userdata, msg):
MQTT_Rx_Buff = str(msg.payload, encoding="utf-8")
print(MQTT_Rx_Buff)
def mqtt():
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(USERNAME, PASSWORD)
client.connect(MQTTHOST, MQTTPORT, HEARTBEAT)
client.publish(topic='/ESP32@PAD', payload='Hello', qos=0)
# client.loop_forever() # 阻塞
client.loop_start() # 线程
if __name__ == '__main__':
mqtt()
while 1:
pass