How to use MicroPython to execute a piece script but keep the reset of the project in ESP-IDF

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
zhenghaku
Posts: 6
Joined: Wed Jul 15, 2020 3:47 pm

How to use MicroPython to execute a piece script but keep the reset of the project in ESP-IDF

Post by zhenghaku » Wed Jul 15, 2020 4:20 pm

Hi, I am new to MicroPython. I think it is great, and can make my project shines.

What I want to achieve is:
The majority of my project will still be in C and ESP-IDF, like the network and all the hardware stuff. But some functions will be written in python and interpreted in runtime. So users can change those function in runtime by editing the python script via web browser. This does not require update the firmware or even reboot the board.

After researching, it looks like MicroPython can achieve this. Since it can do REPL.

After reading the micropython/ports/esp32/main.c, my guess was to use this as my base project and remove the mp_task .
Then uses the pyexec_file_if_exists("main.py"); to execute my python code. (There are no hardware related calls in the script)

Is this the right way? Anything in mp_task can must be called before pyexec_file_if_exists("main.py");

And most importantly how can I exchange data between C and the python script?

Thank you very much. :D

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to use MicroPython to execute a piece script but keep the reset of the project in ESP-IDF

Post by jimmo » Thu Jul 16, 2020 1:43 am

Yes, depending on what you want to be able to do from MicroPython (i.e. if you want hardware access via the "machine" module, etc) then you have different options.

You might also find this to be a suitable option -- embed MicroPython as a library in your ESP-IDF project -- https://github.com/micropython/micropython/pull/5964
(I'm still working on this PR, need to adapt the approach slightly based on review feedback, but maybe the idea is useful to you).

Either way the way you can exchange data is via writing Python bindings that will be accessible to your Python code. See for example ports/esp32/modesp.c.

zhenghaku
Posts: 6
Joined: Wed Jul 15, 2020 3:47 pm

Re: How to use MicroPython to execute a piece script but keep the reset of the project in ESP-IDF

Post by zhenghaku » Thu Jul 16, 2020 3:28 am

Thank you very much, I will definitely check this out. It seems like just what I need.

Post Reply