Getting Started Building Net Core Apps for Raspberry Pi with Raspbian Linux

Image for Getting Started Building Net Core Apps for Raspberry Pi with Raspbian Linux
Tinkerer: Lory Livezey

In this tutorial, we're going to write a simple "Hello World" program in Visual Studio Code (C#), then compile it for Ubuntu ARM, sync the code to the Pi and run it. Sound simple enough? There are quite a few steps, but once you get your environment set up, it's easy!

Configuring Visual Studio Code to Sync with Raspberry Pi In this tutorial, we're going to write a simple "Hello World" program in Visual Studio Code (C#), then compile it for Ubuntu ARM, sync the code to the Pi and run it. Sound simple enough? There are quite a few steps, but once you get your environment set up, it's easy

Previous Step

Setup for on the Raspberry Pi Side:

  Installing Net Core Runtime on Raspberry Pi

Setup for Visual Studio Code:

Configuring Visual Studio Code to Sync with Raspberry Pi

What you will need

(Or Larger)

Software Prerequisites

You'll need the appropriate Net Core Runtime and Net Core SDK software, and Visual Studio Code. The versions I am running are latest as of this writing:

  • Visual Studio Code: 1.22.2
  • Net Core Runtime: 2.1.0 preview 2
  • Net Core SDK: 2.1.300 preview 2

Download and install these, or a more stable release if they are out, from here:

Microsoft Net Core Downloads

Create a Console Application

Open a Command Prompt (DOS Window), and find a directory where you want to develop this app:

Start > Run > Cmd

If you're not familiar or fond of the DOS Prompt, here's a cool trick. In Windows Explorer, go to the directory that you want, and type cmd in the bar at the top that has the path:

List of Templates for Net Core

When you're in the directory where you want to create the new app, type this command to add the Raspberry Pi Template to those available:

dotnet new -i RaspberryPi.Template::*

You'll get a list of the templates that you have to choose from for creating a new app:

List of Templates for Net Core

You'll notice that it gives you examples of commands that you can type to create it. We're going to type this:

dotnet new coreiot -o TinkerPIoT

Once it's done, you can type dir to see the new directory:

Create App

Then type these commands to open the new app in Visual Studio Code:

cd TinkerPIoT
code .

Open the TinkerPIoT.csproj file and change the TargetFramework to version 2.1:

<TargetFramework>netcoreapp2.1</TargetFramework>

You should see two boxes that pop up. One is telling you that there are dependences that the application needs. The other says you should run a Restore. Always do both of these whenever VS Code suggests it.

Restore Dependencies

If you make any changes that require updates to libraries, this is how you do this manually:

Ctrl-` (to open the Terminal)
dotnet restore

Next, open Program.cs. This is the starting point of the application, which simply types something to the console. Change the text that says ``Hello Internet of Things" to say something else. I am saying "It's Tinker Time".

Change program

Run the Program:

Press F5

The Debug Console at the bottom of the screen should show your text:

Debug Console

Compile the Code to ARM

Next, compile the code in a format that the Raspberry Pi can understand:

Ctrl-` (to open the Terminal)
dotnet publish -c Release -r ubuntu.16.04-arm

Publish App to Raspberry Pi

The code was compiled to a directory that we'll browse to, and open in another copy of VS Code. Either use the command prompt or Windows Explorer and go to this directory:

..\TinkerPIoT\bin\Release\netcoreapp2.1\ubuntu.16.04-arm\publish

Once there, type:

Code . (once you are in the directory below)

Open the Binaries in Code

As we learned in the tutorial on setting up VS Code, press F1 then ftp-sync:Init:

F1 FTP Sync

Fill in the settings to sync up the directory with the directory you want to use on your Raspberry Pi:

Ftp-Sync Init

Putty into your Raspberry Pi and create the directory where you want to publish:

cd /home/pi/TinkerPi/web
mkdir TinkerPIoT
ls

Create Directory on Pi

Run the ftp-sync plugin:

Press F1
ftp-sync: Local to Remote
.choose this folder
Full Sync
Run all X Operations

Go back to your Raspberry Pi, and set the rights on the executable:

cd TinkerPIoT
chmod 777 TinkerPIoT

Run the program

./TinkerPIoT

Run the Program

Up Next

That was a process, but once set up you can compile and sync your code very quickly. VS Code is a great development tool that you can use to develop, not just C#, but most any modern language using plugins for intellisense and such. Please join me as we dive deeper into building apps and building projects with the Tinker Pi and the IoT

Happy Coding ~lory~

Be sure to subscribe to our channel!