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? - micropython

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:
I don't want to start a named server and then run another command, it's messy, I just want to run!
I would need to think about how to send Ctrl+D to soft restart. Should not be hard, but lazy to learn

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.

Related

Editing WSL2 instance of Ubuntu Crontab using Windows VSCode

My question is whether it is possible to edit the crontab of a WSL2-based instance of Ubuntu with my Windows VSCode that is connected via WSL remote SSH.
If I type export EDITOR=code inside my WSL instance and then crontab -e, I am able to see a /tmp/crontab.sygFAU file load inside my VSCode instance.
The problem is that once I make edits to this file, it will save the file to /tmp/crontab.sysFAU but it doesn't actually take the next step of replacing the the real crontab file in /var/spool/cron/crontabs.
So once I re-open the crontab, it will just show what I had previously, and not my saved edits.
It would be nice to know if this is not possible or if there are any alternative ways to run a GUI editor because using nano is a pain!
An interesting question that I haven't considered before, myself. Here's what's happening:
You set your editor to code
You crontab -e, which properly loads VSCode with the temporary crontab.
However, because this is a Windows GUI application, it returns control to the parent Linux application (crontab) immediately after starting VSCode. You can see the same result if you just start notepad.exe from your shell in WSL -- Once Notepad starts (rather than exits) control is returned to the shell.
If you switch back to your terminal at this point, you'll see that crontab detected that the editor that it launched exited (returned), and so it has already tried to copy the temporary file to the permanent location.
However, since the temporary files doesn't yet have any changes, crontab decides there's nothing to do.
Editing the file in VSCode and saving it has no effect, other than to leave a dangling /tmp/... file hanging around (since crontab isn't there to clean up).
So what's the solution? We need a way to launch a Windows GUI application and prevent it from returning control to crontab until you are done editing.
I originally thought something from this question might work, but the problem is that the actual command that launches the Windows process is embedded in a shell script, which you can see with less "$(which code)" (or code "$(which code)"), but it's probably not a great idea to edit this.
So the next-best thing I came up with is a simple "wrapper" script around the (already-a-wrapper) code command. Create ~/.local/bin/code_no_fork.sh (could be anywhere) with:
#!/usr/bin/env bash
code $* > /dev/null
echo Press Spacebar to continue
read -r -s -d ' '
Credit: This answer for the Spacebar approach
Then:
EDITOR=~/.local/bin/code_no_fork crontab -e
After you make your edits in VSCode, simply press Space to allow the script to continue/exit, at which point crontab will (assuming no errors were detected) install the new Crontab.
Alternatives
This is should typically only be a problem with Windows GUI applications, so the other possible avenue is to simply use any Linux editor that doesn't fork. If you want a GUI editor, that's entirely possible as long as you are running a WSL release that includes WSLg support (now available for Windows 10 and 11).
I won't offer any individual editor suggestions since that would get into "opinion" and "software recommendation" territory, which is off-topic here.

How to setup zsh personal config (.dotfiles/zsh/zshrc) at every start on Raspberry Pi 400

I am training myself on Linux with a Raspberry Pi 400 (OS: Raspbian) and I would like to setup my personal config file for apps (notably the Terminal in Zsh). I am able to setup my config onto the terminal with the following:
$ cd .dotfiles
$ ./export.sh
$ cd zsh/
$ source ./zshrc
The problem comes when I quit the terminal or if I open a new terminal or if I reboot the computer, the config is not "saved".
I am not an expert, but for changing bash to zsh and maintaining it as default shell language, I did the following:
$ nano /etc/passwd
and changed the following line from :
pi:x:1000:1000:,,,:/home/pi:/bin/bash
to:
pi:x:1000:1000:,,,:/home/pi:/bin/zsh
This move managed to get me zsh from start every time now. I am wondering if i can directly source my .zshrc with the same technique ... Or would you have any better idea?
I would like to avoid repeating the sourcing steps at every start in the terminal ...

Creating a bat file to run raspberry pi commands on windows

I am trying to set up a simple bat file to help non-tech people execute scripts by just clicking a file on desktop.
I haven't used windows in a while, and don't have a real dev environment set up on the computers they are going to be using. I installed putty, and can manually ssh into the pi's and run commands, but I can't teach that process to the employees.
I would like to create a simple bat file that runs the following commands:
ssh pi#192.168.1.xxx
<enter password>: 'xxx'
sudo reboot
Is there a way to do this?
Thank you!
Downloaded Plink #
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
I moved it, AND putty to c:/windows/system32 instead of changing PATH
This doesn't seem necessary, as the next step will automatically locate the file and change the path.
On desktop, right click and create a new shortcut.
In the shortcut path, I typed:
plink -ssh username#192.16.1.xxx -pw raspberry sudo reboot
This worked.
if you recheck the shortcut again, it should automatically change the path to execute plink

Display GUI on Raspberry Pi without startx

I'm currently playing with my brand-new raspberry with adafruit's touch-tft and raspberry Cam.
I noticed, if I run raspivid -p, it displays the preview in a frame on top of the bash.
How can I create an application (java, python, c++(preferred)) to display a GUI without having to startx?
I'd love to use adafruit's touch-tft for a project, but startx needs a lot of resources. Of course it would be cool to have the touch-functionality too.
Edit:
Maybe I expressed myself a little bit confusing: I would like to create an application that doesn't need startx but has some kind of GUI.
The answers below aren't 100% ready to go but they pointed me to the right direction. Since I saw many similar questions I will try to give a step-by-step solution:
Setup your raspberry with adafruit's TFT: https://learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi/overview
(you don't have to replace the TFT with HDMI as standart display)
be sure FRAMEBUFFER=/dev/fb1 startx is running correctly on the TFT screen, I have my TV plugged in the HDMI port, this way I can use the bash on the TV screen and run x on the TFT
Follow this tutorial to create your first pygame: http://www.pygame-doku.laymaxx.de/tut/intro/intro.html
(you can just copy&paste, wget ball.gif)
Insert this 2 lines at the beggining: (github.com/notro/fbtft/wiki/Pygame)
import os
os.environ["SDL_FBDEV"] = "/dev/fb1"
If you want to test the touch-screen to, you can change Line 15:
From:
if event.type == pygame.QUIT: sys.exit()
To:
if event.type == pygame.MOUSEBUTTONDOWN: sys.exit()
This will exit the application if you touch the screen
Save file e.g. pygame1.py and execute with $python pygame1.py
You should now see a bouncing ball on your TFT while the HDMI output still displays the BASH on your TV (but it isn't active of course) touching the screen will exit the game if you completed step 6
Why I'm doing this? This way you can display a simple GUI with e.g. buttons to do some action, like turn your lights on/off if you use your RPI for home-control.
You can use for example pygame library! check this tutorial http://archive.furtherfield.org/rp-resources/RP-workshop-handout.pdf pg. 17
Below, you will find a detailed description of how to be a minimal window manager to start a graphics program of your choice.
-Install Required Packages:
apt-get update
apt-get install xorg - no-install-recommends gdm3 matchbox-window-manager Iceweasel rsync-y
-The system in question, select Add a new user interface gdm3. The terminal is the default name will be used.
adduser kiosk
-Create-Xsession. This script will be executed when the user logs on.
xset s off
xset -dpms
matchbox-window-manager &
while true; do
rsync -qr --delete --exclude='.Xauthority’ /opt/kiosk/ $HOME/
firefox http://www.google.com (Replace your application!!!)
done
Save Ctrl + O; Exit: Ctrl + X
chmod a + x. xsession
cp / opt / kiosk / .xsession / home / kiosk
Set Auto-Login. Kiosk user will enter automatically after boot.
nano / etc/gdm3/daemon.conf
[Daemon]
AutomaticLoginEnable = true
AutomaticLogin = kiosk
We have chosen to configure, program, if necessary
-Screen calibration when not loaded up the whole screen:
/ Boot / config.txt file, clear the comment:
disable_overscan = 1
I hope I helped
Check out Urwid, a text-based ui that can run in a terminal, or, I assume, on a Raspberry Pi started up without startx.
In short you can't, applications like gedit are built atop of a desktop environment (e.g. Gnome, Kde).

Can use local source files in a different directory while I use gdb over ssh in emacs?

I have a 32-bit desktop, where my copy of emacs is running.
I'd like to be able to debug a program running on a very stripped down qemu 64 bit virtual machine.
The virtual machine has an ssh port 24054, which gives me passwordless root access, and it does have gdb installed.
And it has the directory which my desktop calls ~/myco/chip_test mounted as /9p
So if I make a hello.c file in ~/myco/chip_test,
then go to the 64 bit machine in which the VM will run, and compile it with:
$ gcc -o hello -g hello.c
then start the virtual machine, and on my 32-bit desktop run
$ ssh -p 24054 root#anvil 'cd /9p && gdb ./hello'
Then gdb runs perfectly in command line mode in a window on my desktop.
So now of course I want it to run under emacs, because the command line gdb is a bit hard to read, and I'd like to see my cursor stepping through the file, and have windows with watch variables and so on.
So I try M-x gdb
Run gdb(like this): ssh -p 24054 root#anvil cd /9p && gdb --annotate=3 ./hello
And I get a debugger window up in emacs and everything looks right, in fact I can even set and remove breakpoints by clicking in the fringe of the ~/myco/chip_test/hello.c file in my local emacs, which is the same file as what gdb sees as /9p/hello.c, but the moving cursor doesn't appear or move around, so the whole thing is fairly useless.
So I think that emacs/Gud doesn't quite realise that ~/myco/chip_test/hello.c is the file that the debugger thinks is /9p/hello.c, although obviously something interesting is happening since I can set breakpoints, and so I wonder if there is a way to get one or another programs in the loop to translate the filenames so that everything just works.
Any ideas, or am I just going about this all wrong?
You'll get much better results if you either
mount /home/<user>/myco/chip_test on /home/<user>/myco/chip_test in the
VM (so the source paths match exactly), or
run gdbserver in the VM, and connect local GDB to it.