fivdi
Posts: 447
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

mpremote -- MicroPython remote control

Sun Jun 20, 2021 8:54 am

MicroPython v1.16 was released last Friday. It comes with a new command line tool called mpremote which is intended to be the main way to remotely control a MicroPython-based device from the command line.

For further information see the release notes for MicroPython v1.16 at https://github.com/micropython/micropyt ... /tag/v1.16
and the documentation for mpremote at https://pypi.org/project/mpremote/

Dragon12
Posts: 93
Joined: Thu Jul 18, 2013 10:33 am

Re: mpremote -- MicroPython remote control

Wed Jun 23, 2021 2:41 am

This looks interesting. I've searched for information on usage but nothing has turned up although I did find a list of commands.

Entering "mpremote" at the terminal simply locks up the terminal window and informs me how to exit, which is the only command that does anything. I wonder if a usage guide has been written?

User avatar
scruss
Posts: 4170
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: mpremote -- MicroPython remote control

Wed Jun 23, 2021 3:18 am

yeah, the total lack of docs took me by surprise: both man and pydoc3 had nothing.

I found something - but not much - here: https://github.com/micropython/micropython/blob/master/tools/mpremote/README.md
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

fivdi
Posts: 447
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: mpremote -- MicroPython remote control

Wed Jun 23, 2021 9:50 am

I have used mpremote a little and haven't had too many issues yet. The command that I like most so far is "mpremote mount ." which makes it look as if the current directory on the development machine had been copied to the microcontroller flash, but it hasn't, and everything works as if it had been copied.

For example, here is a session that uses mpremote which makes the contents of the current directory look like it has been copied to the microcontroller flash. When run on the microcontroller, the command "os.listdir()" finds the contents of the current directory on the development machine and those files can be imported.

Code: Select all

pi@raspberrypi:~/mp $ ls
mcp9808.py  test.py
pi@raspberrypi:~/mp $ ~/.local/bin/mpremote mount .
Local directory . is mounted at /remote
Connected to MicroPython at /dev/ttyACM0
Use Ctrl-] to exit this shell
>
MicroPython v1.16 on 2021-06-18; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> import os
>>> os.listdir()
['mcp9808.py', 'test.py']
>>> import test
0
1
2
3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 7, in <module>
KeyboardInterrupt: 
>>>

Here is the same session with screen (minicom could also be used.) As can be seen and as is to be expected, the command "os.listdir()" finds no files.

Code: Select all

pi@raspberrypi:~/mp $ ls
mcp9808.py  test.py
pi@raspberrypi:~/mp $ screen /dev/ttyACM0 115200
>>> 
MPY: soft reboot
MicroPython v1.16 on 2021-06-18; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> import os
>>> os.listdir()
[]
>>> 

Dragon12
Posts: 93
Joined: Thu Jul 18, 2013 10:33 am

Re: mpremote -- MicroPython remote control

Thu Jun 24, 2021 4:59 am

Thank you fivdi and scuss. I've had a play with some of the commands, now that I understand how they're used, however I'm not sure what use I can make of them. I'll continue to experiment.

hippy
Posts: 10194
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: mpremote -- MicroPython remote control

Fri Jun 25, 2021 11:31 am

fivdi wrote:
Wed Jun 23, 2021 9:50 am
The command that I like most so far is "mpremote mount ." which makes it look as if the current directory on the development machine had been copied to the microcontroller flash, but it hasn't, and everything works as if it had been copied.
What impressed me is that files written from MicroPython on the Pico appear in that directory.

I am going to look into how they have done it because it looks like it could provide a means to have MicroPython itself read and write files on a host just by running a daemon, no need for 'mpremote' itself - I'm back to subverting 'open()' I suspect.

One thing I would point out is I had ignored this thread for a week because I imagined "mpremote -- MicroPython remote control" would be describing some sort of IR remote control written in MicroPython. Maybe others have too. A title like "mpremote - Remote control of MicroPython" might be better, would have had me read this thread sooner.

hippy
Posts: 10194
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: mpremote -- MicroPython remote control

Fri Jun 25, 2021 11:42 am

Dragon12 wrote:
Wed Jun 23, 2021 2:41 am
Entering "mpremote" at the terminal simply locks up the terminal window and informs me how to exit, which is the only command that does anything.
I had the same problem when I first tried it. I think it was Ctrl-D then Ctrl-B that got me to the REPL.

Having tried to interact with the REPL from my own Python code all I can say is "it's tricky", and it doesn't surprise me it doesn't always work in some cases. Hopefully those cases will be identified and some way figured out to make them work.

I cannot remember exactly what I had done, a "mount" which hung I think was a part of that, but the next time I ran 'mpremote' it didn't find '/dev/ttyACM0'. Rebooted my Pi and it was fine again.

fivdi
Posts: 447
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: mpremote -- MicroPython remote control

Fri Jun 25, 2021 1:19 pm

hippy wrote:
Fri Jun 25, 2021 11:31 am
What impressed me is that files written from MicroPython on the Pico appear in that directory.

I was wondering if this would work. Good to know that it does.
hippy wrote:
Fri Jun 25, 2021 11:31 am
One thing I would point out is I had ignored this thread for a week because I imagined "mpremote -- MicroPython remote control" would be describing some sort of IR remote control written in MicroPython. Maybe others have too. A title like "mpremote - Remote control of MicroPython" might be better, would have had me read this thread sooner.

