The objective of this post is to explain how to execute MicroPython scripts using the uPyCraft IDE. If you haven’t yet set up and tested uPyCraft, please consult this previous post. Although we have access to the MicroPython prompt to send commands, writing an application is much more convenient if we can write our MicroPython code in a file and then execute it. This is what we are going to do in this simple example. The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.
Testing the procedure
In order to execute a MicroPython script, we will need to create a new file where we will write our script’s code. To create a new file, we just need to click the button highlighted in figure 1, on the uPyCraft IDE.
Figure 1 – Creating a new MicroPython script file.
After clicking the button, a new file tab should appear, as indicated in figure 2. There, we can write our MicroPython script to later run on the ESP32.
Figure 2 – uPyCraft script editor.
The script we are going to write is a very simple hello world message and is shown bellow. You can copy and past to try it on your environment.
1 print ("Hello world from script")
Then, we need to save the file, so we can upload it to the ESP32 board. To do so, just click the icon highlighted in figure 3 and specify a name in the popup window. Finally, click the “ok” button.
Figure 3 – Saving the MicroPython script file.
Now, to upload the file, just click the icon highlighted in figure 4. It should connect to the MicroPython prompt. If you haven’t yet done the initial board configuration and flashing of the MicroPython firmware, please consult this previous post.
Figure 4 – Connecting to the MicroPython prompt.
Finally, just hit the upload button, indicated in figure 5. After that, the script should get uploaded to the ESP32 and after that the script will run, showing the output on the prompt at the bottom of the IDE window.
Figure 5 – Successful upload and execution of MicroPython script file.
Note that the file with the script will be uploaded to the ESP32 and thus will be persisted on the file system. To confirm that, we just need to go to the prompt and send the following commands:
1 import os
As indicated in figure 6, the testScript.py file we uploaded is persisted on the file system and will stay there even after disconnecting from the board.
Figure 6 – Script file persisted on file system of the ESP32.
We can re-run the file from the prompt just by importing its content with the command shown bellow: 1 import testScript We should get the same print we obtained before after uploading the file, as indicated in figure 7.
Figure 7 – Re-running the script file uploaded to the ESP32.
If you don’t want to leave the uploaded file in the file system, you can simply send the following command to delete it, assuming a previous import of the os module, as we did before: 1 os.remove('testScript.py') Figure 8 illustrates the removal of the previously uploaded file. Note that in that example I’ve disconnected the board and connected again, just to confirm that the file stays in the file system.
Figure 8 – Removal of the previously uploaded script file.
NOTE: This article is written by Nuno Santos who is an kindly Electronics and Computers Engineer. live in Lisbon, Portugal. you could check the original article here. He had written many useful tutorials and projects about ESP32, ESP8266, If you are interested, you could check his blog to know more.