Firmware build with auto-run of Python module?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Firmware build with auto-run of Python module?

Post by pythoncoder » Sat Jun 17, 2017 2:42 pm

Is it possible to produce a build of firmware which runs user code at startup? I am nearly there: my modules are frozen as bytecode but in order for it to run on power-up I have to copy the one-line main.py to the filesystem. Freezing main.py as bytecode or as a frozen script produces an ENOENT error.

I'd like a build which simply needs to be flashed to the ESP8266. I can't help thinking I'm missing something obvious here...
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Firmware build with auto-run of Python module?

Post by Roberthh » Sat Jun 17, 2017 3:17 pm

There is _boot.py in modules, which is the first script executed at startup. Maybe you can start you code from there.
The related section in main.c reads as:

Code: Select all

    pyexec_frozen_module("_boot.py");
    pyexec_file("boot.py");
    if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
        pyexec_file("main.py");
    }
Or you change the calls for boot.py or main.py in main.c into pyexec_frozen_module().

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Firmware build with auto-run of Python module?

Post by pythoncoder » Sun Jun 18, 2017 7:06 am

Thanks for that, which pointed me to this solution. I modified the _boot.py in my project's frozen directory to read

Code: Select all

# Existing code omitted up to here:
    inisetup.setup()

try:
    uos.stat('/main.py')
except OSError:
    with open("/main.py", "w") as f:
        f.write("""\
import mqtt
""")

gc.collect()
Erase the flash before installing the firmware.
Peter Hinch
Index to my micropython libraries.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Firmware build with auto-run of Python module?

Post by pfalcon » Sun Jun 18, 2017 1:51 pm

pythoncoder wrote: I can't help thinking I'm missing something obvious here...
The docs is the best place to look for answers, current or future, novice or experienced user. http://docs.micropython.org/en/latest/e ... ot-process
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Firmware build with auto-run of Python module?

Post by pythoncoder » Mon Jun 19, 2017 5:38 am

pfalcon wrote:The docs is the best place to look for answers, current or future, novice or experienced user. http://docs.micropython.org/en/latest/e ... ot-process
Thanks for that.

Given that _boot.py is frozen bytecode why does the boot process ignore a frozen main.py file? Fixing this would simplify issuing binary builds.
Peter Hinch
Index to my micropython libraries.

Post Reply