1

我用的是百度loT Core。我使用两个设备作为客户端,一个数据库(TSDB)作为服务器。目标功能:一个客户端将图像发送到数据库,然后将数据传输到另一个客户端。我从如何在 python 中使用 Mosquitto 发布文件获得帮助? 但它仍然不起作用。

发送图片

    import paho.mqtt.client as mqtt
    import json
    import cv2

    HOST = '************'
    PORT = 1883
    client_id = '************'
    username = '***********'
    password = '******************'
    topic = '******'
    
    
    
    # obj = userdata
    # mqttc = client
    def on_connect(mqttc, obj, flags, rc):
        print("rc: " + str(rc))
    
    
    def on_message(mqttc, obj, msg):
        print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload.decode('utf-8')))
    
    
    def on_publish(mqttc, obj, mid):
        print("mid: " + str(mid))
    
    
    def on_subscribe(mqttc, obj, mid, granted_qos):
        print("Subscribed: " + str(mid) + " " + str(granted_qos))
    
    
    def on_log(mqttc, obj, level, string):
        print(string)
    
    
    def on_disconnect(mqttc, obj, rc):
        print("unsuccess connect %s" % rc)

    mqttc = mqtt.Client(client_id)
    mqttc.username_pw_set(username, password) # thanks correction. I found I forget to connect broker.But the question is still 
    mqttc.on_message = on_message
    mqttc.on_connect = on_connect
    mqttc.on_publish = on_publish
    mqttc.on_subscribe = on_subscribe
    mqttc.on_disconnect = on_disconnect
    # Uncomment to enable debug messages
    mqttc.on_log = on_log
    mqttc.connect(HOST, PORT, 60)

    image = 'E:/_TempPhoto/0.jpg'
print(type(image))
def imageToStr(image):
    with open(image, 'rb') as f:
        image_byte = base64.b64encode(f.read())
        print(type(image_byte))
    image_str = image_byte.decode('ascii')  # byte to str
    print(type(image_str))
    return image_str

image1 = imageToStr(image)
data = {
    "engineeringdata": {
        "date": 12,
        "value": "59.3;98.5",
        "image": image1
    }
}
json_mod = json.dumps(data)
mqttc.publish(topic, json_mod, 0)
mqttc.loop_forever()

接收图像

    import paho.mqtt.client as mqtt
    import cv2
    import numpy as np
    import json
    
    HOST = '*********'
    PORT = 1883
    client_id = '************'
    username = '*****************'
    password = '******************'
    topic = '***************'
    
    
    def on_connect(client, userdata, flags, rc):
        print("Connected with result code: " + str(rc))

    def strToImage(str,filename):
       image_str= str.encode('ascii')
       image_byte = base64.b64decode(image_str)
       image_json = open(filename, 'wb')
       image_json.write(image_byte)  #将图片存到当前文件的fileimage文件中
       image_json.close()    
    
    def on_message(client, userdata, msg):
        print(msg.topic + " " + str(msg.payload)) 
        strToImage(str(msg.payload), 'E:/_TempPhoto/restore001.jpg')


    
    client = mqtt.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(HOST, PORT, 60) 
    client.subscribe(topic, 0)
    client.on_message = on_message
    client.loop_forever()
    # while True:
    #    client.loop(15)
    #    time.sleep(2)

这是我发送图像的消息和日志。用'......'把图像的印刷数据的一部分

Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k60) client_id=b'client_for_test'
<class 'str'>
<class 'bytes'>
<class 'str'>
Sending PUBLISH (d0, q0, r0, m1), 'b'$iot/client_for_test/user/fortest'', ... (102800 bytes)
mid: 1
Received CONNACK (0, 0)
rc: 0
Sending PINGREQ
Received PINGRESP
Sending PINGREQ
Received PINGRESP

这是接收图像的打印消息:(似乎正确)

Connected with result code: 0

我的错误案例是 5。而且我没有接收图像。但似乎发送成功。

也许是因为发送图像发送一次就结束了,但接收图像无法正确接收。我只需要两个客户端最简单的代码通过mqtt发送接收图像,拜托!帮帮我!

感谢帮助。前面的问题已经解决了。但是有新的麻烦。我编辑了新代码,我无法收到图像。 请更正我的代码。

4

2 回答 2

1

您可以在发布之前将图像编码为 base64,因此主题的内容将是一个字符串,例如:

import base64
 
with open("t.png", "rb") as imageFile:
    str = base64.b64encode(imageFile.read())
    print str
于 2020-09-03T08:41:51.037 回答
0

发布者.py

import paho.mqtt.publish as publish     
f= open("myimage.jpg")
content = f.read()
mybyteArray = bytearray(content)

mqttc.publish(topic, mybyteArray , 1)

接收器.py

def on_message(client, userdata, msg):
  f = open('myReceivedImage.jpg','w')
  f.write(msg.payload)
  f.close()
于 2020-09-03T12:29:33.003 回答