ESP32 MicroPython: Executing scripts with uPyCraft

The objective of this post is to explain how to execute MicroPython scripts using the uPyCraft IDE. The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

 

Introduction

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.

ESP32 uPyCraft IDE create new script.png

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.

ESP32 uPyCraft script window.png

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.

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.

uPyCraft ESP32 save script file

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.

Click connect button uPyCraft IDE

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.

ESP32 uPyCraft upload and execute MicroPython script file.png

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:

import os
os.listdir()

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.

uPyCraft upload script in file system.png

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:

import testScript

We should get the same print we obtained before after uploading the file, as indicated in figure 7.

ESP32 uPyCraft import script file.png

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:

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.

uPyCraft remove file from directory ESP32.png

Figure 8 – Removal of the previously uploaded script file.

14 thoughts on “ESP32 MicroPython: Executing scripts with uPyCraft”

  1. Pingback: ESP32 MicroPython: HTTP Webserver with Picoweb | techtutorialsx

  2. Pingback: ESP32 MicroPython: HTTP Webserver with Picoweb | techtutorialsx

    1. Hi,

      In the uPyCraft version I’m using, the board doesn’t appear as supported.

      Nonetheless, since the Microcontroller is the same (the ESP32), my guess is that it should work.

      Worse case scenario, you may run into some troubles by flashing the MicroPython using the uPyCraft, since your board’s parameters may differ.

      But I think that at least you should be able to connect to the prompt if you manually flash MicroPython before.

      But as said, these are just guesses. Unfortunately I don’t have one of those boards, so I can’t confirm.

      Let us know you have the opportunity to test it.

      Best regards,
      Nuno Santos

    1. Hi,
      In the uPyCraft version I’m using, the board doesn’t appear as supported.
      Nonetheless, since the Microcontroller is the same (the ESP32), my guess is that it should work.
      Worse case scenario, you may run into some troubles by flashing the MicroPython using the uPyCraft, since your board’s parameters may differ.
      But I think that at least you should be able to connect to the prompt if you manually flash MicroPython before.
      But as said, these are just guesses. Unfortunately I don’t have one of those boards, so I can’t confirm.
      Let us know you have the opportunity to test it.
      Best regards,
      Nuno Santos

  3. Is it have stand-alone mode supporting ??
    I mean, when I power up my board by external power supply (not connecting with computer), the program is doesn’t work.

    1. Hi!

      If you have a main.py file in your file system, it should run when the board powers on, after the boot.py script runs.

      This main .py can have the code you need or call other modules you upload to the file sistem.

      You can check in this tutorial how to use the main.py to connect to the WiFi network on power on:
      https://techtutorialsx.com/2017/06/06/esp32-esp8266-micropython-automatic-connection-to-wifi/

      I’ve not been using MicroPython in a while, but I think this main.py trick still works with newer versions.

      Best regards,
      Nuno Santos

  4. Is it have stand-alone mode supporting ??
    I mean, when I power up my board by external power supply (not connecting with computer), the program is doesn’t work.

    1. Hi!
      If you have a main.py file in your file system, it should run when the board powers on, after the boot.py script runs.
      This main .py can have the code you need or call other modules you upload to the file sistem.
      You can check in this tutorial how to use the main.py to connect to the WiFi network on power on:
      https://techtutorialsx.com/2017/06/06/esp32-esp8266-micropython-automatic-connection-to-wifi/
      I’ve not been using MicroPython in a while, but I think this main.py trick still works with newer versions.
      Best regards,
      Nuno Santos

  5. Pingback: Micro:bit uPython: using external pins for serial communication – techtutorialsx

  6. Pingback: Micro:bit uPython: using external pins for serial communication – techtutorialsx

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading