From 418f3091049803145ff454a67c63b458d9cf8998 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 9 May 2021 15:34:59 +0200 Subject: [PATCH] Another MQTT file upload example using acknowledge --- tools/mqtt-file/Config_demo_9.4.0.3.dmp | Bin 4096 -> 4096 bytes tools/mqtt-file/upload-example1.py | 22 +++-- tools/mqtt-file/upload-example2.py | 124 ++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 tools/mqtt-file/upload-example2.py diff --git a/tools/mqtt-file/Config_demo_9.4.0.3.dmp b/tools/mqtt-file/Config_demo_9.4.0.3.dmp index 248aec96a7966f23fc5c529fad71ff765f9cd98e..7bed4efce2704e198060a23f0132e873346b9904 100644 GIT binary patch delta 147 zcmV;E0Brw&Ab=nc2@+gQ_g`RPVPjxyDQ9|#kr1>^dU{PxA`>JPCR0gTM?_dmT`zxk zbTfN*)Yee BI+*|f delta 170 zcmV;b09F5hAb=nc2@+gQHeX<2VPjxyR%heCkr1?hX?jggQ4$m-7AF`fUP@m;L0eHm zGJJA#Hh6Y9ZghKka(X}s4G#cCA}A$1IeJV!Co4WMAz@TyR##YAT3cLQUSD8gVq;`w zW-tu|1sGjjZf|gLa&vTbc6WGrdV73*et&?0f`f#GhKGpG@8#+L=j_D!_xzNVmY0~$ Y|Mv3(?ys-2Mgg)82. + +Requirements: + - Python 3.x and Pip: + sudo apt-get install python3 python3-pip + pip3 install paho-mqtt + +Instructions: + Edit file and change parameters in User Configuration Section + + Then execute command upload-example2.py + +""" + +import paho.mqtt.client as mqtt +import time +import base64 +import hashlib + +# **** Start of User Configuration Section + +broker = "domus1" # MQTT broker ip address or name +broker_port = 1883 # MQTT broker port + +mytopic = "demo" # Tasmota MQTT topic +myfile = "Config_demo_9.4.0.3.dmp" # Tasmota Settings file name +myfiletype = 2 # Tasmota Settings file type + +# **** End of User Configuration Section + +# Derive from myfile +myfilesize = 4096 + +# Derive from time epoch +myid = 1620484815 + +# Derive fulltopic from broker LWT message +mypublish = "cmnd/"+mytopic+"/fileupload" +mysubscribe = "stat/"+mytopic+"/FILEUPLOAD" # Case sensitive + +# Tasmota currently supports MQTT message size of 1040 characters. Base64 adds 0.25 chars +chucksize = 700 # Tasmota max chunk size + +# Example does use feedback Acknowledge +Ack_flag = False + +# The callback for when mysubscribe message is received +def on_message(client, userdata, msg): + global Ack_flag +# print("Received message =",str(msg.payload.decode("utf-8"))) + Ack_flag = False + +def wait_for_ack(): + global Ack_flag + timeout = 100 + while Ack_flag and timeout > 0: + time.sleep(0.01) + timeout = timeout -1 + + if Ack_flag: + print("Error: Ack timeout") + + return Ack_flag + +client = mqtt.Client() +client.on_message = on_message +client.connect(broker, broker_port) +client.loop_start() # Start loop to process received messages +client.subscribe(mysubscribe) + +time_start = time.time() +print("Uploading file "+myfile+" to "+mytopic+" ...") + +client.publish(mypublish, "{\"File\":\""+myfile+"\",\"Id\":"+str(myid)+",\"Type\":"+str(myfiletype)+",\"Size\":"+str(myfilesize)+"}") +Ack_flag = True + +out_hash_md5 = hashlib.md5() + +fo = open(myfile,"rb") +Run_flag = True +while Run_flag: + if wait_for_ack(): # We use Ack here + Run_flag = False + + else: + chunk = fo.read(chucksize) + if chunk: + out_hash_md5.update(chunk) # Update hash + base64_encoded_data = base64.b64encode(chunk) + base64_data = base64_encoded_data.decode('utf-8') + client.publish(mypublish, "{\"Id\":"+str(myid)+",\"Data\":\""+base64_data+"\"}") + Ack_flag = True + + else: + md5_hash = out_hash_md5.hexdigest() + client.publish(mypublish, "{\"Id\":"+str(myid)+",\"Md5\":\""+md5_hash+"\"}") + Run_flag = False + +fo.close() + +time_taken = time.time() - time_start +print("Done in "+str("%.2f"%time_taken)+" seconds") + +client.disconnect() # Disconnect +client.loop_stop() # Stop loop