Building an IoT device: minizing module import time to conserve battery
Posted: Sun Nov 07, 2021 7:55 pm
I'm building an IoT device that takes data every few minutes and sleeps between taking data. The ESP32 deepsleep function causes the woken CPU to reset so I practically rerun the whole import process after each deepsleep.
I think micropython takes about 2 seconds to boot. That's the time I can't optimize away. But I wrote a number of modules to handle different sensors and upload data to server. This is where I'd like some advice minimizing import time.
Right now my main.py contains the main code and it imports several other files. I've heard of compiled code also frozen code. My ESP32 has SPIRAM so there is enough memory. I would like to minimize the load time and don't mind using more RAM in the process.
It's worth pointing out that my modules import other lower-level modules to handle communication so I don't know if (cross-)compiling or freezing those modules works because I don't have basics of how python bytecode. I could do some reading on that.
I've done some reading on freezing modules or bytecode:
https://mpython.readthedocs.io/en/maste ... ained.html
It seems that freezing means to include a module (source) or compiled bytecode inside the firmware partition, which means compiling it for ESP32. I do have ESP-IDF V4 but it's carefully set up for an important project and I don't wish to disturb it. Also having to compile the firmware isn't my best option anyway if I can find other ways to include my modules and keep MP firmware generic. I also looked at how MP port on ESP32 sets up partition tables. From what I see, the partition table is fixed at 1.5MB so I have to adjust that if by adding more content I go over 1.5MB.
So I guess I'm leaning towards using the micropython cross compiler to precompile my modules into byte code and copy into the flash file system. I think this saves time to compile when importing and should not be slower than frozen bytecode by much. I can also copy the flash content of a complete system (MP+files I saved) and clone my devices. So am I on the right track? Thanks.
I think micropython takes about 2 seconds to boot. That's the time I can't optimize away. But I wrote a number of modules to handle different sensors and upload data to server. This is where I'd like some advice minimizing import time.
Right now my main.py contains the main code and it imports several other files. I've heard of compiled code also frozen code. My ESP32 has SPIRAM so there is enough memory. I would like to minimize the load time and don't mind using more RAM in the process.
It's worth pointing out that my modules import other lower-level modules to handle communication so I don't know if (cross-)compiling or freezing those modules works because I don't have basics of how python bytecode. I could do some reading on that.
I've done some reading on freezing modules or bytecode:
https://mpython.readthedocs.io/en/maste ... ained.html
It seems that freezing means to include a module (source) or compiled bytecode inside the firmware partition, which means compiling it for ESP32. I do have ESP-IDF V4 but it's carefully set up for an important project and I don't wish to disturb it. Also having to compile the firmware isn't my best option anyway if I can find other ways to include my modules and keep MP firmware generic. I also looked at how MP port on ESP32 sets up partition tables. From what I see, the partition table is fixed at 1.5MB so I have to adjust that if by adding more content I go over 1.5MB.
So I guess I'm leaning towards using the micropython cross compiler to precompile my modules into byte code and copy into the flash file system. I think this saves time to compile when importing and should not be slower than frozen bytecode by much. I can also copy the flash content of a complete system (MP+files I saved) and clone my devices. So am I on the right track? Thanks.