How can I send a image by using mosquitto?
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:
- Install Mosquitto Broker
- 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
.
Admin
Updated on January 01, 2020Comments
-
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 almost 8 yearsI recode following your answer. However, It does not work still. Sometime, pub.py is not working forever. Is my code order wrong?
-
hardillb almost 8 yearsDefine"does not work", it functions just fine for me
-
Admin almost 8 yearsI 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 almost 8 yearsNeither 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 almost 8 yearsWould u mind checking the code again please? It was modified. when I run that code, It prints only connect 0 without topic
-
hardillb almost 8 yearsYou still haven't renamed the functions in sub.py properly. The first needs to be
on_connect
-
Admin almost 8 yearsah!!! It's just writing mistake. I already renamed that to on_connect. Are there any mistake without that?