So far, I've been able to use a python script with the paho library to subscribe to a topic, and to write a single incoming message into a file. In this way, I've been able to send text files and 200kB jpg images over MQTT if the whole payload is sent in one MQTT publish. Here is the simple script that I am using for this purpose.

import paho.mqtt.client as mqtt
MQTT_SERVER = "broker.hivemq.com"

# 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))
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    f = open('output', "wb")
    print("File Received")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)


However, I am developing for an embedded LTE-M modem. I need to send my file in 1500 byte blocks. For example, if I want to send a 3016 byte file, I would have to send it in 4 MQTT publishes. The first publish contains information pertaining to the payload.


Explanation: 3 data segments, each data segment is 1500 bytes with the exception of the last data segment which is 16 bytes.

The next two publishes will be 1500 bytes each containing the payload data. The last publish will be the final 16 bytes of the payload data.

My question is, how can I modify my python script so that it can use the first publish in order to append the data publishes into one file? Thanks.

  • The maximum MQTT message size from the spec is 256mb. Is the size limitation based on a limit in your publisher?
    – hardillb
    Aug 25, 2020 at 7:34
  • The limit is based on the Quectel BG96 LTE-M modem that I am using. "After executing [the MQTT publish AT command], the client will be ready to send data, which will be sent as payload. The maximum length of the input data is 1548 bytes at a time"
    – Ivan Hu
    Aug 25, 2020 at 11:29
  • 1
    OK, so you need to try an implement yourself and then show us what you've tried and we will help you fix it, but we won't just supply code based on a set of requirements.
    – hardillb
    Aug 25, 2020 at 14:51
  • 1
    Also why waste a whole first message and not include as much payload as will fit after the header. You can also make the header binary so it takes less space and you could get the whole file into 3 messages.
    – hardillb
    Aug 25, 2020 at 14:53
  • why don't you publish to IvanHu/1, IvanHu/2 and IvanHu/3?
    – jsotola
    Aug 26, 2020 at 6:10


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.