Last active
January 5, 2024 09:15
-
-
Save pfeerick/9607fba1bfae45726187ab53add40027 to your computer and use it in GitHub Desktop.
Extra script to make PlatformIO do compressed OTA updates for the ESP8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gzip | |
import shutil | |
import os | |
Import("env") | |
def compressFirmware(source, target, env): | |
""" Compress ESP8266 firmware using gzip for 'compressed OTA upload' """ | |
SOURCE_FILE = env.subst("$BUILD_DIR") + os.sep + env.subst("$PROGNAME") + ".bin" | |
if not os.path.exists(SOURCE_FILE+'.bak'): | |
print("Compressing firmware for upload...") | |
shutil.move(SOURCE_FILE, SOURCE_FILE + '.bak') | |
with open(SOURCE_FILE + '.bak', 'rb') as f_in: | |
with gzip.open(SOURCE_FILE, 'wb') as f_out: | |
shutil.copyfileobj(f_in, f_out) | |
if os.path.exists(SOURCE_FILE+'.bak'): | |
ORG_FIRMWARE_SIZE = os.stat(SOURCE_FILE + '.bak').st_size | |
GZ_FIRMWARE_SIZE = os.stat(SOURCE_FILE).st_size | |
print("Compression reduced firmware size to {:.0f}% of original (was {} bytes, now {} bytes)".format((GZ_FIRMWARE_SIZE / ORG_FIRMWARE_SIZE) * 100, ORG_FIRMWARE_SIZE, GZ_FIRMWARE_SIZE)) | |
env.AddPreAction("upload", compressFirmware) |
Thx for this script.
One suggested improvement:
SOURCE_FILE = source[0].get_abspath()
Then I prefer to use this as part of the build rather than upload. This is achieved by replacing last row with:
env.AddPostAction("buildprog", compressFirmware)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lol... so it does... I obviously had a brain fart moment there, there, as that was the percentage I wanted to see, but the message should have been something like
Compression reduced firmware size to {:.0f}% of original size
. I see andrewwalters spotted that also when he made the other tweaks.Thanks for the suggestion!