如何使用mosquitto发送图像?

时间:2016-05-28 13:41:54

标签: python raspberry-pi publish-subscribe mqtt mosquitto

我是一名正在研究MQTT的学生,我正在尝试使用Raspberry Pi2中的MQTT mosquitto broker(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。

但是,如果没有连接消息“没有连接结果代码0”

,则没有任何反应

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

我的代码错了?我认为没问题。

如果它只是一种基本的编码技巧,我很抱歉。

请帮助我,我需要你的手。

3 个答案:

答案 0 :(得分:4)

测试代码:

要求:

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

答案 1 :(得分:3)

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() < / p>

答案 2 :(得分:1)

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

只需将您的子on_publish(client, userdata, msg)重命名为on_message(client, userdata, msg)并指定client.on_message = on_message