ESP32 / ESP8266 MicroPython Tutorial: Running a script from the file system
DFRobot
Apr 26 20181276
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.
Introduction
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.
The procedure
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.
def echo(content):
print (content)
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.
import os
os.listdir()
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.
import script
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.
This article takes a closer look at each of these devices, comparing them across key areas like Python support, ease of use, Python programming tools, pin design, and hardware projects.
Deciding between Scratch and Python for your child's coding journey? This article compares both languages based on learning background, goals, methods, and difficulty.