I actually thought something similar myself but ended up giving the thread the title that the MicroPython people themselves use (see https://pypi.org/project/mpremote/)

hippy
Posts: 10194
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: mpremote -- MicroPython remote control

Fri Jun 25, 2021 1:31 pm

I guess that's fair enough.

One thing I want to do is add "-h", "--help", "--version", and some extra commands to make it more 'ampy' like -

Code: Select all

mpremote put <file>   -->   mpremote cp <file> :
mpremote get <file>   -->   mpremote cp :<file> .
This is fun - replaces the original source on the host with an empty file ...

Code: Select all

mpremote mount . cp file.py :

hippy
Posts: 10194
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: mpremote -- MicroPython remote control

Sat Jun 26, 2021 12:08 pm

Having played a bit with 'mpremote' I am in two minds about it. There are too many hangs when it launches for my liking, opening the /dev/ttyACM link but then becoming completely unresponsive without presenting the REPL. Sometimes not exiting on Ctrl-] which means having to resort to 'sudo kill' to end it.

Like other MicroPython tools it doesn't do a lot of error checking or input validation, will simply hang or fail with a Python exception if the command isn't correct or there are problems doing what's asked of it.

But I did manage to implement my desired "put" and "get" so I can perhaps say goodbye to 'ampy' ...

~/pico/micropython/tools/mpremote/mpremote/main.py

Code: Select all

       mpremote fs <command> <args...>  -- execute filesystem commands on the device
 +     mpremote put <file>              -- put local file to device
 +     mpremote get <file>              -- get file from device to current directory
       mpremote repl                    -- enter REPL

Code: Select all

       "rmdir": "fs rmdir",
 +     "put": "fs put",
 +     "get": "fs get",
       "df": [

Code: Select all

   def do_filesystem(pyb, args):
       ...
              pyboard.filesystem_command(pyb, ["cp", os.path.join(dir, file), ":" + dir + "/"])
 +     elif args[0] == "put":
 +        pyboard.filesystem_command(pyb, ["cp", args[1], ":"])
 +     elif args[0] == "get":
 +         pyboard.filesystem_command(pyb, ["cp", ":" + args[1], "."])
       else:
           pyboard.filesystem_command(pyb, args)
       args.clear()

hippy
Posts: 10194
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: mpremote -- MicroPython remote control

Sat Jun 26, 2021 2:23 pm

To fix the "mpremote connect list" bug when it crashes because a serial port like '/dev/ttyAMA0' doesn't provide the data the code expects, line 166 -

Code: Select all

            # List attached devices.
            for p in sorted(serial.tools.list_ports.comports()):
                print(
                    "{} {} {:04x}:{:04x} {} {}".format(
                        p.device, p.serial_number, p.pid, p.vid, p.manufacturer, p.product
                    )
                )

Code: Select all

            # List attached devices.
            for p in sorted(serial.tools.list_ports.comports()):
                try:
                    device = p.device
                    if device == None:
                        device = "<device:none>"
                except:
                    device = "<device:unknown>"
                try:
                    pid = p.pid
                    if pid == None:
                        pid = 0
                except:
                    pid = 0
                try:
                    vid = p.vid
                    if vid == None:
                        vid = 0
                except:
                    vid = 0
                try:
                    serial_number = p.serial_number
                    if serial_number == None:
                        serial_number = "<serial_number:none>"
                except:
                    serial_number = "<serial_number:unknown>"
                try:
                    manufacturer = p.manufacturer
                    if manufacturer == None:
                        manufacturer = "<manufacturer:none>"
                except:
                    manufacturer = "<manufacturer:unknown>"
                try:
                    product = p.product
                    if product == None:
                        product = "<product:none>"
                except:
                    product = "<product:unknown>"
                print(
                    "{} {} {:04x}:{:04x} {} {}".format(
                        device, serial_number, pid, vid, manufacturer, product
                    )
                )
There's probably some better way to do that if anyone cares to.

jimmo
Posts: 6
Joined: Mon Jul 12, 2021 6:44 am

Re: mpremote -- MicroPython remote control

Mon Jul 12, 2021 7:47 am

fivdi wrote: I actually thought something similar myself but ended up giving the thread the title that the MicroPython people themselves use (see https://pypi.org/project/mpremote/)
You have no idea how long it took to choose the name mpremote :)
hippy wrote: What impressed me is that files written from MicroPython on the Pico appear in that directory.

I am going to look into how they have done it because it looks like it could provide a means to have MicroPython itself read and write files on a host just by running a daemon, no need for 'mpremote' itself - I'm back to subverting 'open()' I suspect.
There are two parts that make this work:

a) mpremote injects a class instance that implements the filesystem API, and this is then mounted into the vfs and chdir'ed into. This works much the same way as FUSE on Linux. This gives you both read and write.
b) there's a "side channel" in the main console stream using 0x18 as an "escape byte". Because mpremote implements its own terminal, it intercepts this non-printable and does the background work for handling and responding to the commands.
Dragon12 wrote: Entering "mpremote" at the terminal simply locks up the terminal window and informs me how to exit, which is the only command that does anything. I wonder if a usage guide has been written?
I find that sometimes teh board is still in paste mode (or raw repl), which makes it appear as though the REPL is unresponsive. As hippy pointed out Ctrl-D and Ctrl-B are the way out of each of those modes.

Return to “MicroPython”