python - How can I send a image by using mosquitto? -


i'm student studying mqtt , i'm trying send jpg image using mqtt mosquitto broker(pub , sub) in raspberry pi2.

i've solved many error, however, doesn't work.

this python code pub.py(modified)

import paho.mqtt.client 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 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 output.jpg 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 2.7.9.

after solved error, seems work doesn't.

when implement sub.py, connects implement pub.py in other terminal.

however, there isn't reaction without connect message "connect result code 0"

there no error message don't know mistake is.

my code wrong? think ok.

i'm sorry if basic coding skill.

please me need hand.

in sub.py have 2 on_public functions should renamed on_connect , on_publish respectively.

in pub.py need set on_publish method on client called once publish has been completed.

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

also @ralight pointed out in answer previous question, should change client.loop(5) client.loop_forever() still exit once message has been sent because of mosq.disconnect()


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -