Adding libraries to the binary image

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
preiter
Posts: 7
Joined: Tue Aug 09, 2016 5:14 pm

Adding libraries to the binary image

Post by preiter » Sat Aug 20, 2016 5:59 pm

I'm trying to add some libraries to my firmware-combined.bin so that I can access them from my code.

I'm not sure of the proper procedure. I've been copying files from micropython-lib into the scripts directory. I'm not sure what to copy, just the libraryname.py file? The whole directory?

Some libraries have sublibraries like urllib. If I just want the functions in urllib.parse, do I need urllib as well?

I also got this error:

xtensa-lx106-elf-ld: build/firmware.elf section `.irom0.text' will not fit in region `irom0_0_seg'
xtensa-lx106-elf-ld: region `irom0_0_seg' overflowed by 34020 bytes
make: *** [build/firmware.elf] Error 1

Is the space available for libraries really limited?

Is there a tutorial on this that I could read somewhere?

preiter
Posts: 7
Joined: Tue Aug 09, 2016 5:14 pm

Re: Adding libraries to the binary image

Post by preiter » Sun Aug 21, 2016 1:48 am

I think I have it figured out now.

Modules consisting of a single .py file I just put in the scripts directory. Modules with submodules, like urllib.urequest I put in a subdirectory like urllib/urequest.py.

I still can't get urllib.parse to work, but I suspect that library is not working yet.

Thanks for reading.

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

Re: Adding libraries to the binary image

Post by Roberthh » Sun Aug 21, 2016 7:03 am

Instead of the scripts directory, you can also create a modules directory and place your files there. The difference is, that Python scripts in the modules directory will be pre-compiled at image build time and then executed from flash, saving RAM during both loading and execution. I do that with scripts which are otherwise too large for parsing or execution. Whether this also works for scripts in subdirectories has to be tested.

Beta_Ravener
Posts: 35
Joined: Tue Aug 09, 2016 6:56 pm

Re: Adding libraries to the binary image

Post by Beta_Ravener » Sun Aug 21, 2016 11:29 am

I have just tested out of curiosity and sub-directories in modules seems to work too.
More info:
I have created sub-folder inside modules with following files:

Code: Select all

modules/sub/__init__.py
modules/sub/test.py
The init file is empty and I didn't test omitting it, but compiled it only has 48 bytes. The test file has following code:

Code: Select all

class Test:
	def __init__(self):
		print("Test from subfolder")
The code compiled successfully so I flashed the new firmware like usual and connected to the device. Below is the output from UART:

Code: Select all

>>>from sub.test import Test
>>> t = Test()
Test from subfolder
>>>
Edit: The __init__.py file can be omitted without problems. Saved 48 bytes per module, yay!

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

Re: Adding libraries to the binary image

Post by Roberthh » Sun Aug 21, 2016 3:24 pm

If you have your files in the modules directory, you do not need them in the scripts directory. Otherwise they would be stored twice in flash.

Post Reply