Created
April 5, 2024 18:55
-
-
Save wsd1/882df5d62d99fabcc2a67b5328eb879b to your computer and use it in GitHub Desktop.
一个简单的示例,通过mqtt发送一张jpeg图片。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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