Using MicroPython (PyBoard V1.1) with Visual Studio Code

Intro

I’ve recently started using a PyBoard for a project that I am working on. Writing code in an IDE, saving the files, then manually copying them to the board to test them gets tedious rather quickly. If you’re using Visual Studio Code, there are options to do almost everything from within the IDE itself. Getting things to work as they should took a bit of effort and headscratching. What I needed to do is documented below.
Links to the sources of my information is at the bottom.

Install libraries/addon

  • pip install --upgrade pip --user
  • pip install --upgrade micropy-cli --user
  • pip install micropy-cli[create_stubs]
  • pip install rshell --upgrade
  • To prevent “Source directory /pyboard/stubs does not exist.” after creation open pybwrapper.py, change line 63 from
    pyb_path = f"{self.pyb_root}{_path}"
to:
    pyb_path = f"/flash/{_path}"

and save the file.

  • Inside VSC, install the Pymakr plugin
  • Install node.js (see link below, and reboot afterwards)

Create the stub

  • Connect the PyBoard over USB
  • Find out the serial port number (represented from here on by comXX)
  • Run
    micropy stubs create comXX

    (replace XX by com port number) -> this takes a couple of minutes

  • the result will be copied to ~/.micropy/stubs (in my case in a sub directory “pyboard-1.13.0”

Set up your project (in VSC terminal, with project open)

  • To check that your stub (config file for your board) has been installed run
    micropy stubs list 
  • If not, run
    micropy stubs add <path to stub> 

    (where the path is the directory that contains the info.json file.)

  • run
    micropy init <project name>
  • Select what to generate (‘a’ selects all)
  • select which stub to use (the one you just created)
  • A subdirectory will created with <project name> you just specified
  • Open the pymakr.conf file in the subdirectory <project name>. Change
    "address":"192.168.4.1" 

    to your comXX. To do this system wide for Pymakr, open the command palette (CTRL+SHIFT+P) and select/type “Pymakr > Global settings”

  • In the same config file, I had to set “auto_connect”: false to resolve some vague
    "> Failed to connect (Error: Port is not open). Click here to try again." 

    errors when connecting to the board. Setting this to “true” the following behavior: “ignores any ‘address’ setting and automatically connects to the top item in the serial port list”

  • See here for more explanation on the other options

Using the combination

  • To add package dependencies to your project:
    micropy install <PACKAGE_NAMES>
  • Functionality in the Pymakr addon can be used by opening the command palette (CTRL+SHIFT+P) and typing Pymakr. There are options to (dis)connect, run the current file, up/download the whole project, get the FW version,…
  • Note the new terminal “Pymakr Console”:
    Pymakr console
  • And the “Pymakr toolbar”:
    Pymakr toolbar

Shortcuts (from the Pymakr addon info)

ctrl-shift-c : (Re)connect
ctrl-shift-g : Global settings
ctrl-shift-s : Synchronize project
ctrl-shift-r : Run current file
ctrl-shift-enter : Run current Line

References