The objective of this ESP32 / ESP8266 MicroPython Tutorial is to explain how to upload files from a computer to the MicroPython file system, using a tool called ampy. This tutorial was tested on both the ESP8266 and the ESP32.
The objective of this ESP32 / ESP8266 MicroPython Tutorial is to explain how to upload files from a computer to the MicroPython file system, using a tool called ampy. This tutorial was tested on both the ESP8266 and the ESP32. The prints shown here are for the tests on the ESP32.
Since we will need to use ampy, we are assuming a previous installation of the tool. You can check this previous tutorial, which shows how to install it using Python’s pip. This was tested on Windows 8 operating system.
Uploading a file with ampy is very straightforward as we will see. But the first thing we need to do is creating a simple file with some content on a directory of our choice. For this example, we will write two sentences:
This is the first line of content This is the second line of content
Save the file with a .txt extension (it doesn’t need to have a .txt extension, but since it is a text file we will use it). Let’s call it test.txt.
Then, open the command line and navigate to the file directory. There, to upload the file, just hit the following command, changing COM5 with the com port where your ESP8266 / ESP32 device is.
ampy --port COM5 put test.txt
Important: At the time of writing and with my setup, the first time I execute a ampy command, such as uploading a file, it results in an error being printed to the command line. After that, all the executions work fine without any error. Upon turning off and on the ESP8266 / ESP32 again, the error repeats in the first execution of a command and then everything works fine again.
This should upload the file to the file system of MicroPython. Note that the execution of this command successfully will not give any textual result, as shown in figure 1.
Figure 1 – Successful upload of the test.txt file to MicroPython file system.
Now, we will connect to the MicroPython prompt to confirm the file is actually there in the ESP32 / ESP8266 MicroPython file system. I will be using Putty to interact with it. So, after connecting, just import the os module and call the listdir function from that module, which will return the files on the current directory.
As shown in figure 2, the file uploaded is listed as output of this call.
Figure 2 – Listing the files in the current directory.
Finally, we will read the contents of the file with the commands shown bellow. If you need a detailed tutorial on how to read files with MicroPython, please refer to this previous post.
Figure 3 shows the result of executing the previous commands. Note that we read all the bytes at once, so all the content got printed in the same line, with both the carriage return and newline characters being printed. The previous post about reading a file shows other approaches on reading a file. As expected, the content we have written before is the one printed.