Visual Studio Code Ssh Raspberry Pi



  1. I want to run Visual Studio Code Remote Development using SSH to my Raspberry Pi 3 Model B+ running Raspbian GNU/Linux 9 (stretch). I have tried to follow the 'Getting started' instructions.I run the command Remote-SSH: Connect to Host., but I get the message Can't connect to admin@pihole.local: unreachable or not Linux x8664 (Linux armv7l ). As far as I know, Raspbian is 32 bit.
  2. This vid takes you through a base setup of Visual Studio Code remoting to a non-local non-Windows Services For Linux system As of mid July 2019 this is still.
  3. Raspberry Pi can be used for more than just a small linux host to run services, media server or a NextCloud server, learn how to use it to code in Python. Host pi HostName 192.168.2.137 User pi IdentityFile /.ssh/idrsa Host nfs HostName 192.168.2.91 User bob IdentityFile /.ssh/idrsa Host git-codecommit.amazonaws.com User.

In this short note I will summarize how I can happily use Visual Studio Code on my local Windows desktop or laptop to do remote development on a Raspberry Pi or another Linux machine, and even push Arduino sketches from the Raspberry to an Arduino without touching the Arduino IDE. I have been a fan of the Raspberry Pi single board computer for a number of years now, and have used quite a number of them in my desktop single board computer cluster and my developing Raspberry/Arduino robot car. Still, I found developing code on it to be uncomfortable and sometimes cumbersome, as some IDEs don't support ARM processors whereas others like Thonny are rather basic. Remote development solves this by allowing you to code on your desktop / laptop and run it externally on a host (like the RPi). With the nascent Arduino CLI you can even largely replace the Arduino IDE for developing Arduino sketches on RPi and pushing them to a connected Arduino.

If you’re using Ubuntu on Raspberry Pi, you first need to set up an openssh server to use Visual Studio Code’s remote access tooling. That’s easy enough: just use the built-in package management tools to install the appropriate packages, and then use the Windows command line to ssh to your Pi to set up the appropriate keys. Visual Studio Code: Now on ARM. Microsoft has recently released a version of Visual Studio Code that runs on both ARMv7 and ARM64 Linux devices. With support for 32- and 64-bit ARM code, you can now run Code on both the 32-bit Raspberry Pi OS (recently renamed from Raspbian, as it’s no longer a direct port of Debian) and its 64-bit beta.

Remote Development with Visual Studio Code

For remote development to work in VS Code, you need to be able to SSH into your Raspberry, or other remote machine, using ssh pi@IP_ADDRESS in a command prompt (Windows) / terminal (Linux). You also need to install the VS Code Remote - SSH extension. Once done there's a new symbol in the left toolbar and an indicator on the bottom-left corner of the Status bar. This indicator tells you in which context VS Code is running (local or remote). Click on the indicator to bring up a list of Remote extension commands.

You can enter a new SSH connection or choose among previously used ones that are stored in a configuration file. More details can be found in Microsoft tutorial pages here and here. Upon connection to a new system VS Code will provision the VS Code Server to the remote environement. This will work with a number of Linux flavors, but certain other extensions may not work on ARMv7l (AArch32) and ARMv8l (AArch64) devices if they use x86 native code in the extension. The installation on my Raspberry looked as follows:

That's basically it, from here on everything works fine, even on my old Raspberry Pi 2 Model B. Regarding supported / unsupported extensions I will need more time to explore and will eventually add to this post later on.

Arduino CLI

The Arduino IDE is great at achieving its intended purpose: It's a simple, single-file application development environment. It has just enough integrated tools to help achieve that purpose. But for larger application development it doesn't compare with a full-featured C/C++ IDE, lacking features such as code navigation, auto-completion, version control integration, or an integrated terminal.

Thanks to the nascent Arduino CLI there is now a potentially much more efficient mode of coding for the Arduino. Arduino CLI is the 'glue' that can pair the VS Code IDE with common Arduino compilation and upload tools, providing a command-line interface for such tasks as building Arduino sketches, uploading Arduino sketches, downloading libraries and downloading new board definition files - all without ever opening the Arduino IDE.

Visual Studio Code Ssh Raspberry Pi

The latest version of the Arduino CLI on any supported platform can be obtained using the install.sh script:

Next, I created a configuration file as suggested on the Arduino CLI man page, in order to avoid a lot of typing when issuing a command:

