ESP32 MicroPython .py File

Step 1: Why? The Purpose.

Run the python program from a .py file.

Case 1: .py file is inside ESP32.

Case 2: .py file is inside ESP32/Folder.

Case 3: Autorun the .py file whenever ESP32 start/reset.

Step 2: How? The Process.

  1. Write the program in a file.

  2. Transfer the file inside esp32 and run the program.

  3. Create the file inside esp32 folder and run the program.

  4. Automatically Run the program on start/reset.

Step 3: What? The Result.

Verify "What we Get" by "Why we are Doing".

1: Write the program in python3 IDLE or notepad or notepad++.

Save the file by name "firstBlinkTest.py"

Note: 1 indent = 2 space. Caveat: the saved file should be a python (.py) file and not text (.txt) file.

2: Transfer the file into esp32 and run the program.

a) Change the directory where firstBlinkTest.py is saved.

b) Start rshell with your COM port and notepad for text edit.

copy the firstBlinkTest.py file from this folder to /pyboard i.e esp32.

change directory to /pyboard.

run repl.

c) Press the EN(reset) button on the ESP32 dev board.

d) Run the program

3: Create the file into esp32 folder and run the program.

a) 'ctrl + x' to exit from repl.

b) Remove the firstBlinkTest.py from pyboard.

c) make directory/folder named 'progFiles' in pyboard.

d) change directory to progFiles.

e) list all the files inside the folder. (Empty)

f) edit firstBlinkTest.py

g) write the program in notepad as shown above, save and close.

h) list file to verify the existance of firstBlinkTest.py file.

i) cat firstBlinkTest.py to see the content of the file.

j) start repl and press the EN(reset) button in ESP32 dev board.

h) Run the program

5: Automatically Run the program on start/reset.

a) Change directory to /pyboard.

b) List the files and folders in pyboard. (1 file 'boot.py' and 1 folder 'progFiles')

c) edit main.py in notepad.

d) Write the above program in notepad, save and close.

Here, in the program, we append the path of python-interpreter search. So that when we import firstBlinkTest,

the python-interpreter is now able to search inside the 'progFiles' folder for such program and if found: run it.

e) List file to check the existence of main.py

f) start repl and press the EN(reset) button in ESP32 dev board.

Knowledge Base:

Inside ESP32 dev board i.e pyboard, 2 very important files:

  1. 'boot.py' - this file is necessary to boot the ESP32. The file is empty. Don't delete or mesh with the file.

  2. 'main.py' - after finish booting, the MicroPython interpreter automatically execute the main.py file if present.