jQuery火箭图标返回顶部代码

MQTT

安装:

pip install paho-mqtt

 

 

# -*- coding: utf-8 -*-
 
import paho.mqtt.client as mqtt
import time
import uuid
   
mac = uuid.uuid1()
type(mac)
mac = mac.hex
mac = mac[-12:].upper() #获取MAC地址
#print(mac) 


MQTTHOST = "ip"
MQTTPORT = 1883


mqttClient_id = mac
mqttClient = mqtt.Client(mqttClient_id)
mqttClient.username_pw_set("yang", "11223344")  # 必须设置,否则会返回「Connected with result code 4」



 
# 连接MQTT服务器
def on_mqtt_connect():
    mqttClient.connect(MQTTHOST, MQTTPORT, 60)
    mqttClient.loop_start()
    print("ok")
 
 
# publish 消息
def on_publish(topic, payload, qos):
    mqttClient.publish(topic, payload, qos)
 
# 消息处理函数
def on_message_come(lient, userdata, msg):
 
    print(msg.topic + " " + ":" + str(msg.payload))
  
   
 
 
# subscribe 消息
def on_subscribe():
    mqttClient.subscribe("/server", 1)
    mqttClient.on_message = on_message_come # 消息到来处理函数

 
 
def main():
    on_mqtt_connect()


    on_publish("/test/server", "Hello Python!", 1)
    on_subscribe()
    while True:
        pass
 
 
 
if __name__ == '__main__':
    main()

 附加断线重连参考机制:https://blog.csdn.net/qingzhuyuxian/article/details/106145631

           https://blog.csdn.net/leytton/article/details/52782523

posted @ 2021-04-22 20:37  小樊同学  阅读(114)  评论(0编辑  收藏  举报