parent
37688ff4c8
commit
25d360f93c
@ -0,0 +1,68 @@
|
||||
import os
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
import time
|
||||
|
||||
broker = '121.40.129.71'
|
||||
port = 48835
|
||||
|
||||
|
||||
|
||||
with open("../user.txt", "r") as f:
|
||||
user_id = f.read().strip()
|
||||
user_id = user_id.split("=")[-1]
|
||||
|
||||
# topic = "request/{}".format(user_id)
|
||||
|
||||
# 回调函数,当客户端收到连接响应时被调用
|
||||
def on_connect(client, userdata, flags, rc, properties=None):
|
||||
print("连接结果: " + str(rc))
|
||||
# 订阅请求主题
|
||||
client.subscribe("response/{}".format(user_id), qos=2)
|
||||
|
||||
client.subscribe("image/{}".format(user_id), qos=2)
|
||||
|
||||
# 回调函数,当客户端收到消息时被调用
|
||||
def on_message(client, userdata, msg):
|
||||
# print("收到消息: " + msg.topic + " " + str(msg.payload))
|
||||
|
||||
if msg.topic == "response/{}".format(user_id):
|
||||
print("收到响应: " + str(msg.payload))
|
||||
|
||||
if msg.topic == "image/{}".format(user_id):
|
||||
with open("image.jpg", "wb") as f:
|
||||
f.write(msg.payload)
|
||||
print("图片已保存。")
|
||||
|
||||
global response_received
|
||||
response_received = True
|
||||
|
||||
|
||||
# 创建客户端实例
|
||||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,)
|
||||
|
||||
# 设置回调函数
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
|
||||
# 连接到MQTT代理
|
||||
client.connect(broker, port, 60)
|
||||
|
||||
# 启动一个后台线程来处理网络流量和回调
|
||||
client.loop_start()
|
||||
|
||||
# 发布消息
|
||||
|
||||
|
||||
# 等待响应
|
||||
response_received = False
|
||||
while not response_received:
|
||||
print("等待响应...")
|
||||
time.sleep(1)
|
||||
client.publish("request/{}".format(user_id), "image produce", qos=2)
|
||||
|
||||
# 停止网络循环并断开连接
|
||||
client.loop_stop()
|
||||
client.disconnect()
|
||||
|
||||
print("收到响应,客户端退出。")
|
@ -1,51 +0,0 @@
|
||||
# -*- coding: utf-8 -*-# -*- coding: utf-8 -*-
|
||||
# 订阅者
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
broker = '121.40.129.71'
|
||||
|
||||
# 连接成功回调
|
||||
def on_connect(client, userdata, flags, rc,properties):
|
||||
if rc.is_failure:
|
||||
print(f"连接失败: {rc}. loop_forever()将重新连接")
|
||||
else:
|
||||
print(f'连接成功: {rc}')
|
||||
client.subscribe('host_status/#')
|
||||
|
||||
|
||||
def on_subscribe(client, userdata, mid, reason_code_list, properties): # 订阅回调
|
||||
if reason_code_list[0].is_failure:
|
||||
print(f'订阅失败: {reason_code_list[0]}')
|
||||
else:
|
||||
print(f'订阅成功: {reason_code_list[0].value}')
|
||||
|
||||
def on_unsubscribe(client, userdata, mid, reason_code_list, properties): # 取消订阅回调
|
||||
if len(reason_code_list) == 0 or not reason_code_list[0].is_failure:
|
||||
print("取消订阅成功(if SUBACK is received in MQTTv3 it success)")
|
||||
else:
|
||||
print(f"取消订阅失败: {reason_code_list[0]}")
|
||||
client.disconnect() # 断开连接
|
||||
|
||||
# 消息接收回调
|
||||
def on_message(client, userdata, msg):
|
||||
print(msg.topic+" "+str(msg.payload))
|
||||
|
||||
mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="book_1")
|
||||
mqtt.on_connect = on_connect
|
||||
mqtt.on_message = on_message
|
||||
mqtt.on_subscribe = on_subscribe
|
||||
mqtt.on_unsubscribe = on_unsubscribe
|
||||
mqtt.is_connected()
|
||||
|
||||
mqtt.user_data_set(["user_data", "book_1"])
|
||||
mqtt.connect(broker, 48835, 60)
|
||||
|
||||
mqtt.loop_forever()
|
||||
print(f"收到以下消息: {mqtt.user_data_get()}")
|
||||
# while True:
|
||||
# rc = mqtt.loop(timeout=1.0)
|
||||
# print(11)
|
||||
# if rc != 0:
|
||||
# mqtt.reconnect()
|
||||
|
||||
|
@ -1,14 +0,0 @@
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
def publish_image(mqtt_client, topic, image_path):
|
||||
with open(image_path, "rb") as image_file:
|
||||
image_data = image_file.read()
|
||||
mqtt_client.publish(topic, image_data)
|
||||
|
||||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,)
|
||||
client.connect("121.40.129.71", 48835, 60) # 连接到 MQTT 代理
|
||||
|
||||
client.loop_start()
|
||||
img_path = input("Enter the path of the image: ")
|
||||
publish_image(client, "test/image", img_path)
|
||||
client.loop_stop()
|
@ -1,4 +1,3 @@
|
||||
#!/usr/bin/bash
|
||||
|
||||
|
||||
libcamera-still -o image.jpg --timeout 1000
|
||||
libcamera-still -o /tmp/image.jpg --timeout 10
|
@ -1,18 +0,0 @@
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
def on_connect(client, userdata, flags, rc,properties):
|
||||
print("Connected with result code " + str(rc))
|
||||
client.subscribe("test/image")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
with open("received_image.jpg", "wb") as image_file:
|
||||
image_file.write(msg.payload)
|
||||
print("Image received and saved")
|
||||
|
||||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, )
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
|
||||
client.connect("121.40.129.71", 48835, 60) # 连接到 MQTT 代理
|
||||
|
||||
client.loop_forever()
|
@ -1,32 +0,0 @@
|
||||
# MQTT订阅客户端
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
broker = "121.40.129.71"
|
||||
|
||||
# 连接成功回调
|
||||
def on_connect(client, userdata, flags, rc,properties):
|
||||
print('Connected with result code '+str(rc))
|
||||
client.subscribe('tts/#')
|
||||
|
||||
# 消息接收回调
|
||||
def on_message(client, userdata, msg):
|
||||
print(msg.topic+" "+str(msg.payload))
|
||||
|
||||
|
||||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, clean_session=True)
|
||||
|
||||
# username = "gd32"
|
||||
# password = "educoder"
|
||||
# client.username_pw_set(username, password)
|
||||
# 指定回调函数
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
|
||||
# 建立连接
|
||||
client.connect(broker, 48835, 60)
|
||||
# 发布消息
|
||||
|
||||
# client.publish('emqtt',payload='Hello World',qos=0)
|
||||
|
||||
client.loop_forever()
|
Loading…
Reference in new issue