ESP32 / ESP8266 MicroPython: Uploading files to the file system

The objective of this post 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 post 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. If you prefer, you can check Adafruit’s installation instructions here. 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.

ESP32 ESP8266 MicroPython Upload file with ampy

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.

ESP32 ESP8266 MicroPython list files from a directory

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.

file = open("test.txt", "r")
file.read()
file.close()

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.

ESP2 ESP8266 MicroPython reading content from file

Figure 3 – Reading the previously uploaded file.


Related Posts

Advertisements
This entry was posted in ESP32, ESP8266 and tagged , , , , , . Bookmark the permalink.

7 Responses to ESP32 / ESP8266 MicroPython: Uploading files to the file system

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

  2. lpasqualis says:

    Thank you for sharing this information. I’ll need to try it out.

    Liked by 1 person

  3. Pingback: ESP32 / ESP8266 MicroPython: Automatic connection to WiFi | techtutorialsx

  4. Pingback: ESP32 MicroPython: Using SHA-256 | techtutorialsx

  5. Rum Lyen says:

    Hi,
    Would you have enough time for a post like uploading file from the ESP32’s interfaced SD card to the server using Wifi network?
    Thank you
    Rum

    Liked by 1 person

    • antepher says:

      Hi!

      Thanks for the suggestion.

      I’m currently taking a look at the BT stack, after that I will try to take a look about sending data through WiFi from the file system / SD card.

      Best regards,
      Nuno Santos

      Like

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s