ESP32 / ESP8266 MicroPython Tutorial: Uploading files to the file system
DFRobot
Apr 26 20181516
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.
Introduction
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.
The procedure
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.
import os
os.listdir()
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.
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.