How can I send a image by using mosquitto?

21,545

Solution 1

In sub.py you have 2 on_public functions which should be renamed on_connect and on_publish respectively.

In pub.py you need to actually set the on_publish method on the client so it will be called once the publish has been completed.

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

Also as @ralight pointed out in his answer to your previous question, you should change client.loop(5) to client.loop_forever() it will still exit once the message has been sent because of the mosq.disconnect()

Solution 2

Tested Code:

Requirements:

  1. Install Mosquitto Broker
  2. Install paho.mqtt package.

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)

One small modification, file permission is write byte instead of write mode.

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

Expected Output:

Able to transfer the Image from Raspberry Pi to server machine.

Solution 3

In your sub.py you need a callback which handles incoming messages for your subscription. The standard callback for that is on_message.

Just rename in your sub.y on_publish(client, userdata, msg) to on_message(client, userdata, msg)and assign client.on_message = on_message.

Share:
21,545
Admin
Author by

Admin

Updated on January 01, 2020

Comments

  • Admin
    Admin over 4 years

    I'm trying to send a jpg image using MQTT mosquitto broker(pub and sub) in Raspberry Pi2.

    This is my python code pub.py(modified)

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

    and it's 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()
    

    My python verson is 2.7.9.

    After I solved some error, It seems to work but It doesn't.

    When I implement the sub.py, It connects successfully so I implement the pub.py in other terminal.

    However, There isn't any reaction without connect message which is "Connect with result code 0"

    There is no error message so I don't know what my mistake is.

  • Admin
    Admin almost 8 years
    I recode following your answer. However, It does not work still. Sometime, pub.py is not working forever. Is my code order wrong?
  • hardillb
    hardillb almost 8 years
    Define"does not work", it functions just fine for me
  • Admin
    Admin almost 8 years
    I think so, when I read my code I think there is no logical or syntax error. It seems to wrok actually It does work but I cant see any topic message in sub.py termial when I implement pub.py/sub.py. I think It doesn't publish a image. I can see only connection sentence.
  • hardillb
    hardillb almost 8 years
    Neither app will print anything else. The pub app will just run and exit, the sub app just write the received file to /tmp/output.jpg
  • Admin
    Admin almost 8 years
    Would u mind checking the code again please? It was modified. when I run that code, It prints only connect 0 without topic
  • hardillb
    hardillb almost 8 years
    You still haven't renamed the functions in sub.py properly. The first needs to be on_connect
  • Admin
    Admin almost 8 years
    ah!!! It's just writing mistake. I already renamed that to on_connect. Are there any mistake without that?