ESP32 / ESP8266 MicroPython: Running a script from the file system

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 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 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

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

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.

ESP32 ESP8266 MicroPython upload file with ampy

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

As can be seen bellow at figure 2, our is now on the file system, as expected.

ESP8266 ESP32 MicroPython list directory files

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.

ESP32 ESP8266 Importing MicroPython Module

Figure 3 – Importing the script file.


Related Content


Related Posts


4 Replies to “ESP32 / ESP8266 MicroPython: Running a script from the file system”

  1. Hi. Is it possible to call a php script in a remote server from micropython? I have an eap8266 nodemcu running micropython and it has a door switch attached. I would like to it to call a php script on the server whenever there is a change in status. Then the script will storw the value to a mysql server.
    I do not have an idea how to call the php script from within micropython, please.

    Liked by 1 person

    1. Hi! I’m not very familiar with PHP in particular, but it is possible to send the state change to a backend server independently if you implement it in PHP or any other language.

      You just need to define a backend endpoint that will receive a HTTP request from the ESP with the current status and then implement the backend logic to do whatever your application needs.

      But my suggestion is to develop the PHP backend and try to reach it with a tool such a Postman for testing before trying to connect the end to end system.

      Otherwise, if you have an error in your backend, it may seem that the problem is on the ESP side.

      After you succeed with those unit tests, then you can try to connect the ESP.

      Hope this helps getting you in the right track.

      Best regards,
      Nuno Santos


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s