Skip to content

Instantly share code, notes, and snippets.

@WakeupTsai
Last active June 16, 2025 13:55
Show Gist options
  • Save WakeupTsai/6cac70f8e9f26cc909e9223346580a0f to your computer and use it in GitHub Desktop.
Save WakeupTsai/6cac70f8e9f26cc909e9223346580a0f to your computer and use it in GitHub Desktop.
[Image-MQTT] Publish and subscribe the images through MQTT #mqtt #image
import paho.mqtt.publish as publish
f= open("test.jpg")
filecontent = f.read()
byteArr = bytearray(filecontent)
publish.single('topic', byteArr, qos=1, hostname='m2m.eclipse.org')
import paho.mqtt.client as mqtt
# 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("topic")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
f = open('receive.jpg','w')
f.write(msg.payload)
f.close()
print 'image received'
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("m2m.eclipse.org", 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()
Send images through MQTT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment