如何使用 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(modified)

 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。

但是,没有连接消息“连接结果代码 0”没有任何反应

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

原文由 ahnstar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 622
2 个回答

在 _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() 而发送消息后它仍然会退出 ---

原文由 hardillb 发布,翻译遵循 CC BY-SA 3.0 许可协议

测试代码:

要求:

  1. 安装 Mosquitto 代理
  2. 安装 paho.mqtt 包。

发布.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)

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

子.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 传输到服务器计算机。

原文由 DRV 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题