From 510f33f83270f72eb827fc70ce31230c0bf66069 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 9 May 2021 13:09:53 +0200 Subject: [PATCH] Add MQTT file upload example --- tasmota/settings.ino | 3 + tools/mqtt-file/Config_demo_9.4.0.3.dmp | Bin 0 -> 4096 bytes tools/mqtt-file/upload-example1.py | 91 ++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 tools/mqtt-file/Config_demo_9.4.0.3.dmp create mode 100644 tools/mqtt-file/upload-example1.py diff --git a/tasmota/settings.ino b/tasmota/settings.ino index 5b512ee75..f473a427c 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -744,6 +744,7 @@ void SettingsDefault(void) { AddLog(LOG_LEVEL_NONE, PSTR(D_LOG_CONFIG D_USE_DEFAULTS)); SettingsDefaultSet1(); SettingsDefaultSet2(); + SettingsDefaultSet3(); SettingsSave(2); } @@ -1142,7 +1143,9 @@ void SettingsDefaultSet2(void) { Settings.flag3 = flag3; Settings.flag4 = flag4; Settings.flag5 = flag5; +} +void SettingsDefaultSet3(void) { #ifdef USER_TEMPLATE String user_template = USER_TEMPLATE; JsonTemplate((char*)user_template.c_str()); diff --git a/tools/mqtt-file/Config_demo_9.4.0.3.dmp b/tools/mqtt-file/Config_demo_9.4.0.3.dmp new file mode 100644 index 0000000000000000000000000000000000000000..248aec96a7966f23fc5c529fad71ff765f9cd98e GIT binary patch literal 4096 zcmeH~i8~Z{9L8N?TvM*7L^SS17C9o7LK4$BhHhJ-TWtvy$xX-+#mG6d84}}~G|mtb z29b<$A2Y^aX3Q9x?DOod(w1kR+MZ|o6W-_Z{yx9&3lKcGPxHvpW5*nhp0PgF``lsF z{OtMjLgJiU9Q&cR>qJ)z0L?7d6NfT!2W zwIL>WBt?Av_xc3Rd`xaabZC+3YD9eop>LU zn3IRh7#Zs-$_|MPZ|dtG@baZ;6^V};nEL+8$4 zxae}}@^4qJx?Xd0_qcxJrl;2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qtt-file/upload-example1.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + upload-example1.py - Upload Tasmota settings file + + Copyright (C) 2021 Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +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-example1.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" + +# Tasmota currently supports MQTT message size of 1040 characters. Base64 adds 0.25 chars +chucksize = 700 # Tasmota max chunk size + +# Example does not use feedback Acknowledge +sleeptime = 0.2 + +client = mqtt.Client() +client.connect(broker, broker_port) + +client.publish(mypublish, "{\"File\":\""+myfile+"\",\"Id\":"+str(myid)+",\"Type\":"+str(myfiletype)+",\"Size\":"+str(myfilesize)+"}") + +out_hash_md5 = hashlib.md5() + +fo = open(myfile,"rb") +Run_flag = True +while Run_flag: + 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+"\"}") + + else: + Run_flag=False + time.sleep(sleeptime) # We do not use Ack here + +md5_hash = out_hash_md5.hexdigest() +client.publish(mypublish, "{\"Id\":"+str(myid)+",\"Md5\":\""+md5_hash+"\"}") + +fo.close() +client.disconnect() # Disconnect