The next thing to do upon a fresh install is to update the local cache of available platforms and libraries by running:

Arduino and Raspberry are connected via USB cable on my mobile robot, so let's see whether the Arduino can be recognized:

The Arduino Uno was recognized and from the output of the command we see the platform core called arduino:avr is the one that needs to be installed to make it work. The fact that we see an Unknown board listed doesn't really matter as long as we identify the platform core and use the correct FQBN string.

You can search for available cores like this:

Let's install the arduino:avr platform core:

The process is also documented in the following picture which, on the left hand side, shows the folder structure that has been built during the installation process:

From a hardware perspective we are good to go but we may need to import external libraries before we can compile an Arduino sketch. In my case I needed a library for the Adafruit Motor shield. You can search for libraries like this:

This will result in a list of related libaries out of which you can then install the right one:

We are now good to go. Arduino sketches can be developed in VS Code and then compiled and uploaded from the integrated terminal. To make the last two steps handier and avoid typing long commands I added a few lines to the .bashrc shell script, using

First I added the directory of the arduino-cli executable to the user $PATH variable so that I can execute the Arduino CLI without typing the full path:

This is necessary since I didn't store arduino-cli in one of the standard directories for files with executable permissions, such as /bin, /sbin, /usr/sbin, /usr/local/bin or /usr/local/sbin. As a second addition, I create aliases acompile and aupload as follows:

They allow me to compile Arduino sketches and upload them to the board using the following short commands:

Note that you need to give the directories as arguments, not, e.g., the .ino file for compilation. After saving the .bashrc file I loaded the changes into the current shell session using the source command:

To confirm that the directory was successfully added, one can print the value of $PATH by typing:

The final picture shows the result of our work. We can now develop Python code for the Raspberry and C++/.ino code for the Arduino side by side. Also we can use several parallel terminal windows to upload sketches and monitor code execution:

Links

Finally, some links that I found helpful:

  • Visual Studio Code remote development using SSH is well-documented here and here.
  • The Arduino CLI is relatively well documented on their GitHub page. Additional websites that may be helpful: One, two, three.

See also

Historically, debugging embedded applications is hard. More often than not, you’ll be developing code on your desktop or laptop and deploying code to an embedded device.

Getting everything set up to allow that to be seamless is often difficult to achieve.

In this guide, I’ll show you how to configure your PC and a Raspberry Pi, to allow you to hit the F5 key in Visual Studio code and have the code shipped to the Raspberry Pi, run and have VS Code attach to the Visual Studio debugger actually on the Pi.

There are alternatives to this methodology. One of them is the Remote SSH extension for VS Code. However, this extension relies on the source code for your application to be stored on the device itself. I’m not a fan of this, as it means that you need to be consistently connected to the remote device to code. If you hop on a train and lose connectivity, you can’t save your work.

Ssh

This guide (and single line install script!) will configure your system so you can code entirely on your local machine with no reliance on the remote machine until you need to deploy.

The previous version of this guide

This guide is actually a simplified version of a previous post I made about remote debugging .NET Applications on a Raspberry Pi from VS Code.

The previous version had relied on two different items; WSL with OpenSSH (primarily for rsync) and Putty’s Plink.

This version uses the free cwrsync tool to remove the need for WSL entirely. Further, using Scott Hansleman’s instructions here, we can remove the need for plink entirely.

Finally, I’ve created a one line script you can run from an elevated command prompt which makes getting your machine setup to Remote Deploy and Debug from Visual Studio Code a cinch!

Prerequisites

Before you start, you’re going to need to have the following installed;

  • A PC with Windows 10 19042 or above.
  • .NET 5 SDK on your PC
  • SSH enabled on the Raspberry Pi – Use the Raspberry Pi Config Utility.

The Quick Way – Run a script

Instead of following along with a heap of instructions, I’ve created a script that carries out everything you need to prepare your pc to Remote Deploy and Debug .NET code from Visual Studio Code.

Open a command prompt as an Administrator and navigate to your root C drive with “cd /“. Next run the following command;

The first thing the script will do is prompt you for the hostname of the Raspberry Pi you want to deploy your code to.

You’ll also be asked to enter the SSH password for the Pi, so the script can get the Pi setup to allow passwordless SSH – This is often simply “raspberry“.

