I’ve managed to get .NET Core apps running on Windows 10 IoT Core, and on Ubuntu 16.04 (and also Ubuntu MATE), but until recently I’d never tried with Raspbian. I’ve read a few posts from people saying that they couldn’t get it to work, and a couple of nights ago I decided to bite the bullet and give it a go.
There’s a good post here on how to get the .NET Core runtime on a Raspberry Pi running Raspbian, and as always, there are a few tricks to getting things running from scratch. To augment this, I’ve created a “Hello World” application template for .NET Core Raspberry Pi on NuGet which I think will make things easier for the community.
At a very high level, the steps to getting a .NET Core 2 app on Raspbian are:
- Install Raspbian onto an SD card and insert into your Raspberry Pi 3.
- Set up SSH and test that you can log into your Raspberry Pi from your development machine.
- Install .NET Core 2 onto the Raspberry Pi.
- On your development machine, install the Raspberry Pi C# template for dotnet core.
- Create a new console application using this template.
- Deploy this application to your Pi running Raspbian using Cake.
I’ll run through each of these steps below.
Install Raspbian onto an SD card and insert into your Raspberry Pi 3
There are already great explanations of how to install the Raspbian OS onto a Raspberry Pi 3 – many people who have a Pi know how to do this already and I don’t really want to just repeat a well understood process here – so I’ve just put some useful links below:
- The guide from RaspberryPi.org on how to install a new Raspbian image.
- You can get the latest images of Raspbian here.
- You can download Etcher.io from here and burn the image to your SD card.
Set up SSH and test that you can log into your Raspberry Pi from your development machine
Once you’ve set up Raspbian and booted your Pi to the desktop, you’ll need to allow SSH connections. These aren’t enabled by default but it’s very easy to configure this.
First open the main Raspberry Pi menu on your desktop as shown in the image below, and open the Preferences sub-menu to reveal the “Raspberry Pi Configuration” option.
Open the “Raspberry Pi Configuration” option screen, and click on the “Interfaces” tab. There’s lots of useful settings here, but the one we want to enable is SSH – click on the “Enabled” radio button as shown below, and then click on OK. SSH is now enabled on your Pi.
You’ll need to find the IP address of your Raspberry Pi – I think the easiest way is to open a terminal on your Pi and type:
This tells me that my Pi has the IP address 192.168.1.111.
Now we need to check you can log in from your development machine. I personally find it’s easiest to use PuTTY to do this. I’ve blogged about installing PuTTY before so I won’t repeat it all here – but a few tips are:
This bit is really important: When you install PuTTY using the installer, it ships with a couple of other programs – pscp.exe and plink.exe, which live in the same directory as putty.exe. You’ll need both of these to deploy the code to the Pi. Pscp.exe allows you to copy files from Windows to Linux, and plink allows you to remotely change permissions on the files you deploy.
It also makes life easier to add the putty installation directory to your machine’s path. If Cake doesn’t know where pscp and plink are on your machine, then you’ll probably see and error about unknown executables during deployment.
So open PuTTY, enter the Pi’s IP address and select the “Connection Type” to be SSH, as shown below:
When you click Open, a command prompt should open where you can type the username and password for the Pi 3.
The default username and password combo for Raspbian is “pi” and “raspberry“, and you should change the default password as soon as possible.
Install .NET Core 2 onto the Raspberry Pi
There’s a straightforward set of commands that you can run through PuTTY to install .NET Core 2 onto your Pi running Raspbian – I’ve written them below:
# Update the Raspbian Jessie install sudo apt-get -y update # Install the packages necessary for .NET Core sudo apt-get -y install libunwind8 gettext # Download the nightly binaries for .NET Core 2 wget https://dotnetcli.blob.core.windows.net/dotnet/Runtime/release/2.0.0/dotnet-runtime-latest-linux-arm.tar.gz # Create a folder to hold the .NET Core 2 installation sudo mkdir /opt/dotnet # Unzip the dotnet zip into the dotnet installation folder sudo tar -xvf dotnet-runtime-latest-linux-arm.tar.gz -C /opt/dotnet # set up a symbolic link to a directory on the path so we can call dotnet sudo ln -s /opt/dotnet/dotnet /usr/local/bin
Now you can test this install by running the dotnet –info command to see the version installed on Raspbian.
On your development machine, install the Raspberry Pi C# template
Now that we have .NET Core 2 installed on our Raspbian, we can go back to our development machine to create an application to run on the Pi.
First, install the template for creating Raspberry Pi applications
dotnet new -i RaspberryPi.Template::*
This will create a new template available to dotnet core – you can list them all with the command:dotnet new --list
And in the screenshot below, you can see there is now a new template called “Empty .NET Core IoT Project”, highlighted in red below.
Create a new console application using this template
It’s really easy to create a new console application now – just run the command below (obviously my application is called “HelloRaspbian”, but yours could be something different):
dotnet new coreiot -n HelloRaspbian
When you browse to this new application folder using your preferred development tool (mine is VSCode), you’ll see some files – we need to make a couple of changes.
First, run the command below to pull down the latest Cake build PowerShell file:
Invoke-WebRequest http://cakebuild.net/download/bootstrapper/windows -OutFile build.ps1
This command is also in the README.txt file which comes packaged with the application.
Now, open the build.cake file and you’ll see some defaults at the top of the file:
/////////////////////////////////////////////////////////////////////// // ARGUMENTS (WITH DEFAULT PARAMETERS FOR LINUX (Ubuntu 16.04, Raspbian Jessie, etc) /////////////////////////////////////////////////////////////////////// var runtime = Argument("runtime", "linux-arm"); var destinationIp = Argument("destinationPi", "<>"); var destinationDirectory = Argument("destinationDirectory", @"<>"); var username = Argument("username", "<>"); var executableName = Argument("executableName", "HelloRaspbian");
Replaced those placeholders with the correct environment variables – I’ve shown my own settings below:
/////////////////////////////////////////////////////////////////////// // ARGUMENTS (WITH DEFAULT PARAMETERS FOR LINUX (Ubuntu 16.04, Raspbian Jessie, etc) /////////////////////////////////////////////////////////////////////// var runtime = Argument("runtime", "linux-arm"); var destinationIp = Argument("destinationPi", "192.168.1.111"); var destinationDirectory = Argument("destinationDirectory", @"/home/pi/DotNetConsoleApps/RaspbianTest"); var username = Argument("username", "pi"); var executableName = Argument("executableName", "HelloRaspbian");
I’ve created a folder on the Pi to deploy my application to, using the command below at the PuTTY SSH prompt at my home directory (/home/pi/).
mkdir -p DotNetConsoleApps/RaspbianTest
Deploy this application to your Pi running Raspbian using Cake
Once I’ve replaced the placeholders in my Cake file, the only thing left to do is run the build.ps1 file from a PowerShell prompt.
To test this, go back to the PuTTY SSH prompt and navigate to your home directory and run:
And you’ll get a text output saying “Hello Internet of Things!”
I hope this post is useful to anyone trying to get a C# console application running on Raspbian. I think Raspbian is the default OS for Raspberry Pi users, so this should open up many development opportunities. My Raspberry Pi template makes creating the default console application easier, and Cake is a brilliant way to orchestrate the deployment process (rather than dragging and dropping files using tools like WinSCP, and having to change file permission manually). I’ll be blogging more on the future on deploying IoT applications to this platform.
I’ve written a few posts now about how to deploy C# Raspberry Pi applications to Windows 10 IoT Core, Ubuntu, and Raspbian (all using Cake as the orchestration tool) – next time I’ll write about how to use Cake to automatically build a UWP AppxBundle and deploy that AppxBundle to Windows 10 IoT Core.