Skip to content

Instantly share code, notes, and snippets.

@vsigno
Last active July 19, 2022 13:46
Show Gist options
  • Save vsigno/90454e0c41c1ced3e6b6dac799dbcbb0 to your computer and use it in GitHub Desktop.
Save vsigno/90454e0c41c1ced3e6b6dac799dbcbb0 to your computer and use it in GitHub Desktop.
This tutorial shows the steps needed to install .NET6 and to develop, build and deploy a .NET6 App on RaspberryPi.

Running .NET 6 App on RaspberryPi

This tutorial shows the steps needed to install .NET6 and to develop, build and deploy a .NET6 App on RaspberryPi.

Requirements

  • a RaspberryPi (only AMR32v7 versions are supported. E.g. Pi Zero W is not supported)
  • code editor (e.g. VSCode) on the dev machine, Windows or Mac

Preparing the Pi

Follow the usual steps to install RaspberryPi OS, enable SSH and run update and upgrade

sudo apt install mono-complete
  • Install .NET Core 6.0 SDK (Linux/ARM32) and ASP.NET Core 3.1 Runtime (Linux/ARM32)

Note: Check the latest versions on https://dotnet.microsoft.com/download/dotnet-core/6.0

wget https://download.visualstudio.microsoft.com/download/pr/a218e3b9-941b-43be-bfb1-615862777457/80954de34ab68729981ed372a8d25b46/dotnet-sdk-6.0.301-linux-arm.tar.gz
wget https://download.visualstudio.microsoft.com/download/pr/084bfc2b-f28d-4995-87f0-d82519245825/7f5398fc2caf95355b154856868ef560/aspnetcore-runtime-6.0.6-linux-arm.tar.gz
  • create a new folder and unzip both tar archives
sudo mkdir dotnet-arm32
tar zxf aspnetcore-runtime-6.0.6-linux-arm.tar.gz -C $HOME/dotnet-arm32
tar zxf dotnet-sdk-6.0.301-linux-arm.tar.gz -C $HOME/dotnet-arm32

Make them accessible globally

export DOTNET_ROOT=$HOME/dotnet-arm32 
export PATH=$PATH:$HOME/dotnet-arm32

Note: path needs to be added after every restart. To add it on autostart

sudo nano .profile

and add these lines at the end of this file

# set .NET SDK and Runtime path
export DOTNET_ROOT=$HOME/dotnet-arm32
export PATH=$PATH:$HOME/dotnet-arm32

Reboot the Pi (sudo reboot) and run dotnet --info to check the installation

Optional for playing sounds

NetCoreAudio is a simple cross-platform library to play audio on .NET Apps. In order to playback MP3 files, the library mpg123 is needed

sudo apt-get install mpg123

Developing the App on Win/Mac

Be sure to have the correct .NET6 installed and that dotnet is available globally

  • In VSCode, open a terminal and create a new console App
dotnet new console -o NAME-OF-APP --framework net6.0
  • to add packages use dotnet add package NAME-OF-THE-PACKAGE, NuGET is the main portal to find packages. For example
# Access to GPIO on Rpi
dotnet add package RaspberrySharp --version 1.40

# MQTT Client
dotnet add package M2Mqtt --version 4.3.0

# Playback library for WAV - it needs mpg123 for mp3
dotnet add package NetCoreAudio --version 1.6.3

# Mangage signalling 
dotnet add package Mono.Unix --version 7.1.0-final.1.21458.1

Note: be sure that the packages are compatible with the version of .Net chosen

Some additional parameters can be added to NAME-OF-APP.csproj to control and optimise the build process: create a single file; trim the not used DLL; define the target framework (.Net6.0 in this case).

Note: the <ItemGroup> is populated automatically using dotnet add package NAME-OF-THE-PACKAGE

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <SelfContained>true</SelfContained>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>Link</TrimMode>
    <PublishReadyToRun>true</PublishReadyToRun>
    <IncludeNativeLibrariesForSelfExtract>
    true
    </IncludeNativeLibrariesForSelfExtract>
    <PublishSingleFile>true</PublishSingleFile>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="M2Mqtt" Version="4.3.0" />
    <PackageReference Include="Mono.Unix" Version="7.1.0-final.1.21458.1" />
    <PackageReference Include="NetCoreAudio" Version="1.6.3" />
    <PackageReference Include="RaspberrySharp" Version="1.4.0" />
  </ItemGroup>
</Project>

Build and deploy the App for linux-arm

dotnet publish --configuration FOLDER-NAME --runtime linux-arm

Copy the release file ./bin/FOLDER-NAME/net6.0/linux-arm/publish/NAME-OF-APP on RPI (e.g. FileZilla)

Note: Linux file doesn't have extension

Make it executable and run it

chmod +x ./NAME-OF-APP
sudo ./NAME-OF-APP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment