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" MQTT_TOPIC = "IvanHu" # 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)) client.subscribe(MQTT_TOPIC) # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): f = open('output', "wb") f.write(msg.payload) print("File Received") f.close() client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect(MQTT_SERVER, 1883, 60) client.loop_forever()
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.
SEGMENTS:"3" SEGMENTSIZE:"1500" LASTSEGMENTSIZE:"16"
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.