ESP32 / ESP8266 MicroPython: Running scripts from a computer

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.


Introduction

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 [1] 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.

 

The procedure

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.

ESP32 ESP8266 MicroPython installing adafruit ampy

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.

ESP32 ESP8266 MicroPython running script from computer

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.

ESP8266 ESP32 MicroPython ampy first execution error

Figure 3 – Error on first attempt of executing a script.


Related content


Related Posts

 

References

[1] https://learn.adafruit.com/micropython-basics-load-files-and-run-code/overview

5 thoughts on “ESP32 / ESP8266 MicroPython: Running scripts from a computer”

  1. Pingback: ESP32 / ESP8266 MicroPython: Uploading files to the file system | techtutorialsx

  2. Pingback: ESP32 / ESP8266 MicroPython: Uploading files to the file system | techtutorialsx

  3. Pingback: ESP32 / ESP8266 MicroPython: Running a script from the file system | techtutorialsx

  4. Pingback: ESP32 / ESP8266 MicroPython: Running a script from the file system | techtutorialsx

  5. Pingback: ESP32 / ESP8266 MicroPython: HTTP GET Requests | techtutorialsx

  6. Pingback: ESP32 / ESP8266 MicroPython: HTTP GET Requests | techtutorialsx

  7. Hello
    The error (Figure 3 – Error on first attempt of executing a script.) always happens even that ampy installation, ampy–help command done correctly
    I need some Help please

    1. Hi! Unfortunately I’ve not been able to reproduce it in my environment so unfortunately I can’t be of much help.

      My suggestion is that you open an issue on the github page of the ampy project, so the developers of the project may help you.
      https://github.com/adafruit/ampy

      Let me know if you have found assistance there.

      Best regards,
      Nuno Santos

  8. Hello
    The error (Figure 3 – Error on first attempt of executing a script.) always happens even that ampy installation, ampy–help command done correctly
    I need some Help please

    1. Hi! Unfortunately I’ve not been able to reproduce it in my environment so unfortunately I can’t be of much help.
      My suggestion is that you open an issue on the github page of the ampy project, so the developers of the project may help you.
      https://github.com/adafruit/ampy
      Let me know if you have found assistance there.
      Best regards,
      Nuno Santos

Leave a Reply

Discover more from techtutorialsx

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

Continue reading