Setting up and programming your Raspberry Pi remotely
If you use your Raspberry Pi like I do even some of the time then you will know what a hassle it is to plug all of your peripherals in to get your Raspberry Pi ready, just to do all of your coding and development on another device. I have even found myself writing code on my laptop, just to copy it on the Pi, make changes and have to delete my application off the Pi so I can copy the new version on. Now we will discuss programming your Raspberry Pi remotely instead.
Well with Visual Studio Code you no longer need to do any of this.
Setting up your SD card
The process starts with writing your image to your SD. Insert your SD card into your laptop/desktop and open Raspberry Pi imaging tool.
If you have not installed it yet, you can find it here. First thing you need to do is open the alternative menu by pressing Ctrl+Shift+X. This will give you a menu to add your networking details as the image is being written to the card.
You enable SSH and set your new password. Then you want to configure your WiFi by entering you SSID and password. Next you need to set up you locale and WiFi country, this is very important for your WiFi to work.
Now you are finally ready to insert your SD card into your Raspberry Pi and switch it on.
Find you pi IP by having a look on what devices are connected to your router. Chances are you should only have one Raspberry Pi listed.
Visual Studio Code
Next we can turn our attention to your laptop/desktop. Start by installing Visual Studio Code. Next you want to install the Remote Development extension. You do this by clicking on the extensions tab on the left side of the display.
Type in Remote in the search bar and click on install on the right one.
Next you can connect to your Raspberry Pi. Launch the VS Code command palette using Ctrl+Shift+P on Linux or Windows, or Cmd+Shift+P on macOS. Search for and select Remote SSH: Connect current window to host (there’s also a connect to host option that will create a new window).
Enter the SSH connection details, using user@host. For the user, enter the Raspberry Pi username (the default is pi). For the host, enter the IP address of the Raspberry Pi, or the hostname. The hostname needs to end with .local, so if you are using the default hostname of raspberrypi, enter raspberrypi.local.
The .local syntax is supported on macOS and the latest versions of Windows or Linux. If it doesn’t work for you then you can install additional software locally to add support. On Linux, install Avahi using the command sudo apt-get install avahi-daemon. On Windows, install either Bonjour Print Services for Windows, or iTunes for Windows.
For example, to connect to my Raspberry Pi 400 with a hostname of pi-400 using the default pi user, I enter [email protected].
The first time you connect, it will validate the fingerprint to ensure you are connecting to the correct host. Select Continue from this dialog.
Enter your Raspberry Pi’s password when prompted. The default is raspberry, but you should have changed this (really, you should!).
VS Code will then install the relevant tools on the Raspberry Pi and configure the remote SSH connection.
VS Code may seem daunting at first – it’s a powerful tool with a huge range of extensions. The good news is Microsoft has you covered with lots of hands-on, self-guided learning guides on how to use it with different languages and development tools, from using Git version control, to developing web applications. There’s even a guide to learning Python basics with Wonder Woman!
The Great news is that as soon as you have done this setup once, it will always be there, and next time will be just so much easier to get Coding. The only hard part now is thinking up your next great project!
Resources
https://www.raspberrypi.org/blog/coding-on-raspberry-pi-remotely-with-visual-studio-code/