Page 1 of 1

A new way to distribute an ESP solution without requiring from the user to compile...

Posted: Fri Jul 05, 2024 4:00 pm
by rin67630
Following problem was to solve:
Distributing an ESP32 or ESP8266 solution without requiring the user to compile a version with his own credentials.

I don't want use the usual way of a temporary website to enter the credentials.
That is a very clumsy solution requiring a lot of code and program space (all the HTML code must be in program space)
Event for the user it is not straigt forward...
SmartConfig is a nice solution, but only provides SSID and Password.

I need at least: SSID, WiFiPassword, Device Name, Cloud Account, Device Credentials.
(the rest of the options and parameters can then be gathered from the cloud)

The solution is quite simple, elegant and requires practically no additional ESP code, no library and nothing to install on the user's side.
(excepted Python, which he needs anyway to upload the binary).

In your ESP code, you define placeholders for your credentials and make them long enough to potentially fit any user's content.

Here:
  1.  
  2.   #define DEVICE_NAME             "DEVCNAME        "
  3.   #define WIFI_SSID                   "WIFISSID        "
  4.   #define WIFI_PASS                   "WIFIPASS                "
  5.   #define THINGER_USERNAME    "CLOUDNAM        "
  6.   #define DEVICE_CREDENTIALS   "DEVCCRED        "  
The placeholders MUST match exactly those defined in the provided PythonPatcher.py Python script.
You then compile your code and distribute the raw binary together with PythonPatcher.py

The user places the two files on his desktop and runs the PythonPatcher.py.

The script will ask for the file to patch, and the credentials and patch the binary accordingly, saving the patched file on the destop too:
  1. %Run PythonPatcher.py
  2. Enter binfile to patch:PythonTest.bin
  3. Enter SSID:GW-****
  4. Enter Password:*******
  5. Enter Device Name:Steroids
  6. Enter Cloud User Name:Thing4
  7. Enter Device Credentials:*******
  8. File PythonTest_patched.bin saved
You then upload the patched file to the ESP and you are online !

Soo easy...

Caveats:
This solution is not suitable to distribute an ESP device including code, the user must patch and upload.
You must take care to use the credentials only at one place of your code (if you need it a several places, copy it to a String-variable).
In my example, the placeholders are 16 chars long, the placeholder for WiFI password is 24 chars long
You might use longer strings in your code and match them in the PythonPacher.
You might adapt the python code for more/less credentials

The python Script is on Github:
https://github.com/rin67630/ESP_Binary_ ... Patcher.py

Credit for the idea to patch the binary: inq720.
Thank you for that brilliant idea!

Next step will be to include the upload from esptool.py in the patcher, so it gets even easier...

Re: A new way to distribute an ESP solution without requiring from the user to compile...

Posted: Fri Jul 05, 2024 5:22 pm
by lbernstone
The ESP-IDF includes an nvs partition generator. You could use this to add any data you want in a private namespace, and then use Preferences to consume that data.
As far as automating, take a look at how partitions.csv is handled in platform.txt. You could have a fixed name for your secrets file, and then write a custom platform.local.txt to process it, and slipstream it into your firmware upload. I know the Espressif team has been working on a secrets method like this, but have no idea of the task status.

Re: A new way to distribute an ESP solution without requiring from the user to compile...

Posted: Fri Jul 05, 2024 5:54 pm
by rin67630
lbernstone wrote:
Fri Jul 05, 2024 5:22 pm
The ESP-IDF includes an nvs partition generator.
The aim is to use nothing at user side that the user must install, configure and learn !
Just Python, that he needs anyway to upload.
Also to keep it damn simple:
Copy three files to the desktop, start PythonPatcher.py, answer the questions, upload the patched file.
Finito !
Also on the ESP side: absolutely no code required and the non volatile memory is left free to use elsewhere.

Re: A new way to distribute an ESP solution without requiring from the user to compile...

Posted: Tue Jul 09, 2024 5:21 am
by rin67630
The PythonPatcher can now directly flash after the patching process.

Imagine: you need five devices with different credentials.
You compile once, run five times the PythonPatcher with different credentials and have immediatley your five devices online !

https://github.com/rin67630/ESP_Binary_ ... Patcher.py

Enjoy!

Next step: catch empty users' answers.
1st) with en error since it would patch nulls instead.
2nd) by retrieving the content of a previously patched file (a bit tricky...)