This script will perform the following functions;

  • Prompts for the hostname of the Pi you’ll be connecting to
  • If they don’t exist, creates a set of SSH Keys using ssh-keygen
  • Adds the Pi at the hostname specified to the trusted hosts, which prevents the prompt to trust the thumbprint when connecting
  • Copies the Generated SSH Keys from the PC to the Pi
  • Downloads the Visual Studio Debugger on the Pi
  • Downloads cwRsync
  • Configures a “home” directory and sub directories for cwRsync and copies the generated SSH Keys to it to allow the cwRsync ssh tool to connect to the Pi without a password.
  • Creates a .NET 5 console application in the root named as <Pi Hostname>_test
  • Adds a preconfigured launch.json and tasks.json to allow for the project to be deployed and remote debugged.
  • Launches the new console application in Visual Studio Code.

Trying out the Remote Deployment and Debugging from VS Code

Visual studio code ssh raspberry pi zero

Once the process has completed. You should be looking at a VS Code Window with the new Console Application Loaded.

Before we can try deploying to the Pi and Debugging remotely, we need to select the correct debugging profile.

Click on the “Run” toolbar button on the left, to show the “Run” options. Drop down the Run Option Dropdown and select the “.NET Core Launch (remote)”

We’re now ready to test it all out! Go ahead and either press the green “Start Debugging” button, or hit F5 to start the process off.

The predefined tasks.json and launch.json files will run through the following processes on the way to debugging the code on the Pi;

  • Build the Project
  • Create Linux-ARM binaries using the dotnet publish command
  • Copy the necessary published linux-arm binary files over to the Pi’s Home directory using Rsync.
  • Use an SSH session to Launch and attach VS Code to the Visual Studio Debugger installed on the Raspberry Pi.
  • Run the code.

You’ll see the various steps complete, then the “Hello World” application will run with the output showing in the “Debug Console”.

You’ll see various messages reporting which dlls have been loaded. You’ll see “Hello World!” in the Debug Console in blue, before the application ends.

Congratulations, you’ve now deployed your first .NET application to a Raspberry Pi and debugged it remotely!

Next Steps

Now that you’ve got everything set up, it’s possible for you to create new applications and also Deploy and Debug them remotely.

You’ll just need to copy the launch.json and tasks.json files to your .vscode directory once the project has been created.

You’ll then need to modify lines where we’ve referenced the Pi Hostname if you intend to deploy to a different Pi perhaps.

Also, if your solution has multiple projects, you’ll need to modify the tasks.json and launch.json files to point to the correct project within your overall solution.

The Step by Step Way

If you’d like to take a more manual approach to the configuration, or perhaps if you’re interested in what the script is doing, then feel free to follow along from here instead of running the script above.

Generate SSH keys

The first thing we need to do is check if you already have some SSH keys, and if not, then we need to generate some SSH keys so that we can SSH into our Raspberry Pi without using a password.

