Microsoft has announced that it's .NET Core is on its way to the Raspberry Pi 3, and an official .NET 2.0 Core, adapted to run on ARM devices, is coming later this year.
Recently .NET Core became available for ARM32 platforms. As it currently stands, it's not an official release, the build of .NET Core offered on GitHub will work with both Ubuntu 16.04, Ubuntu 14.04 and Windows 10 IoT Core with no official support.
Since the Pi day is coming up, let's make an app that will try and calculate the Pi. We will be using Leibniz formula for π to calculate the Pi.
The processThe first thing that we want is to install .NET Core on our main machine. Install .NET Core 2.0 SDK depending on your OS. If you want to make your own app, make sure that you have Visual Studio installed.
Step 1Once you have installed .NET Core 2.0 SDK (either by extracting binaries or using the installer) on your main machine go to your terminal/commandline, create a folder named picalc and go into it.
mkdir picalc
cd picalc
Step 2Now we need to create a new app, so to create a template run the following:
dotnet new console -n picalc
cd picalc
Since we want out program to compile for ARM32, we need to edit our picalc.csproj
So add the following line bellow <RuntimeFrameworkVersion>
:
<RuntimeIdentifiers>win8-arm;ubuntu.14.04-arm;ubuntu.16.04-arm</RuntimeIdentifiers>
The final file should look like this (Note: The Runtime Framework Version will vary, so it may not be the same):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeFrameworkVersion>2.0.0-beta-001737-00</RuntimeFrameworkVersion>
<RuntimeIdentifiers>win8-arm;ubuntu.14.04-arm;ubuntu.16.04-arm</RuntimeIdentifiers>
</PropertyGroup>
</Project>
Explanation:
- win8-arm - Adds compilation support for Windows 10 IoT Core.
- ubuntu.14.04-arm - Adds compilation support for Ubuntu 14.04.
- ubuntu.16.04-arm - Adds compilation support for Ubuntu 16.04.
Now comes the coding part, edit Program.cs
so the content of it as follows (you can also download Program.cs
file bellow):
using System; using System;
using System.Threading;
namespace picalc
{
class Program
{
static string[] text = {
@" ___ ___ ___ ___ ___ ",
@" /\ \ ___ /\ \ /\ \ /\__\ /\ \ ",
@" /::\ \ /\ \ /::\ \ /::\ \ /:/ / /::\ \ ",
@" /:/\:\ \ \:\ \ /:/\:\ \ /:/\:\ \ /:/ / /:/\:\ \ ",
@" /::\~\:\ \ /::\__\ /:/ \:\ \ /::\~\:\ \ /:/ / /:/ \:\ \ ",
@" /:/\:\ \:\__\ __/:/\/__/ /:/__/ \:\__\ /:/\:\ \:\__\ /:/__/ /:/__/ \:\__\",
@" \/__\:\/:/ / /\/:/ / \:\ \ \/__/ \/__\:\/:/ / \:\ \ \:\ \ \/__/",
@" \::/ / \::/__/ \:\ \ \::/ / \:\ \ \:\ \ ",
@" \/__/ \:\__\ \:\ \ /:/ / \:\ \ \:\ \ ",
@" \/__/ \:\__\ /:/ / \:\__\ \:\__\ ",
@" \/__/ \/__/ \/__/ \/__/ " };
static double Pi = 0;
static double n = 1;
static long i = 0L;
static void Main(string[] args)
{
while (i < long.MaxValue)
{
foreach (string s in text)
{
Console.WriteLine(s);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Pi += (4.0 / n);
n += 2.0;
Pi -= (4.0 / n);
n += 2.0;
Console.WriteLine("Generated value of Pi: " + Pi);
i++;
Thread.Sleep(50);
Console.Clear();
}
}
}
}
If you want, you can write your own app using Visual Studio. The process would be the same, you write code and then replace the contents of Program.cs
with your code.
Now that we have our code in place, before restoring all dependencies, make sure there is a nuget.config file next to your other files and it's content is:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
</packageSources>
</configuration>
After we have checked the nuget configuration, it's time to restore all dependencies, so run the following:
dotnet restore
Step 6Finally we can compile our app into an executable. To that run:
dotnet publish -r <runtime identifier>
where <runtime identifier>
is the platform you want to compile for, in this case win8-arm
, ubuntu.14.04-arm
or ubuntu.16.04-arm.
For example:
dotnet publish -r win8-arm
To publish your app to Windows 10 IoT Core and:
dotnet publish -r ubuntu.16.04-arm
To publish your app to Ubuntu 16.04 on your Raspberry Pi.
Step 7Under
./bin/Debug/netcoreapp2.0/<runtime identifier>/publish
or
.\bin\Debug\netcoreapp2.0\<runtime identifier>\publish
you will see the whole self contained app that you need to copy to your Raspberry Pi (I recommend using SFTP for transferring files if you are using Ubuntu on your Raspberry Pi). Note: <runtime identifier>
is the platform you published for.
For Linux (Ubuntu):
- Install Ubuntu 14.04 or 16.04 on your Raspberry Pi.
- Install the prerequisite packages for .NET Core:
sudo apt-get install libunwind8 libunwind8-dev gettext libicu-dev liblttng-ust-dev libcurl4-openssl-dev libssl-dev uuid-dev unzip
- Copy your app to the Raspberry Pi, go into the directory and to manage premissions run:
chmod u+x picalc
- and finally execute your app by running:
./picalc
See your Raspberry Pi calculating Pi!
Note: While it is possible to build the product on the Pi, it isn't easy today and it's slow.
For Windows 10 IoT Core:
- Install Windows 10 IoT Core on your Raspberry Pi.
- Copy your app (.dll file) to your Raspberry Pi.
- Download the CLI from Github Daily Builds - Windows (arm32).
- Copy the contents of the zip to System32 folder on your Raspberry Pi over FTP.
- Go to device portal, navigate to your .dll file and run
dotnet picalc.dll.
.
- Now you will see your Raspberry Pi calculating Pi!
- Error while restoring packages
If you are having issues during the dotnet restore
step, make sure to add the following line in the NuGet configuration file (C:\Users\yourusername\AppData\Roaming\NuGet\NuGet.Config): https://dotnet.myget.org/F/dotnet-core/api/v3/index.json
Thanks to Dmitry Butenko
Comments