Some use cases and small talk for the mpremote tool. #9096
Replies: 12 comments 33 replies
-
This is a good start, one of the things I struggle with is how to sync/copy a local directory to a device. I can never get the command correct and I always default back to Thonny when copying lots of files. Would be great to have some sort of sync command that only uploads changed files etc... |
Beta Was this translation helpful? Give feedback.
-
Thanks @Wind-stormger Just to repeat the same comment I made on the original forum post -- the |
Beta Was this translation helpful? Give feedback.
-
Hey~ @jimmo Regarding the path format in Windows, I think this method can be used to judge the current system, and then apply the corresponding path format to operate the file. import platform
print(platform.system())
if(platform.system()=='Windows'):
pass
elif(platform.system()=='Linux'):
pass
else:
pass |
Beta Was this translation helpful? Give feedback.
-
Your post prompted me to take a look at mpremote for the first time (I use rshell most of the time). One great feature is the ability to automatically connect to the first free device when several boards are physically connected to the computer. Sadly, on a Mac, the first free device is always Is there a clever way to configure mpremote so that it always ignores |
Beta Was this translation helpful? Give feedback.
-
I updated the chapter on |
Beta Was this translation helpful? Give feedback.
-
Update,add: |
Beta Was this translation helpful? Give feedback.
-
It would be great if it can also connect ws://ip:port for repl. |
Beta Was this translation helpful? Give feedback.
-
Great description, very useful! |
Beta Was this translation helpful? Give feedback.
-
I'd recommend users of $ mpremote wfif_connect
$ mpremote wifi_scan
$ mpremote exec "host='192.168.0.10';offset=1" ntptime The last sets the device RTC from a local NTP server with a local time offset of 1hr. A command can of course be scripted, or be the target of an alias. Pure magic... |
Beta Was this translation helpful? Give feedback.
-
In all my use cases I need to set the RTC of the Pico Pi. So I changed the file mpremote/main.py (version 1.20.0) as follows: exchange the line 314 "import machine; machine.RTC().datetime((2020, 1, 1, 0, 10, 0, 0, 0))" by f"import machine; machine.RTC().datetime({_rtc_datetime})" and insert somewhere at the beginning (line 44): from time import localtime
_rtc_datetime = (t:=localtime())[:3]+(0,)+t[3:6]+(0,) Now the time of the Pico Pi is always set to the local time of the PC. |
Beta Was this translation helpful? Give feedback.
-
bug? |
Beta Was this translation helpful? Give feedback.
-
I tried to address some of the comments from this thread in a documentation update for mpremote. Please feel free to comment there: #11659 |
Beta Was this translation helpful? Give feedback.
-
I've tried a lot of different 3rd party micropython tools, some are Windows apps, some are VScode plugins.
But when I tried MicroPython's mpremote tool, I almost decided it was going to be my go-to tool. 😄
I usually use it in conjunction with VScode. Its code highlighting, auto-completion, and auto-indentation are very useful. It is also easy to open other plugins at the same time. The important thing is that VScode is also a multi-platform IDE.
Maybe some minimalists, I think the mpremote tool will also be suitable, just use the system's terminal with any text editor, if you are a terminal command line veteran, you only need a terminal to do everything.
Document Page
GitHub Page
PyPI Page
Most of the use cases are pretty clear in the documentation, but I'm not a command line veteran, so I'll put some use cases I figured out here, maybe it will help some newbies like me.
Using mpremote 0.3.0 that has been released in PyPI.
Existing use cases for this post:
1. cp
This is probably the most common command besides repl, used to copy files from local to device, or from device to local. Probably because I am not yet familiar with the terminal command syntax, at first I could not fully grasp the usage of the cp command from the several use cases in the documentation.
1.1 Copy the file, in the current path of the terminal
First create a clean temporary folder and write a
main.py
into it.Simplest use case, enter this folder path in terminal, copy file from local to device :
The two commands achieve the exact same function.
After the
:
symbol, if you enter a filename, the file will be renamed to this filename when copied to the device.Copy file from device to local :
The two commands achieve the exact same function.
If you want to rename, you can delete the
.
symbol and enter the filename you want.1.2 Copy files in absolute paths
It is a little more complicated.
On Windows, use the absolute path to the file to copy from local to device:
Copy file from device to local :
In Linux, such as Ubuntu, copy a file from an absolute path can omit the target filename:
1.3 Copy multiple files at the same time
In Linux, such as Ubuntu, copy multiple files from local to device with absolute path :
Copy multiple files from device to local absolute path:
2. exec, run
These commands are used to control the remote device to run Python code or scripts without copying files.
2.1 execute the given Python code
Just like entering a line of Python code in REPL.
2.2 run a script from the local filesystem
Just like entering paste mode in the REPL, copy and paste the code into the specified Python script, then run it.
3. mount
I haven't thought about this path yet, take a look at the following use case and try to understand it, you'll be as excited as I am.
3.1 Mount the local directory on the remote device
First create a clean temporary directory and write some Python script into it, as follows:
Enter the path to this directory in the terminal.
Let's confirm some information, list files on the device:
Enter REPL and confirm again:
Exit REPL, mount the local directory, enter repl again:
Confirm files again:
There is no
boot.py
here, but the Python script we created appears in the list.Import and run two test scripts:
View a file:
Beyond surprise, you might wonder, if the script files were all uploaded to the device and stored? which I did initially.
Now, we keep the terminal in REPL, modify this file locally:
Go back to REPL and check again:
All doubts dissipated, the file was only saved in the local directory, and this directory was mounted on the device.
Soft reset is handled and will re-mount the directory.
@jimmo explained more about this in discussioncomment-3500600 , discussioncomment-3526060
This is like a mobile hard disk, or a NAS, cloud disk, connected to the MicroPython device. Obviously, this function can greatly save the flash life of the device. Usually, you only need to copy the python script file to flash when you have to run it offline.
It is recommended to execute python scripts using the following combined commands:
3.2 Continue to use the script in the original flash after mounting the local directory
Use the
uos.listdir("/")
command in the REPL and you will see the files originally stored in flash:If you want to use flash scripts (such as
main.py
) while keeping the local directory mounted, you can use the following command to add the original flash root directory path to thesys.path
list:If there is already
main.py
in the mounted local directory, only themain.py
of the local directory will be run. You can usereverse()
to change the list order:Scripts in flash will now run first.
3.3 A way to speed up testing
If we need to test and modify a script again and again, and the script has imported many script modules stored locally, in this case, each mount test will take several seconds or even tens of seconds.
I've come up with a way to effectively reduce the runtime by
import
modules in the REPL that are not duplicated.Take testing SSD1306 OLED display as an example,
main.py
is the main program,ssd1306.py
is the driver module, and the program code will not be listed in full here.Use time.tick_ms() to verify the time required to test the
main.py
script in the local directory, which takes about 6 seconds :Use
sys.modules
to view the currently imported script modules, you can see thatssd1306.py
has been imported:We just need to remove
main
and keep the other modules:Test again:
Significantly improved!
( To Be Continued)
Beta Was this translation helpful? Give feedback.
All reactions