37创客科创中心

 找回密码
 立即注册
查看: 1297|回复: 0

823 一个mqtt案例

[复制链接]

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
发表于 2024-8-23 12:02:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x



  1. #  -*- coding: UTF-8 -*-

  2. import siot

  3. # 自定义函数
  4. def DIS(NaRong):#显示内容
  5.     print(NaRong)


  6. def initZhuTi():#初始化主题(为列表类型)
  7.     ZhuTi = ['37ck/m']
  8.     ZhuTi.clear()
  9.     ZhuTi.extend(['37ck/m'])
  10.     ZhuTi.extend(['37ck/821'])
  11.     ZhuTi.extend(['230525/525'])
  12.     return ZhuTi


  13. def DingYue(ZhuTi):#订阅主题(多主题订阅)
  14.     ShuLiang = 0
  15.     for index in range(len(ZhuTi)):
  16.         DIS(len(ZhuTi))
  17.         siot.getsubscribe(topic=(ZhuTi[ShuLiang]))
  18.         ShuLiang = (ShuLiang + 1)
  19.     return '订阅完成'


  20. def MQTT(U):
  21.     DingYue(initZhuTi())


  22. def FaSong_topic(ZhuTi, XiaoXi):
  23.     siot.publish(topic=ZhuTi, data=XiaoXi)
  24.     return (str(ZhuTi) + str((str("  主题发送消息  ") + str(XiaoXi))))


  25. # 事件回调函数
  26. def on_message_callback(client, userdata, msg):
  27.     DIS((str(msg.topic) + str((str(":(接收)") + str(msg.payload.decode())))))

  28. #这部分内容填写自己的MQTT服务器配置信息
  29. siot.init(client_id="", server="服务器IP地址", port=端口, user="用户", password="密码")
  30. siot.set_callback(on_message_callback)
  31. siot.connect()
  32. siot.loop()
  33. MQTT('37创客')

  34. while True:
  35.     FaSong_topic('37ck/m', input("37ck/m输入消息:"))
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|37创客科创中心

GMT+8, 2025-12-11 17:15 , Processed in 0.129309 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表