MicroPython on ESP32 with SPIRAM support

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Sun Dec 31, 2017 12:54 pm

unfortunately that didn't work. It seems that once you import the module it puts it in memory and doesn't care if it has been deleted from the flash file system or not and it doesn't reload it when imported again.

It would be great if when you import a module that has already been imported it will remove the old one from memory and reload the new one. This will make for fast debugging of programs as I have note++ open in 1 window editing the file over ftp then I can have putty/telnet open in another window. I can jump to note++ and edit a program then jump back to putty window and press up-arrow enter to re-import the module and run it then if there is still a bug I jump back to note++ make a change then jump to putty window and press up-arrow then enter to re run the new edited program.

or something like what ampy does with serial. ampy --port /com4 run script.py

This is how I write and debug my programs with the Raspberry Pi

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Sun Dec 31, 2017 1:11 pm

or possibly a clear memory function that will clear all variables and modules in python memory.

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: MicroPython on ESP32 with SPIRAM support

Post by loboris » Sun Dec 31, 2017 1:26 pm

This works:

I've created a small module test.py:

Code: Select all

def fn(n):
    print("TEST_1", n)

fn(7)
Then I've imported it:

Code: Select all

>>> import sys
>>> import test                                                                                                                             
TEST_1 7                                                                                                                                    
>>>                                                                                                                                         
>>> test.fn(999)                                                                                                                            
TEST_1 999                                                                                                                                  
>>> 
Edit module source, change "TEST_1" to "TEST_CHANGED"
Delete the imported module:

Code: Select all

>>> del sys.modules['test']                                                                                                                 
>>> 
Import again:

Code: Select all

>>> import test                                                                                                                             
TEST_CHANGED 7                                                                                                                              
>>>                                                                                                                                         
>>> test.fn(888)                                                                                                                            
TEST_CHANGED 888                                                                                                                            
>>> 
del module_name does not work, but del sys.modules['module_name'] does.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Sun Dec 31, 2017 1:46 pm

@loboris Your a freaking genius :)
I just tried it and it works perfectly :)

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

Re: MicroPython on ESP32 with SPIRAM support

Post by Roberthh » Sun Dec 31, 2017 2:51 pm

What works for me is:
If you have a module, let's call it "my_module". Then you can do the following:

Code: Select all

import sys
sys.modules.pop("my_module")
then you can reimport that module again.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Sun Dec 31, 2017 10:09 pm

It looks like note++ has a plugin called nppftp that allows you to edit remote files over ftp directly without needing filezilla.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Sun Dec 31, 2017 10:39 pm

I have installed the nppftp plugin for note++ and connected it to the esp32 use passive binary ftp insecure and it connects fine and shows me the contents of the esp32 and I can create directories and remove directories and can delete files on the esp32 but it fails when it tries to upload or down load.

Does anyone have any idea why thus is so. I have note++ as exempt from firewall. could it because of the chunk size setting of the esp32 ftp??

edit after a little googling it could be that there maybe a problem with passive downloads in the nppftp plugin

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Sun Dec 31, 2017 11:01 pm

@laboris

There seems to be a little bug in the telent in AP mode. I can use ftp with filezilla both in station mode over my router and in access point directly but putty will only connect to telent in station mode over my router and fails to connect in access point mode directly.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MicroPython on ESP32 with SPIRAM support

Post by OutoftheBOTS_ » Mon Jan 01, 2018 1:41 am

del sys.modules['my_module'] vs sys.modules.pop("my_module")

Does del sys.modules return the memory to the heap to be able to be reallocated or does it just set it to zeros ??

I assume using sys.modules.pop will remove the module from the list and return the memory to the heap??

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

Re: MicroPython on ESP32 with SPIRAM support

Post by pythoncoder » Mon Jan 01, 2018 9:00 am

In this post Dave Hylands suggested this piece of code, saved in boot.py, to implement reload.

Code: Select all

def reload(mod):
    import sys
    mod_name = mod.__name__
    del sys.modules[mod_name]
    return __import__(mod_name)
Peter Hinch
Index to my micropython libraries.

Post Reply