Skip to content

Commit

Permalink
add script to compress firmware before ota upload (1technophile#1065)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dkneisz authored Aug 29, 2021
1 parent f126a51 commit 149712b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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.
47 changes: 47 additions & 0 deletions prod_env.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <ip> ;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
33 changes: 33 additions & 0 deletions scripts/compressFirmware.py
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit 149712b

Please sign in to comment.