How can I send a image by using mosquitto?

ahnstar picture ahnstar · May 28, 2016 · Viewed 15.4k times · Source

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.

Answer

D_Raja picture D_Raja · Sep 7, 2019

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.