zoukankan      html  css  js  c++  java
  • python3 mqtt 发布以及订阅

    安装库
    pip3 install paho-mqtt
     
    发布话题
    import paho.mqtt.client as mqtt
    import time
    import sys
     
     
    HOST = "103.77.337.89"
    PORT = 1883
     
     
    def on_connect(client, userdata, flags, rc):
        print("Connected with result code " + str(rc))
    def on_subscribe(client,userdata,mid,granted_qos):
        print("消息发送成功")
     
     
    client = mqtt.Client(protocol=3)
    client.username_pw_set("admin", "password")
    client.on_connect = on_connect
    client.on_subscribe = on_subscribe
    client.connect(host=HOST, port = PORT, keepalive=60)  # 订阅频道
    time.sleep(1)
    i = 0
     
     
    while True:
        try:
            # 发布MQTT信息
            sensor_data = "test" + str(i)
            client.publish(topic="public", payload=sensor_data.encode("utf-8"), qos=0)
            time.sleep(3)
            i += 1
        except KeyboardInterrupt:
            print("EXIT")
            client.disconnect()
            sys.exit(0)
     
     
     
    订阅话题
    import time
    import paho.mqtt.client as mqtt
    # The callback for when the client receives a CONNACK response from the server.
     
     
    HOST = "103.77.337.89"
    PORT = 1883
     
     
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("连接成功")
            print("Connected with result code " + str(rc))
     
     
    def on_message(client, userdata, msg):
        print(msg.topic + " " + str(msg.payload))
    client = mqtt.Client(protocol=3)
    client.username_pw_set("admin", "password")
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(host=HOST, port = PORT, keepalive=60)  # 订阅频道
    time.sleep(1)
    client.subscribe("public")
    #client.subscribe([("public", 0), ("test", 2)])
    client.loop_forever()
     
     
     
    订阅话题
    import time
    import paho.mqtt.client as mqtt
    # The callback for when the client receives a CONNACK response from the server.
     
     
    HOST = "103.77.337.89"
    PORT = 1883
     
     
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("连接成功")
            print("Connected with result code " + str(rc))
     
     
    def on_message(client, userdata, msg):
        print(msg.topic + " " + str(msg.payload))
    client = mqtt.Client(protocol=3)
    client.username_pw_set("admin", "password")
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(host=HOST, port = PORT, keepalive=60)  # 订阅频道
    time.sleep(1)
    client.subscribe("SJHTopic2")
    #client.subscribe([("public", 0), ("test", 2)])
    client.loop_forever()
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    scrapy爬虫框架学习
    python:面向对象—— __slots__来限制实例的属性命名范围
    列表list 的常用操作
    python replace()用法
    python 字符串 空字符串 len()
    python 字符串 编码问题
    python中语句、函数、类、模块、包之间的关系
    matplotlib 绘制正玄余玄曲线
    Es6/CommonJs/AMD模块系统的差异以及用法
    BOM和DOM的区别
  • 原文地址:https://www.cnblogs.com/sea-stream/p/13528676.html
Copyright © 2011-2022 走看看