GoogleCloudPlatform / iot-core-micropython

Apache License 2.0
52 stars 14 forks source link

ESP8266 Compatibility #16

Open JoeToeniskoetter opened 3 years ago

JoeToeniskoetter commented 3 years ago

Thank you for your work on this library. I tried to port this over to a generic ESP8266 with 4m of flash but am hitting memory errors. Is it possible to make this compatible with the ESP8266 module?

Thanks

ferrarimarco commented 3 years ago

Hi @JoeToeniskoetter !

Can you share more details about your issue?

JoeToeniskoetter commented 3 years ago

Hi @ferrarimarco , sure thing.

Here is the error message I receive when attempting to run the example code

Traceback (most recent call last):
  File "main.py", line 14, in <module>
  File "third_party/rsa/__init__.py", line 1, in <module>
MemoryError: memory allocation failed, allocating 439 bytes

line 14 in my main.py is importing the rsa module

from third_party import rsa

Seems to me that the rsa modules are just too large for the ESP8266 to handle. Running esptool's flash_id returns the following information about my device.

Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
Manufacturer: 20
Device: 4016
Detected flash size: 4MB

Somethings I've tried so far:

  1. Removing unused imports from main.py. These included the os, socket, ssl and esp32 modules.
  2. Using the mpy-cross module to precompile the .py files to .mpy https://github.com/micropython/micropython/tree/master/mpy-cross
  3. Importing the rsa module first (this was suggested on a micropython forum).
  4. Using the gc module and calling gc.collect() after each import statement

Thanks!

ferrarimarco commented 3 years ago

@gguuss: Do you have more info to share about possible workarounds? IIRC, the rsa module is needed to generate the key pair for IoT core.

gguuss commented 3 years ago

I'm unsure how we could shrink RSA but you could potentially use the ES JWT by embedding something like the Arduino JWT library and then wrapping the C library. Adding @C4 in case he's investigated a solution that fits the ESP8266.

gguuss commented 3 years ago

Also, make sure that you're making full use of the flash, it's possible the 8266 is being partitioned with an OTA option.

JoeToeniskoetter commented 3 years ago

Thanks @gguuss ! I will try making use of the full flash size. Do you know how to specify partitions sizes like that when flashing the esp8266?

varna9000 commented 3 years ago

Try to compile the modules into byte code (.mpy) and then import them. This saves memory. Also a step further is to byte freeze the modules into the micropython .bin, but that requires compiling the whole micropython bin. The first option is more accessible.