AbyssalSwamp  ActivaUser
» Guest:  Register | Login | 会员列表

RSS subscription to this AbyssalSwamp  

Previous thread Next thread
     
Title: python开发的mqtt发送与接收程序  
 
sky999
天山茗客



UID 181291
Digest 2
Points 10
Posts 3937
码币MB 2542 Code
黄金 0 Catty
钻石 884 Pellet
Permissions 10
Register 2020-11-28
Status offline
python开发的mqtt发送与接收程序

发送--------------------- import paho.mqtt.publish as publish # MQTT服务器的地址 mqtt_broker = "127.0.0.1" # 要发布的主题 mqtt_topic = "www.caffz.com" # 要发布的内容 mqtt_message = "Hello from www.caffz.com!" # 发布消息 publish.single(mqtt_topic, mqtt_message, hostname=mqtt_broker) 接收-------------------------- import paho.mqtt.client as mqtt # MQTT服务器的地址 mqtt_broker = "127.0.0.1" # 订阅的主题 mqtt_topic = "www.caffz.com" def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) # 订阅主题 client.subscribe(mqtt_topic) def on_message(client, userdata, msg): print(f"Received message: {msg.payload.decode()}") # 创建MQTT客户端 client = mqtt.Client() # 设置连接和消息回调 client.on_connect = on_connect client.on_message = on_message # 连接到MQTT服务器 client.connect(mqtt_broker, 1883, 60) # 保持连接 client.loop_forever()



CAFFZ.com
2023-11-27 11:40#1
View profile  Blog  Send a short message  Top
     


  Printable version | Recommend to a friend | Subscribe to topic | Favorite topic  


 


All times are GMT+8, and the current time is 2026-1-24 05:30 Clear informations ->sessions/cookies - Contact Us - CAFFZ - ZAKE