Ceci est une ancienne révision du document !
Sometimes, when you update your microcontroller with a new version of MicroPython, your entire memory gets erased. When that happens, of course, all of the code that you worked on gets erased as well. If you are using Thonny, you have the ability to copy any files that you want to keep safe off of the Microcontroller onto your main computer. But only one file at a time. I found a neat utility called mpfshell that can make your life a whole lot easier. You can find their repository at https://github.com/wendlers/mpfshell, and it’s well worth your time to download, install and learn how to use it. According to the repository main page, the utility is “A simple shell based file explorer for ESP8266 and WiPy Micropython based devices.” Just so you know, mpfshell works on more than just the ESP8266 and the WiPy. I’ve tested it on the ESP8266, the ESP32, and the Raspberry Pi Pico, and it works just fine on all three devices. For complete transparency, I haven’t tried all the commands on all three Microcontrollers, because I use it only to copy and push files to and from the Microcontroller devices so far. Let’s examine how you would use it.
Of course, you need to install the program to be able to use it, which is easy since it is on PyPi. In a terminal, you need to use only pip. On your main machine, open a terminal and type $ pip install mpfshell You can also download the entire repository and install it from that. Once you have it installed, you will want to open a terminal in the folder where you want to either copy the files from the main computer to the Microcontroller, or from the Microcontroller to the main computer. You’ll want to know what serial port your device is connected to before you start (see above). Usually, the device will be on either /dev/ttyUSB0 or /dev/ttyACM0, but the actual port your device connects to may change depending on what you have connected to your system at that moment. In the example above, my machine is connected to an ESP32 on /dev/ttyUSB0. Now, just start the program… $ mpfshell Micropython File Shell v1.1.2, sw@kaltpost.de & junhuanchen@qq.com & lht856@foxmail.com – Running on Python 3.7 using PySerial 3.4 – mpfs [/]>
Next, you have to tell mpfshell to connect to the device on the correct serial port with the open {port} command. I’ve seen this just seems to lock up sometimes. If that happens just do a <Ctrl> C in the terminal and start the shell again. It almost always works the second time. mpfs [/]> open /dev/ttyUSB0 Connected to esp32 mpfs [/]> Notice that mpfshell tells you what type of device you are connected to. This can be very helpful if you have multiple devices connected. Now that you are connected to your microcontroller, do an ls to find out what files and folders are on your device. mpfs [/]> ls Remote files in '/': boot.py esp32i2cscan.py gfx.py image1.py image2.py rnerd_ssd1306_esp32.py rnerd_ssd1306_esp32_graphics1.py ssd1306.py mpfs [/]> Of course, the files on your Microcontoller will be different from what’s on mine, but you get the idea.
Now, let’s do a complete copy from the ESP32 (or whatever your Microcontroller is) to the computer’s hard drive. To do this, you need to use the mget command. However, you need to use a special command. The command is mget .*\.py . You might recognize that this command uses a regular expression (regex) to get all files. mpfs [/]> mget .*\.py * get boot.py * get esp32i2cscan.py * get gfx.py * get image1.py * get image2.py * get rnerd_ssd1306_esp32.py * get rnerd_ssd1306_esp32_graphics1.py * get ssd1306.py mpfs [/]> To do a bulk copy from your computer to the microcontroller, you can use the mput command. mpfs [/]> mput .*\.py * put ssd1306.py * put boot.py * put image2.py * put rnerd_ssd1306_esp32_graphics1.py * put rnerd_ssd1306_esp32.py * put image1.py * put esp32i2cscan.py * put .python-version * put gfx.py mpfs [/]>
You can copy a single file by just using the get or put command. mpfs [/]> get boot.py mpfs [/]> put boot.py boot.py mpfs [/]> If you forget what command you might need to use at any given time, simply type help. mpfs [/]> help Documented commands (type help <topic>): ======================================== EOF close execfile lef ls mpyc put r runfile c e get lexecfile md mrm pwd repl v cat ef help lls mget o q rf view cd exec lcd lpwd mput open quit rm mpfs [/]>
You can also get help on any of the commands… mpfs [/]> help mget mget <SELECTION REGEX> Download all remote files that match the given regular expression. The local files will be named the same as the remote files. “mget” does not get directories, and it is not recursive. mpfs [/]> And to leave mpfshell, simply type quit. mpfs [/]> quit greg@earth: $ I sincerely hope that you find this utility as helpful as I do. Until next time, as always; stay safe, healthy, positive and creative!