Q: Where to put main.py in Custom Firmware?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
cabalist
Posts: 7
Joined: Thu Jun 02, 2016 7:37 pm

Q: Where to put main.py in Custom Firmware?

Post by cabalist » Mon Jul 11, 2016 1:03 am

Hello,

I am using the NodeMCU to build about 40 units of a device. I am building the ESP8266 firmware for these from source. From what I am reading it appears that the custom libraries I want to use (i.e. urllib.urequest, gzip, my own libraries) are placed in the scripts/ directory. This works and they are all importable.

However to get these devices to immediately start executing code they need a main.py and/or boot.py. I have found the code that creates the default boot.py in scripts/inisetup.py. I can edit this code and write my main.py there but I am curious if there is a better way?

As succinctly as I can put it: Where do I put main.py if I am rolling my own firmware?

Thanks!

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

Re: Q: Where to put main.py in Custom Firmware?

Post by Roberthh » Mon Jul 11, 2016 7:30 am

main.py is a proper place to put your start code in. That resides in the flash file system. If you prefer to have it in the scripts folder, which moves into the frozen files space, then you have to change esp8266/main.c and rebuild.
But bothing prevents you from putting your startup code in _boot.py. If you look into main.c, you'll see:

Code: Select all

#if MICROPY_MODULE_FROZEN
    pyexec_frozen_module("_boot.py");
    pyexec_file("boot.py");
    pyexec_file("main.py");
#endif
which means, that _boot.py, boot.py and main.py are simply executed one after another.

B.T.W.: I'm not sure whether boot.py and main.py should only be execute whan MICROPY_MODULE_FROZEN is set. Maybe that's just a leftover from an earlier development phase.

cabalist
Posts: 7
Joined: Thu Jun 02, 2016 7:37 pm

Re: Q: Where to put main.py in Custom Firmware?

Post by cabalist » Mon Jul 11, 2016 7:45 am

Thanks for your reply! That is exactly what I needed to know.

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

Re: Q: Where to put main.py in Custom Firmware?

Post by Roberthh » Mon Jul 11, 2016 11:04 am

If in the code snippet above you replace the calls to pyexec_file() by pyexec_frozen_module(), then you can put main.py and boot.py into the scripts folder. Or you add your own pyexec_frozen_module("mycode.py") into esp8266/main.c. For modules in scripts/, you have to rebuils the binary anyhow. Seel also deshipu's comment in https://github.com/micropython/micropython/issues/2230.

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

Re: Q: Where to put main.py in Custom Firmware?

Post by markxr » Mon Jul 11, 2016 10:49 pm

You can also drop a dos-fs image (must have a sector size=4096) at offset 561152. This image can contain boot.py and whatever other Python or data files you need.

That's what I'm doing.

But this is really not well documented, and I suspect that this offset may be subject to future change.

lnsri22
Posts: 23
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Q: Where to put main.py in Custom Firmware?

Post by lnsri22 » Mon Dec 17, 2018 1:08 pm

[quote=Roberthh post_id=11945 time=1468222217 user_id=601]
main.py is a proper place to put your start code in. That resides in the flash file system. If you prefer to have it in the scripts folder, which moves into the frozen files space, then you have to change esp8266/main.c and rebuild.
But bothing prevents you from putting your startup code in _boot.py. If you look into main.c, you'll see:
[code]#if MICROPY_MODULE_FROZEN
pyexec_frozen_module("_boot.py");
pyexec_file("boot.py");
pyexec_file("main.py");
#endif
[/code]
which means, that _boot.py, boot.py and main.py are simply executed one after another.

B.T.W.: I'm not sure whether boot.py and main.py should only be execute whan MICROPY_MODULE_FROZEN is set. Maybe that's just a leftover from an earlier development phase.
[/quote]
:) Thank you!!. It worked like a charm!!

Post Reply