2022年12月28日 星期三

Paho Python MQTT Client – Subscribe With Examples

 Paho Python MQTT Client – Subscribe With Examples

源自於 http://www.steves-internet-guide.com/subscribing-topics-mqtt-client/

'''

Python 範例程式已經根據可用 MQTT Broker 修改

程式先訂閱 house/bulb1 主題,然後發行 house/bulb1 此主題及內容

'''

import time

import paho.mqtt.client as mqtt


broker="broker.hivemq.com"

#broker_address="iot.eclipse.org"


# define callback

def on_message(client, userdata, message):

    time.sleep(1)

    print("received message =", str(message.payload.decode("utf-8")))


def on_subscribe(client, userdata, mid, granted_qos):

    print("on_subscribe --> Subscribe with mid "+str(mid)+" received.")

    print("on_subscribe --> Subscribe with granted_qos "+str(granted_qos)+" received.")

    


client = mqtt.Client("client-001")  # create client object client1.on_publish = on_publish #assign function to callback client1.connect(broker,port) #establish connection client1.publish("house/bulb1","on")

######Bind function to callback

client.on_subscribe = on_subscribe

#####

print("connecting to broker ", broker)

client.connect(broker)  # connect

client.loop_start()  # start loop to process received messages

print("subscribing ")

#client.subscribe("alex9ufo/house/bulb1")  # subscribe

client.subscribe([("alex9ufo/house/bulb3",2),("alex9ufo/house/bulb4",1),("alex9ufo/house/bulb5",0)])

print("publishing ")

client.publish("alex9ufo/house/bulb3", "on")  # publish

time.sleep(2)

client.publish("alex9ufo/house/bulb4", "off")  # publish

time.sleep(2)

client.publish("alex9ufo/house/bulb5", "on")  # publish

time.sleep(2)



client.publish("alex9ufo/house/bulb3", "off")  # publish

time.sleep(2)

client.publish("alex9ufo/house/bulb4", "on")  # publish

time.sleep(2)

client.publish("alex9ufo/house/bulb5", "on")  # publish

time.sleep(2)


client.disconnect()  # disconnect

client.loop_stop()  # stop loop

>>> %Run Subscribe_1.py

connecting to broker  broker.hivemq.com

subscribing 

publishing 

on_subscribe --> Subscribe with mid 1 received.

on_subscribe --> Subscribe with granted_qos (2, 1, 0) received.

>>> 


'''

Python 範例程式已經根據可用 MQTT Broker 修改

程式先訂閱 house/bulb1 主題,然後發行 house/bulb1 此主題及內容

'''

import time

import paho.mqtt.client as mqtt


broker="broker.hivemq.com"

#broker_address="iot.eclipse.org"


# define callback

def on_message(client, userdata, message):

    time.sleep(1)

    print("received message =", str(message.payload.decode("utf-8")))


def on_subscribe(client, userdata, mid, granted_qos):

    print("on_subscribe --> Subscribe with mid "+str(mid)+" received.")

    print("on_subscribe --> Subscribe with granted_qos "+str(granted_qos)+" received.")

    


client = mqtt.Client("client-001")  # create client object client1.on_publish = on_publish #assign function to callback client1.connect(broker,port) #establish connection client1.publish("house/bulb1","on")

######Bind function to callback

client.on_message = on_message

#####

print("connecting to broker ", broker)

client.connect(broker)  # connect

client.loop_start()  # start loop to process received messages

print("subscribing ")

#client.subscribe("alex9ufo/house/bulb1")  # subscribe

client.subscribe([("alex9ufo/house/bulb3",2),("alex9ufo/house/bulb4",1),("alex9ufo/house/bulb5",0)])

print("publishing ")

client.publish("alex9ufo/house/bulb3", "on")  # publish

time.sleep(2)

client.publish("alex9ufo/house/bulb4", "off")  # publish

time.sleep(2)

client.publish("alex9ufo/house/bulb5", "on")  # publish

time.sleep(2)



client.publish("alex9ufo/house/bulb3", "off")  # publish

time.sleep(2)

client.publish("alex9ufo/house/bulb4", "on")  # publish

time.sleep(2)

client.publish("alex9ufo/house/bulb5", "on")  # publish

time.sleep(2)


client.disconnect()  # disconnect

client.loop_stop()  # stop loop


>>> %Run Subscribe_1.py

connecting to broker  broker.hivemq.com

subscribing 

publishing 

received message = on

received message = off

received message = on

received message = off

received message = on

received message = on

>>> 



沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...