Publishing a file using paho-mqtt

3,375 views
Skip to first unread message

Ahmed Al-haddad

unread,
Sep 30, 2015, 10:52:06 PM9/30/15
to MQTT
Hello everyone. This is my first post here so pardon me in case I violate any of the rules. Your guidance is much appreciated. 

 I was trying to publish a message that is not a string using paho-mqtt in python and I came across this answer on Stackover flow http://stackoverflow.com/questions/19506939/how-can-i-publish-a-file-using-mosquitto-in-python . In this answer they used mosquitto, and from what I know, mosquitto is the same as paho-mqtt as it was donated to eclipse. But then when I tried the same steps I received this error 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
I am not quite sure what I have done wrong. 
I am connected to the paho-mqtt iot.eclipse.org server. Do I need to create my own server? I am using it for my fyp so there could be more things connected to the server, is that ok? 

 

P/S: Just asking for future reference, I find MQTT quite interesting, but I feel that there's a lot that I still need to learn, especially implementation wise. Do I need to read the code to understand? Also, how frequently possible it is for me to ask here?

Thanks a lot.

Laing, Michael

unread,
Oct 1, 2015, 7:20:25 AM10/1/15
to mq...@googlegroups.com
See https://bugs.eclipse.org/bugs/show_bug.cgi?id=462441 for the bug report on Paho/MQTT-python

A workaround for python 2.7 is:

...always pass in the payload as <unicode> or <bytearray>, avoiding <str>, particularly if the payload is opaque in one's application.

ml 

--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
To post to this group, send email to mq...@googlegroups.com.
Visit this group at http://groups.google.com/group/mqtt.
For more options, visit https://groups.google.com/d/optout.

Ahmed Al-haddad

unread,
Oct 1, 2015, 2:54:53 PM10/1/15
to MQTT
but hmm, following the answer from the link that is in the original post, it shows a way to convert the stringarray to a bytearray. I did that, but I am still getting the same thing?

f = open("data")
imagestring = f.read()
byteArray = bytes(imagestring)
client.publish("photo", byteArray ,0)
^this above is the answer. Using paho it is something like this for me

f = open ("photo.jpg")
image = f.read()
byteArray = bytes(image)
print byteArray # just to see what it is
client.publish("topic", payload=byteArray, qos=0, retain=False)
Should I use python 3 instead? 

Laing, Michael

unread,
Oct 1, 2015, 3:21:46 PM10/1/15
to mq...@googlegroups.com
bytes() returns an <str> in python 2.7, use bytearray() to return a <bytearray> as stated in the workaround and avoid <str>.

Many get confused with this, esp. between python 2 and 3...

ml

Ahmed Al-haddad

unread,
Oct 3, 2015, 7:35:32 PM10/3/15
to MQTT
Thank you a lot sir. It worked. 

Laing, Michael

unread,
Oct 3, 2015, 7:38:35 PM10/3/15
to mq...@googlegroups.com
glad to hear it :)
Reply all
Reply to author
Forward
0 new messages