I am trying to publish image data to MQTT (CloudMQTT) with following code, but the data is not appearing on MQTT, don't even see any data on MQTT broker.

import identity
import json
import paho.mqtt.client as mqtt
import time
import datetime
import RPi.GPIO as GPIO
import bme280
import picamera
import base64

DEVICE_ID = identity.local_hostname()

config = json.loads(open('config.json').read())
SERVER = config['mqtt1']['hostname']
PORT = int(config['mqtt1']['port'])
USER = config['mqtt1']['username']
PASSWORD = config['mqtt1']['password']

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to broker")
        print("Connection failed")

def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed: " + str(message.topic) + " " + str(mid) + " " + str(granted_qos))

def on_message(client, userdata, message):
    print("message topic=",message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)

client = mqtt.Client(DEVICE_ID)                 
camera = picamera.PiCamera()
camera.resolution = (1280, 720)
client.username_pw_set(USER, password=PASSWORD)   
client.on_connect= on_connect                     
client.on_subscribe= on_subscribe                  
client.on_message= on_message                     
client.connect(SERVER, port=PORT)            
topics = [("DOWN/site01/pod02",2)]                 
data = {'device_id':DEVICE_ID}
PUBLISH_DATA = "UP/" + "site01/" + DEVICE_ID + "/data"
PUBLISH_IMAGE = "UP/" + "site01/" + DEVICE_ID + "/image"

    while True:
        data['date'] = str(datetime.datetime.today().isoformat())
        data['temperature'],data['pressure'],data['humidity'] = bme280.readBME280All()
        data['switch1'] = GPIO.input(14)
        data['switch2'] = GPIO.input(15)

        image_data = open("image.jpg", "rb")
        image_string = image_data.read()
        imageByteArray = bytes(image_string)
        client.publish(PUBLISH_IMAGE, imageByteArray, 0)
except KeyboardInterrupt:

Anyone know what I am missing here?


1 Answer 1


This is because the message payload is way bigger than will fit into a single TCP packet so the problem is that you are not starting the Paho client network loop which will chunk the message up and stream it out to the network.

You have 2 choice.

First start the network loop in the script. This is best if you are planning on sending multiple images.

Secondly if you are just publishing 1 image then the paho client code has a single function to handle just that.

Examples of how to use both can be found in the following Stack Overflow Answer.

  • image.jpg size with camera.resolution = (1024, 768) is 380K, Can I compress it before sending ?
    – rp346
    Jan 6, 2019 at 19:08
  • You can do what ever you want with it, MQTT payloads are just byte arrays. You will of course need to know how to unpack it at the subscriber
    – hardillb
    Jan 6, 2019 at 19:21
  • After reducing the resolution to 50x50, image data started appearing on the mqtt broker.
    – rp346
    Jan 6, 2019 at 20:16
  • Sounds like cloudMQTT have a payload size limit (or it could be you are tripping the bandwidth limit of 10K/s)
    – hardillb
    Jan 6, 2019 at 20:23
  • I was able to publish image with resolution upto 630x630, but not beyond this.
    – rp346
    Jan 11, 2019 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.