Skip to content
Dave edited this page Jun 19, 2023 · 46 revisions

MPRemote Visual Studio Code Extension

What is it?

The goal of the MPRemote extension for Visual Studio Code is to give easy point and click access to many of the functions of the MicroPython project's MPRemote command-line tool. Rather than typing commands in a separate window, you simply navigate to them using the VS Code user interface.

Why should I care?

You shouldn't, really. The extension doesn't do anything MPRemote can't do on it's own. It simply adds a convenient wrapper around the commands you execute. But, if you would like to keep your focus on one tool with one user interface, you might give it a try.

Where can I find it?

The extension can be installed from the Visual Studio Code Marketplace. While you're there, check out my handy ESPTool extension to add point and click flashing capability for Espressif microcontrollers.

How do I use it?

First, make sure you have the prerequisite Python and MPRemote python module installed on you development host. Then open up Visual Studio Code and install the MPRemote extension. Now you're ready to follow along on a quick tour of the main features.

Serial Port Explorer Context Menu

In VS Code's Explorer pane, toward the bottom, you will see a new view labeled SERIAL PORTS. This is where you will find your MicroPython devices. Click to expand it if it isn't already and you'll see serial port names for every device detected.

If you haven't plugged your MicroPython device into a USB port yet, do that now and click the Refresh button to the right of the SERIAL PORTS label.

The Serial Ports context menu (right-click) selections give you convenient access to the majority of MPRemote commands. Find your microcontroller in the list and right-click on the serial port name and you'll be given a list of MPRemote commands to choose from. Pick something harmless, like List files on remote system (ls). Watch the VS Code terminal window for the command output.

image

Figure 1: Serial Ports context menu

Some commands require additional input from you. For example, Make a new directory (mkdir) needs the name of the directory. The input prompt for the name will appear at the top of the VS Code window.

Try it out by creating a new directory for temp.

  1. Find you microcontroller in the serial port list.
  2. Right-click and choose Make a new directory (mkdir) from the context menu.
  3. Enter temp in the input box, and press Enter.

image

Figure 2: User input prompt

You can use the ls command to verify the /temp directory was created as expected.

Most commands work this way and should be fairly intuitive if you've used a tool like Thonny or MPRemote from command-line. You'll also notice the name in parentheses matches the MPRemote command used with the mpremote command-line tool. For example, selecting List files on remote system (ls) in the extension gives the same results as running mpremote ls from the command-line.

Uploading Everything at Once

One command in the context-menu that does not have a direct corrolation to MPRemote command-line is Upload all files to the remote filesystem (sync). This command copies all files and directories in the folder that is open inside VS Code's Explorer pane to the microcontroller flash filesystem. It does this recursively.

If you try to run it now, without any folders open in VS Code, you'll get an error reminding you to open the project folder first.

Here's an example of creating a new MicroPython project using VS Code that you can upload with sync.

  1. Create a new, empty directory somewhere and name it after your project. (I've called mine Test and created it on my Windows Desktop.)
  2. Open this new directory inside VS Code using File > Open Folder.
  3. Use VS Code to create a new file called hello.py inside this directory.
  4. Add the line of Python code shown below and save the file locally.
print("Hello World!")

Now use the sync command to take everything from the project folder and copy it to the microcontroller's flash filesystem. Verify hello.py got uploaded by using the ls command.

This simple example uploads only one file, but it would work just as well if that file were buried several directory levels deep. To demonstrate, use VS Code to create a index.html in folder structure like this in your project directory: /public/html/index.html

You can put whatever you want inside index.html or nothing at all. It's only used as an example of recursive copy.

Now use the sync command to upload everything. You should see hello.py as well as the public directory when you use ls. You'll need to use chdir to look inside /public/html.

Editor Window Context Menu

The Serial Ports context menu has the majority of commands, but there are a couple available in the editor window's context menu. Right-click in any blank area of the editor window and look for MPRemote submenu. Here you will find the commands that deal specifically with the file that's currently open for editing.

image

Figure 3: Editor Window Context Menu

Next Steps

You can explore on your own. Most commands are in context menus, either by right-clicking on the serial port name, inside the editor window or file explorer pane under the MPRemote submenu. There are also some sample workflows in the wiki that will give you a more detailed look at how you can use the extension to add joy to your day-to-day coding.