Ai
1 Star 0 Fork 1

谷动谷力/MQTT-Client-Examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sub.py 960 Bytes
一键复制 编辑 原始数据 按行查看 历史
# sub.py
import time
from umqtt.simple import MQTTClient
# 定义 sub 客户端的连接信息
SERVER="broker.emqx.io"
ClientID = f'raspberry-sub-{time.time_ns()}'
user = "emqx"
password = "public"
topic = "raspberry/mqtt"
msg = b'{"msg":"hello"}'
def sub(topic, msg):
# 在回调函数打印主题和消息
print('received message %s on topic %s' % (msg, topic))
def main(server=SERVER):
# 创建连接,参数分别为客户端 ID,broker 地址,broker 端口号,认证信息
client = MQTTClient(ClientID, server, 1883, user, password)
client.set_callback(sub)
client.connect()
print('Connected to MQTT Broker "%s"' % (server))
# 如果与 broker 失去连接后重连,仍然会继续订阅 raspberry/topic 主题
client.subscribe(topic)
while True:
if True:
client.wait_msg()
else:
client.check_msg()
time.sleep(1)
if __name__ == "__main__":
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lojam/MQTT-Client-Examples.git
git@gitee.com:lojam/MQTT-Client-Examples.git
lojam
MQTT-Client-Examples
MQTT-Client-Examples
master

搜索帮助