From 16b57416df99a7cdc7fdbfa2d03c84110702ccf7 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 18 May 2024 01:56:29 +0200 Subject: [PATCH] 0.8.119 * added script and [instructions](../manual/factory_firmware.md) how to generate factory firmware which includes predefined settings --- .gitignore | 1 + manual/factory_firmware.md | 59 ++++++++++++++++++++++++++++++++++ scripts/add_littlefs_binary.py | 55 +++++++++++++++++++++++++++++++ src/CHANGES.md | 1 + src/platformio.ini | 1 + 5 files changed, 117 insertions(+) create mode 100644 manual/factory_firmware.md create mode 100644 scripts/add_littlefs_binary.py diff --git a/.gitignore b/.gitignore index b5c699cc..bb6620a2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ src/config/config_override.h src/web/html/h/* src/web/html/tmp/* +src/data/* /**/Debug /**/v16/* *.db diff --git a/manual/factory_firmware.md b/manual/factory_firmware.md new file mode 100644 index 00000000..e96cf575 --- /dev/null +++ b/manual/factory_firmware.md @@ -0,0 +1,59 @@ +# Generate factory firmware + +If the firmware should already contain predefined settings this guide will help you to compile these into a single binary file. + +## Generate default settings + +First install on the requested platform the standard firmware and configure everything to your needs. Once you did all changes store them and export them to a `json` file. + +## Further prepare default settings + +First create a directory `data` inside the following project path: `src/`. + +As the export removes all your password you need to add them again to the `json` file. Open the `json` file with a text editor and search for all the `"pwd": ""`. Between the second bunch of quotation marks you have to place the password. + +*Note: It's recommended to keep all information in one line to save space on the ESP littlefs partition* + +Next rename your export file to `settings.json` and move it to the new created directory. It should be look similar to this: + +``` +ahoy + |-- src + |-- data + |-- settings.json + |-- config + |-- network + ... +``` + +## modify platform.ini to build factory binary + +Open the file `src/platformio.ini` and uncomment the following line `#post:../scripts/add_littlefs_binary.py` (remove the `#`) + +## build firmware + +Choose your prefered environment and build firmware as usual. Once the process is finished you should find along with the standard `firmware.bin` an additional file called `firmware.factory.bin`. Both files are located here: `src/.pio/build/[ENVIRONMENT]/` + +## Upload to device + +Python: +`esptool.py -b 921600 write_flash --flash_mode dio --flash_size detect 0x10000 firmware.factory.bin` + +Windows: +`esptool.exe -b 921600 write_flash --flash_mode dio --flash_size detect 0x10000 firmware.factory.bin` + +For a 4MB flash size the upload should be finished within 22 seconds. + +## Testing + +Reboot your ESP an check if all your settings are present. + +## Keep updated with 'Mainline' + +From time to time a new version of AhoyDTU will be published. To get this changes into your alread prepared factory binary generation environment you have to do only a few steps: + +1. revert the changes of `platformio.ini` by executing from repository root: `git checkout src/platformio.ini` +2. pull new changes from remote: `git pull` +3. modify the `platformio.ini` again as you can read above (remove comment) +4. build and upload +5. enjoy diff --git a/scripts/add_littlefs_binary.py b/scripts/add_littlefs_binary.py new file mode 100644 index 00000000..7afc3391 --- /dev/null +++ b/scripts/add_littlefs_binary.py @@ -0,0 +1,55 @@ +import os +import subprocess +import shutil +Import("env") + + +def build_littlefs(): + result = subprocess.run(["pio", "run", "--target", "buildfs", "--environment", env['PIOENV']]) + if result.returncode != 0: + print("Error building LittleFS:") + exit(1) + else: + print("LittleFS build successful") + +def merge_bins(): + flash_size = int(env.get("BOARD_FLASH_SIZE", "4MB").replace("MB", "")) + app0_offset = 0x10000 + if env['PIOENV'][:7] == "esp8266": + app0_offset = 0 + elif env['PIOENV'][:7] == "esp8285": + app0_offset = 0 + + littlefs_offset = 0x290000 + if flash_size == 8: + littlefs_offset = 0x670000 + elif flash_size == 16: + littlefs_offset = 0xc90000 + + # save current wd + start = os.getcwd() + os.chdir('.pio/build/' + env['PIOENV'] + '/') + + with open("firmware.bin", "rb") as firmware_file: + firmware_data = firmware_file.read() + + with open("littlefs.bin", "rb") as littlefs_file: + littlefs_data = littlefs_file.read() + + with open("firmware.factory.bin", "wb") as merged_file: + # fill gap with 0xff + merged_file.write(firmware_data) + if len(firmware_data) < (littlefs_offset - app0_offset): + merged_file.write(b'\xFF' * ((littlefs_offset - app0_offset) - len(firmware_data))) + + merged_file.write(littlefs_data) + + os.chdir(start) + +def main(target, source, env): + build_littlefs() + merge_bins() + + +# ensure that script is called once firmeware was compiled +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", main) diff --git a/src/CHANGES.md b/src/CHANGES.md index 8e6855b0..d3721677 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -4,6 +4,7 @@ * fix reset values at midnight if WiFi isn't available #1620 * fix typo in English versions * add yield day to history graph #1614 +* added script and [instructions](../manual/factory_firmware.md) how to generate factory firmware which includes predefined settings ## 0.8.118 - 2024-05-10 * possible fix reset max values #1609 diff --git a/src/platformio.ini b/src/platformio.ini index efcc6e5f..9f226af0 100644 --- a/src/platformio.ini +++ b/src/platformio.ini @@ -23,6 +23,7 @@ extra_scripts = pre:../scripts/convertHtml.py pre:../scripts/applyPatches.py pre:../scripts/reduceGxEPD2.py + #post:../scripts/add_littlefs_binary.py lib_deps = https://github.com/esphome/ESPAsyncWebServer @ ^3.2.0