|
本帖最后由 选择困难症 于 2023-8-24 08:15 编辑
根据之前大佬们的帖子,成功连接wifi+安装pip3
在安装paho-mqtt-1.6.1时出现
- raise ValueError('ZIP does not support timestamps before 1980')
- ValueError: ZIP does not support timestamps before 1980
复制代码 文件未设置时间,无法正确安装
- tar -zxvf paho-mqtt-1.6.1.tar.gz
- cd paho-mqtt-1.6.1
- find ./ -type f
- find ./ -type f -exec touch -d "2023-08-23" {} +
复制代码 对文件夹下所有时间进行修改,成功进行安装。
根据文档中的播放音频命令
- import paho.mqtt.client as mqtt
- import subprocess
- # MQTT代理配置
- broker_address = "broker.emqx.io" # 请用您的MQTT代理地址替换
- broker_port = 1883 # 默认MQTT端口
- music_file = "/usr/share/myir/Music/The promise.mp3"
- cmd = ["gst-play-1.0", music_file]
- # 回调函数
- def on_connect(client, userdata, flags, rc):
- if rc == 0:
- print("已连接到MQTT代理!")
- # 连接后订阅一个主题
- client.subscribe("xzknz321")
- else:
- print(f"连接失败,返回代码:{rc}")
- def on_message(client, userdata, message):
- print(f"接收到主题 '{message.topic}' 的消息:{message.payload.decode()}")
- a = message.payload.decode()
- if a == "play_music":
- print(a)
- subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- # 创建MQTT客户端实例
- client = mqtt.Client()
- # 设置回调函数
- client.on_connect = on_connect
- client.on_message = on_message
- # 连接到MQTT代理
- client.connect(broker_address, broker_port, keepalive=60)
- # 启动循环,处理网络数据,回调和保持连接
- client.loop_start()
- try:
- # 发布一条消息到主题
- client.publish("xzknz321", "你好,MQTT!")
- # 让程序保持运行以接收消息
- while True:
- pass
- except KeyboardInterrupt:
- # 在程序被中断时(Ctrl+C),优雅地断开MQTT客户端连接
- client.disconnect()
- client.loop_stop()
- print("已从MQTT代理断开连接。")
复制代码
|
|