How to run a MicroPython host script file on the Raspbery Pi PIco from the host command line and receive UART output back on terminal?

I know how to do something similar with Thonny: I could paste my code into the editor, and press the green "Run" button. This would run the program and give me output. But it would require copy pasting my file into Thonny (I want to code in Vim or run pre-existing examples) and pressing GUI buttons which I don't want to do.

Another related approach would be to copy the program as main.py to the pico, e.g. with rshell: How can you make a micropython program on a raspberry pi pico autorun? But this requires plugging and unplugging the USB, and then reconnecting to the UART every time to see the output.

Is it possible to send file contents to a GNU Screen session? would likely also solve or almost solve my problem, but:

Upvotes: 2

Views: 1101

Answers (1)

The first way I got it to work was with https://github.com/scientifichackers/ampy That tool is just designed for the job, and does it perfectly with the run command:

python3 -m pip install --user adafruit-ampy
ampy --port /dev/ttyACM0 run blink.py

Outcome:

  • stops execution of current program
  • starts execution of blink.py
  • shows UART output on my shell

I can then quit ampty with Ctrl + C to get back to my shell, and the program continues to run.

Tested on adafruit-ampy==1.1.0, Ubuntu 22.04 host, Raspberry Pi Pico W, MicroPython rp2-pico-w-20221014-unstable-v1.19.1-544-g89b320737.uf2.

Upvotes: 3

Related Questions