This project describes how to setup a Raspberry Pi Zero W for remote Python development using VSCode.
Once setup, you can develop and single-step debug Python scripts from your local PC and execute on a remote Raspberry Pi Zero W.
The following was adapted from: https://bitbucket.org/jIRI/rasp-pi-zerow-vscode/src/master/
NOTE: See rpi0_tflite_picamera which is provides a more realistic dev environment.
This assumes you have a Raspberry Pi Zero W (W = built-in wifi/bluetooth support). There a tons of dev kits available, e.g.: https://www.canakit.com/raspberry-pi-zero-wireless.html?src=raspberrypi
NOTE: You need a micro SD card, the RPI board-only doesn't have onboard ROM to store the OS.
NOTE: You might also need a micro SD reader if you computer doesn't have one. e.g.:
https://www.amazon.com/s?k=usb+micro+sd+card+reader
Plug the SD card in your computer's SD card reader, then
use the Raspberry PI Imager to program RPI OS to your micro SD card:
https://www.raspberrypi.org/software/
It's recommended to start with the default OS (with desktop support) before getting crazy with RPI OS-lite.
After programming RPI OS from setup 1, unplug the SD card, then plugin it back into your computer.
Go to your file explorer, you should see the SD card be mounted as a new drive (at least on Windows)
with a description as: boot
.
Open the SD card 'boot' drive and edit the following files in the root of the SD card:
config.txt:
Open config.txt
and to the end add:
dtoverlay=dwc2
More details about config.txt here:
https://www.raspberrypi.org/documentation/configuration/config-txt/
cmdline.txt:
Open cmdline.txt
, just after rootwait
add:
modules-load=dwc2,g_ether
ssh
Create empty file named ssh
in root of the SD card
If you're using Windows, install: https://support.apple.com/kb/DL999?viewlocale=en_US&locale=en_US
If you're using Linux, install:
sudo apt-get install avahi-daemon
Unmount the SD card an plug it into the RPI.
Then plug the USB micro into the RPI's USB
port (not PWR) and the other side into your computer.
Go somewhere around Control Panel -> Network and Internet -> Network Connections
Double click your default network connection, click Properties..., tab Sharing
On Windows, PuTTY is recommended: http://www.putty.org/
Connect over SSH (port 22) with connection string: pi@raspberrypi.local
Accept certificate
Default password is raspberry
From the SSH session, issue the command:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
From the opened file editor, add the following to the end:
network={
ssid="MY_NETWORK_NAME"
psk="MY_NETWORK_PASSWORD"
}
Updating MY_NETWORK_NAME
and MY_NETWORK_PASSWORD
with your Wi-Fi info.
Then, Ctrl+O
to save, Ctrl+X
to exit nano.
Next, issue the command:
sudo wpa_cli reconfigure
Then issue the command:
sudo reboot
This will reboot the RPI and close your SSH session. You'll need to start a new SSH session for the subsequent steps.
From the RPI SSH session (step 7), issue the command:
mkdir workspace
chmod 0777 workspace
cd workspace
pwd
Which should create the directory:
/home/pi/workspace
From the RPI SSH session (step 7), issue the commands:
sudo apt-get update
sudo apt-get install -y samba samba-common-bin
Then issue:
sudo nano /etc/samba/smb.conf
Add to the end of file:
[workspace]
path=/home/pi/workspace
browsable=yes
writable=yes
only guest=no
create mask=0777
directory mask=0777
public=yes
Then issue:
sudo service smbd restart
In your local file explorer, you should be able to open (on Windows):
\\RASPBERRYPI\workspace
We use debugpy to enable Python remote debugging.
From the RPI SSH session (step 7), issue the command:
sudo pip3 install debugpy==1.3.0
(You may be able to use a newer version, but that version worked for me)
NOTE: This repo uses Python3, hence pip3
.
From the RPI SSH session (step 7), issue the commands:
sudo raspi-config --expand-rootfs
sudo reboot
We use the dirsync Python package to automatically synchronize files between the local workspace and the remote RPI workspace.
Issue the following command from a local terminal to install the package:
pip3 install dirsync
After installing, the dirsync
command should be accessbile to VSCode.
Assuming you cloned this repo, open the VSCode 'workspace' that is at the root of this repo: workspace.code-workspace
then install the 'Recommended Extensions'.
If using Windows, map the \\RASPBERRYPI\workspace
network drive, more details here
After this is complete, you should have a new drive, e.g. Z:\\
that points to your RPI's /home/pi/workspace
directory.
This is required so we can easily sync the local workspace with the RPI's workspace.
Open <repo root>/.vscode/tasks.json
,
In the sync-workspace
task, update the arguments as necessary for your workspace.
At a minimum, you may need to change z:/
to your networking mapping drive from the previous step.
That's it! Running the Debug Python
debug configuration should:
- Synchronize the local workspace with the RPI's workspace (assuming the network drive is properly mapped)
- Start the
main.py
python script with remote debugging enabled - Cause VSCode to connect to the debug server and allow for single-stepping in the Python script as if it were running locally