The objective of this post is to explain how to run a MicroPython script from a computer, using a tool called ampy. The tutorial was tested both on the ESP8266 and on the ESP32.
The objective of this post is to explain how to run a MicroPython script from the the computer, using a tool called ampy. The tutorial was tested both on the ESP8266 and on the ESP32.
This is a tool from Adafruit  and you can find here the source code. Nevertheless, as we will see, the simplest way to start using ampy is by installing it via pip. Please note that Adafruit has a lot of documentation on the tool, which I really encourage you to check.
This tool will be very useful for running MicroPython code from a file instead of having to send the commands one by one, as we have been doing in previous tutorials.
The execution of this tutorial assumes a previous installation of Python and pip on the host computer. It also assumes a previous installation of MicroPython on the ESP32 / ESP8266.
First of all, we will install ampy. To do so, just open the command line and send the following pip command:
pip install adafruit-ampy
In my case, I’m running Python 2.7 and I’m on Windows, so I get the result shown in figure 1 upon running the command. Yours may be different, depending on the Python version and operating system.
Figure 1 – Installing the ampy module.
If you prefer, you can check here the original installation instructions from Adafruit, which are very detailed.
Now, running the code of a script will be very simple. To do so, just create file called script.py on a folder of your computer and write the code bellow.
Note that you can give it other name you like. Also, the extension doesn’t need to be .py (I’ve tried putting the code in a .txt file and it works fine), but since it will contain a Python script, it is a good practice to have the correct extension.
print("Hello from my script!")
Now, just save the file and, in the command line, navigate to the folder where the file is. There, send the command written bellow, changing the values between  by the correct ones for your case.
ampy --port [YourDevicePort] run [YourFileName]
In my case, I have a ESP8266 on port COM7 and a ESP32 on port COM5, so the commands to test both of them are shown bellow, also with the correct file name.
ampy --port COM5 run script.py ampy --port COM7 run script.py
If everything goes well, you should get an output similar to figure 2, which shows the output of the script, running on both the ESP32 and the ESP8266.
Figure 2 – Output of the script, running both on the ESP8266 and on the ESP32.
Important: In the versions of the tools I’m using, the first execution of any script upon connecting the devices results in an error similar to the one shown in figure 3. It happens both on the ESP8266 and on the ESP32. Nevertheless, after this first error, subsequent executions of scripts work fine. I haven’t yet found the reason for this, but I’ll share here if I find out.
Figure 3 – Error on first attempt of executing a script.