We can create our keys (or leave them untouched if they already exist by running the following command;

Copy SSH Files to the Pi

Now that we have the SSH keys, we need to get them on to our Pi.

We can use the windows Type command along with the SSH command to copy the id_rsa.pub file to the Pi. This way, we won’t need to enter a password when we use the SSH command.

Enter the following command, replacing the “<hostname>” placeholder with the hostname for your pi;

You’ll be prompted for the password for your pi of course (In most cases this is “raspberry” of course).

We can test this works now with the following;

You should see the nice SSH Session appear…

Copying files to the Pi with cwRsync

We’ll be using a tool called Rsync to copy files to the Pi over SSH. The beauty of Rsync is, not only can it copy files over an SSH connection to the Pi, but it will also compare the files already on the Pi, with the files about to be copied. It will then only copy the files which have changed.

This is great for the development lifecycle, as we’re often only changing the odd file here and there normally, so after the first deployment, only one or two files need to be copied to the Pi.

Rsync is a Linux application, in the previous blog post we use the Windows Subsystem for Linux to use the Rsync application.

However, there’s a Windows implementation based on Cygwin called cwRsync, we can use this instead of needing to configure WSL at all.

Due to an issue with this version of cwRsync, we also need to create a directory structure that cwRsync can use to store it’s known list of hosts. Normally this would be stored in our userdata folder… But for some reason, cwRsync needs a different directory.

We also need to copy the SSH keys we created above to this new directory.

Finally, we need to set the correct permissions on the new “home” folder, otherwise cwRsync will complain (rather misleadingly) that our keys have insufficient security

Assuming you have an up to date version of Windows 10, you can run a set of commands to set everything up.

Open a command prompt as an Administrator and run the following commands;

Install the Visual Studio Debugger on the Pi

When we’re debugging from VS Code, we’ll be using the Microsoft Visual Studio Debugger Package to carry out the debugging on the Pi. The VS Debugger will run the code on the pi through an SSH session, and VS code will connect remotely to the debug session allowing us to interact with that session on our local machine.

To install the VS Debugger, run the following commands;

You’ll need to switch out the “<Pi Host Name>” place holder for the Host Name of your Raspberry Pi.

That will begin the process of downloading and installing the VS Debugger on the Raspberry Pi;

Create and configure a .NET 5 console application

It’s now time to create a .NET 5 console application and configure it to Remote Deploy and Debug to our Raspberry Pi.

Choose a suitable location on your machine and run the following command, replacing the “<name of project>” placeholder with the name of your project;

Navigate to your new project in the command prompt and use the following to open your new project in VS Code;

Once VS Code has finished loading up, if you’ll be prompted by the C# Extension to add the Build and Debug Assets to the project, go ahead and click the “Yes” button;

This will add default launch.json and tasks.json files to your project in a directory called “.vscode”;

Adding VS Code Tasks for Publish and Deploy

We now need to add a couple of extra tasks to the tasks.json file to firstly create some binaries which are compatible with the Raspberry Pi.

Visual Studio Code Ssh Raspberry Pi Zero

The Raspberry Pi has an ARM processor and is running Linux. We can create compatible binaries using the following command;

Next we can deploy these compiled binaries to the Raspberry Pi using our new cwRsync tool which we configured earlier, using the following command;

We can have Visual Studio Code carry out both of these tasks for us by adding them to the tasks.json file.

Open the tasks.json file and add the following two tasks below the “watch” task (remember to place this code immediately after the “watch” task curly brace, and not at the end of the file!);

You’ll need to switch out the “<Pi Host Name>” placeholder with the correct Host Name for your Raspberry Pi.

Adding VS Code Launch config to Launch and attach the Remote Debugger

Next, we need to configure the launch.json file to connect to the Visual Studio debugger on the Pi, run our code and begin debugging.

Open the launch.json file and add the following section of json below the “.NET Core Attach” launch task;

You’ll again need to switch out the “<Pi Host Name>” placeholder with the correct Host Name for your Raspberry Pi.

Trying out the Remote Deployment and Debugging from VS Code

You’re not ready to Deploy and Debug your .NET 5 application on the Raspberry Pi!

Before we can try deploying to the Pi and Debugging remotely, we need to select the correct debugging profile.

Click on the “Run” toolbar button on the left, to show the “Run” options. Drop down the Run Option Dropdown and select the “.NET Core Launch (remote)

We’re now ready to test it all out! Go ahead and either press the green “Start Debugging” button, or hit F5 to start the process off.

The predefined tasks.json and launch.json files will run through the following processes on the way to debugging the code on the Pi;

  • Build the Project
  • Create Linux-ARM binaries using the dotnet publish command
  • Copy the necessary published linux-arm binary files over to the Pi’s Home directory using Rsync.
  • Use an SSH session to Launch and attach VS Code to the Visual Studio Debugger installed on the Raspberry Pi.
  • Run the code.

You’ll see the various steps complete, then the “Hello World” application will run with the output showing in the “Debug Console”.

You’ll see various messages reporting which dlls have been loaded. You’ll see “Hello World!” in the Debug Console in blue, before the application ends.

Congratulations, you’ve now deployed your first .NET application to a Raspberry Pi and debugged it remotely!

Next Steps

Now that you’ve got everything set up, it’s possible for you to create new applications and also Deploy and Debug them remotely.

Code

Visual Studio Code Ssh Raspberry Pi 4

You’ll just need to copy the launch.json and tasks.json files to your .vscode directory once the project has been created.

You’ll then need to modify lines where we’ve referenced the Pi Hostname if you intend to deploy to a different Pi perhaps.

Visual Studio Python Raspberry Pi

Also, if your solution has multiple projects, you’ll need to modify the tasks.json and launch.json files to point to the correct project within your overall solution.