Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用MOSQUITO发送图像?_Python_Raspberry Pi_Publish Subscribe_Mqtt_Mosquitto - Fatal编程技术网

Python 如何使用MOSQUITO发送图像?

Python 如何使用MOSQUITO发送图像?,python,raspberry-pi,publish-subscribe,mqtt,mosquitto,Python,Raspberry Pi,Publish Subscribe,Mqtt,Mosquitto,我正在尝试使用Raspberry Pi2中的MQTT mosquitto代理(pub和sub)发送一个jpg图像 这是我的python代码pub.py(已修改) 它是sub.py(修改的) 我的python verson是2.7.9 在我解决了一些错误之后,它似乎起作用了,但没有 当我实现sub.py时,它成功连接,因此我在其他终端中实现了pub.py 但是,如果没有“连接结果代码为0”的连接消息,则不会有任何反应 没有错误消息,所以我不知道我的错误是什么。在sub.py中,您有两个on\u p

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

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

它是sub.py(修改的)

我的python verson是2.7.9

在我解决了一些错误之后,它似乎起作用了,但没有

当我实现sub.py时,它成功连接,因此我在其他终端中实现了pub.py

但是,如果没有“连接结果代码为0”的连接消息,则不会有任何反应

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

在sub.py中,您有两个
on\u public
函数,它们应该分别重命名为
on\u connect
on\u publish

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

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

另外,正如@ralight在回答您之前的问题时指出的那样,您应该将
client.loop(5)
更改为
client.loop\u forever()
因为sub.py中的
mosq.disconnect()
需要一个回调来处理订阅的传入消息。这方面的标准回调是消息上的


只需在sub.y
on_publish(client,userdata,msg)
中将您的sub.y
重命名为
on_message(client,userdata,msg)
并分配
client.on_message=on_message
测试代码:

要求:

  • 安装MOSQUITO代理
  • 安装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)
    
    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()
    
    一个小的修改,文件权限是写字节而不是写模式

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

    我根据您的回答重新编码。然而,它仍然不起作用。有时候,pub.py不会永远工作。我的代码顺序错了吗?定义“不工作”,它的功能对我来说很好。我想是的,当我阅读我的代码时,我认为没有逻辑或语法错误。它看起来很好,实际上它确实有效,但当我实现pub.py/sub.py时,我在sub.py术语中看不到任何主题消息。我认为它不会发布图像。我只能看到连接语句。两个应用程序都不会打印任何其他内容。pub应用程序将运行并退出,sub应用程序只将收到的文件写入
    /tmp/output.jpg
    您仍然没有正确重命名sub.py中的函数。第一个需要是连接上的