Skip to content

Instantly share code, notes, and snippets.

@wsd1
Created April 5, 2024 18:55
Show Gist options
  • Save wsd1/882df5d62d99fabcc2a67b5328eb879b to your computer and use it in GitHub Desktop.
Save wsd1/882df5d62d99fabcc2a67b5328eb879b to your computer and use it in GitHub Desktop.
一个简单的示例,通过mqtt发送一张jpeg图片。
import os
import paho.mqtt.client as mqtt
# MQTT 代理地址和端口
broker_address = "192.168.xx.xx"
broker_port = 1883
# 文件路径
file_path = "160x128_demo_.jpg"
# MQTT 主题
topic = "miniTV/frame"
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# 打开文件并发送内容
with open(file_path, "rb") as file:
file_content = file.read()
client.publish(topic, file_content)
# 断开连接
client.disconnect()
def main():
# 连接到 MQTT 代理
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
client.connect(broker_address, broker_port)
client.on_connect = on_connect
#连接用户名和密码
#client.username_pw_set('agile-user', 'agile-pwd')
client.loop_forever()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment