如何使用 mosquitto 发送图像?

How can I send a image by using mosquitto?

我正在尝试在 Raspberry Pi2 中使用 MQTT mosquitto 代理(pub 和 sub)发送 jpg 图像。

这是我的 python 代码 pub.py(已修改)

import paho.mqtt.client as mqtt

def on_publish(mosq, userdata, mid):
    mosq.disconnect()

client = mqtt.Client()
client.connect("test.mosquitto.org", 1883, 60)
client.on_publish = on_publish

f=open("b.jpg", "rb") #3.7kiB in same folder
fileContent = f.read()
byteArr = bytearray(fileContent)
client.publish("image",byteArr,0)

client.loop_forever()

它是 sub.py(已修改)

import paho.mqtt.client as mqtt

def on_connect(client, userdata, rc):
    print("Connect" + str(rc))
    client.subscribe("image") 

def on_message(client, userdata, msg):
    print "Topic : ", msg.topic
    f = open("/tmp/output.jpg", "w")  #there is a output.jpg which is different
    f.write(msg.payload)
    f.close()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("test.mosquitto.org", 1883, 60)

client.loop_forever()

我的 python 版本是 2.7.9.

在我解决了一些错误之后,它似乎可以工作但没有。

当我执行 sub.py 时,它连接成功,所以我在其他终端执行 pub.py。

但是,没有连接消息就没有任何反应"Connect with result code 0"

没有错误信息,所以我不知道我的错误是什么。

sub.py 中,您有 2 个 on_public 函数,应分别重命名为 on_connecton_publish

pub.py 中,您需要在客户端实际设置 on_publish 方法,以便在发布完成后调用它。

...
client.connect("test.mosquitto.org", 1883, 60)
client.on_publish = on_public
...

正如@ralight 在他对您之前问题的回答中指出的那样,您应该将 client.loop(5) 更改为 client.loop_forever() 由于 mosq.disconnect()

在您的 sub.py 中,您需要一个回调来处理您订阅的传入消息。标准回调是 on_message.

只需将 sub.y on_publish(client, userdata, msg) 重命名为 on_message(client, userdata, msg) 并分配 client.on_message = on_message.

测试代码:

要求:

  1. 安装 Mosquitto Broker
  2. 安装 paho.mqtt 包。

pub.py

import paho.mqtt.publish as publish
MQTT_SERVER = "xxx.xxx.xxx.xxx"  #Write Server IP Address
MQTT_PATH = "Image"

f=open("image_test.jpg", "rb") #3.7kiB in same folder
fileContent = f.read()
byteArr = bytearray(fileContent)


publish.single(MQTT_PATH, byteArr, hostname=MQTT_SERVER)

一个小修改,文件权限是写字节而不是写模式。

sub.py

import paho.mqtt.client as mqtt
MQTT_SERVER = "localhost"
MQTT_PATH = "Image"

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe(MQTT_PATH)
    # The callback for when a PUBLISH message is received from the server.


def on_message(client, userdata, msg):
    # more callbacks, etc
    # Create a file with write byte permission
    f = open('output.jpg', "wb")
    f.write(msg.payload)
    print("Image Received")
    f.close()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

预期输出:

能够将图像从 Raspberry Pi 传输到服务器计算机。