parent
9e5bbc3a3a
commit
9f585d6bb6
@ -0,0 +1,14 @@
|
|||||||
|
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()
|
@ -0,0 +1,18 @@
|
|||||||
|
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()
|
Loading…
Reference in new issue