From 149712b57356fd90f0bb2f11e5b8fc5bda618529 Mon Sep 17 00:00:00 2001 From: Dave <43378003+dkneisz@users.noreply.github.com> Date: Sun, 29 Aug 2021 13:27:23 +0200 Subject: [PATCH] add script to compress firmware before ota upload (#1065) * add script to compress firmware before ota upload * add examples for OTA flashing/updating Sonoff RF Bridge - use compress script to save space uploading firmware - use "board_build.ldscript = eagle.flash.1m64.ld" to free some additional space for OTA flashing. --- platformio.ini | 4 ++++ prod_env.ini.example | 47 +++++++++++++++++++++++++++++++++++++ scripts/compressFirmware.py | 33 ++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 scripts/compressFirmware.py diff --git a/platformio.ini b/platformio.ini index f784941..2a8a16e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -195,6 +195,8 @@ build_flags = '-DGateway_Name="OpenMQTTGateway_SRFB"' '-UMQTTsetMQTT' ;We remove this function to have sufficient FLASH available for OTA, you should also use ESPWifiManualSetup to save flash memory and have OTA working board_build.flash_mode = dout +board_build.ldscript = eagle.flash.1m64.ld ;this frees more space for firmware uplad via OTA. +;extra_scripts = scripts/compressFirmware.py ;uncomment this to compress the firmware. This helps updating e.g. Sonoff RF Bridge via OTA flash by saving space for the uploaded firmware. [env:esp32dev-all-test] platform = ${com.esp32_platform} @@ -1071,3 +1073,5 @@ build_flags = '-DINPUT_GPIO=0' '-DGateway_Name="OpenMQTTGateway_SRFB_Direct"' board_build.flash_mode = dout +board_build.ldscript = eagle.flash.1m64.ld ;this frees more space for firmware uplad via OTA. +;extra_scripts = scripts/compressFirmware.py ;uncomment this to compress the firmware. This helps updating e.g. Sonoff RF Bridge via OTA flash by saving space for the uploaded firmware. diff --git a/prod_env.ini.example b/prod_env.ini.example index 677c869..b204e0e 100644 --- a/prod_env.ini.example +++ b/prod_env.ini.example @@ -78,3 +78,50 @@ build_flags = '-DMQTT_PASS="abcdefghi"' '-DMQTT_SERVER="192.168.1.17"' '-DDEFAULT_LOW_POWER_MODE=2' + +;hacked Sonoff RF-Bridge +[env:sonoff-rfbridge-hacked] +platform = ${com.esp8266_platform} +board = esp8285 +lib_deps = + ${com-esp.lib_deps} + ${libraries.esppilight} +build_flags = + ${com-esp.build_flags} + '-DZgatewayPilight="Pilight"' + '-DRF_RECEIVER_GPIO=4' + '-DRF_EMITTER_GPIO=5' + '-DLED_INFO=13' + '-DLED_INFO_ON=0' + '-DZsensorGPIOInput="GPIOInput"' + '-DINPUT_GPIO=0' + '-DGateway_Name="OpenMQTTGateway_SRFB_Direct"' +board_build.flash_mode = dout +board_build.ldscript = eagle.flash.1m64.ld ;this frees more space for firmware uplad via OTA. + +;hacked Sonoff RF-Bridge OTA flash +[env:sonoff-rfbridge-hacked-ota] +upload_protocol = espota +upload_port = ;change this to the ip address of you SRFB +upload_speed = 512000 +upload_flags = + --auth=OTAPASSWORD + --port=8266 +platform = ${com.esp8266_platform} +board = esp8285 +lib_deps = + ${com-esp.lib_deps} + ${libraries.esppilight} +build_flags = + ${com-esp.build_flags} + '-DZgatewayPilight="Pilight"' + '-DRF_RECEIVER_GPIO=4' + '-DRF_EMITTER_GPIO=5' + '-DLED_INFO=13' + '-DLED_INFO_ON=0' + '-DZsensorGPIOInput="GPIOInput"' + '-DINPUT_GPIO=0' + '-DGateway_Name="OpenMQTTGateway_SRFB_Direct"' +board_build.flash_mode = dout +extra_scripts = scripts/compressFirmware.py +board_build.ldscript = eagle.flash.1m64.ld ;this frees more space for firmware uplad. Should also be uses for initial flash via serial diff --git a/scripts/compressFirmware.py b/scripts/compressFirmware.py new file mode 100644 index 0000000..cd57a6d --- /dev/null +++ b/scripts/compressFirmware.py @@ -0,0 +1,33 @@ +import gzip +import shutil +import os +Import("env") + + +def compressFirmware(source, target, env): + """ Compress firmware using gzip for 'compressed OTA upload' """ + SOURCE_FILE = env.subst("$BUILD_DIR") + os.sep + env.subst("$PROGNAME") + ".bin" + SOURCE_BAK = SOURCE_FILE + '.bak' + do_compress = True + if os.path.exists(SOURCE_FILE) and os.path.exists(SOURCE_BAK): + src_mtime = os.stat(SOURCE_FILE).st_mtime + bak_mtime = os.stat(SOURCE_BAK).st_mtime + """ Recompress if .bin file is newer than .bak file """ + do_compress = (src_mtime > bak_mtime) + + if do_compress: + print("Compressing firmware for upload...") + shutil.move(SOURCE_FILE, SOURCE_BAK) + with open(SOURCE_BAK, 'rb') as f_in: + with gzip.open(SOURCE_FILE, 'wb', compresslevel = 9) as f_out: + shutil.copyfileobj(f_in, f_out) + """ Set modification time on compressed file so incremental build works """ + shutil.copystat(SOURCE_BAK, SOURCE_FILE) + + if os.path.exists(SOURCE_BAK): + ORG_FIRMWARE_SIZE = os.stat(SOURCE_BAK).st_size + GZ_FIRMWARE_SIZE = os.stat(SOURCE_FILE).st_size + + print("Compression reduced firmware size to {:.0f}% (was {} bytes, now {} bytes)".format((GZ_FIRMWARE_SIZE / ORG_FIRMWARE_SIZE) * 100, ORG_FIRMWARE_SIZE, GZ_FIRMWARE_SIZE)) + +env.AddPostAction("$BUILD_DIR/firmware.bin", [compressFirmware]) \ No newline at end of file