The objective of this post is to explain how to run a script from MicroPython’s file system. This was tested on both the ESP32 and the ESP8266.
The objective of this ESP32 / ESP8266 MicroPython Tutorial is to explain how to run a script from MicroPython’s file system. This was tested on both the ESP32 and the ESP8266. The prints shown here were taken from the tests performed with the ESP32.
Note that this will be different from the previous tutorial where we ran a script from our computer on the MicroPython executing in a ESP. In this case, the script will be in MicroPython’s file system.
Naturally, this will be very useful for us to be able to execute Python code without the need to be repeating commands on the prompt.
This tutorial assumes a previous installation of the MicroPython support for the device used (ESP32 and/or ESP8266), and a previous installation of Python and ampy on the host computer. All the procedures shown here were tested on Windows 8.
First of all, we will create a MicroPython script to upload to our ESP device. It will be very basic and we will only create an echo function that receives some content as input parameter and prints it.
We will also print a simple message, to show that both invocations of functions defined at the file and other functions from Python work well. Check the file script bellow.
print("Running a script from the file system!")
echo("Invoking a function")
Save the file in a directory of your choice and with a .py extension. You can name it whatever you like, but for this example we will call it script.py.
Now we will upload the file to the ESP8266 / ESP32 using ampy. To do so, just open the command line, navigate to the folder where the file is and hit the command shown bellow. If you need a detailed tutorial on how to upload files with ampy, please check this post.
ampy --port COM5 put script.py
Note that you need to substitute COM5 by the serial port where your device is. Also, if you used another name for the file, don’t forget to change it. Check the expected result at figure 1.
Figure 1 – Uploading the script file with ampy.
Now we need to connect to the MicroPython prompt to send some commands. I will be using Putty, but you can use other software of your choice. Once the connection is established, we will confirm that our file is on the file system.
As can be seen bellow at figure 2, our script.py is now on the file system, as expected.
Figure 2 – Listing the files in the current directory of the file system.
Finally, to run the script, we just need to import it, since it will act like a regular Python module. Upon importing, all the executable statements will run, and the functions defined will be available.
script.echo("Running the imported function")
As can be seen in figure 3, upon importing the module, we get the output from the executable statements we previously defined. Also note that repeating the import will not run those statements again. We can then access the functions defined in our file by calling filename.functionName.