05 January, 2019

Raspberry Pi and 3d printed Rain meter - Part 2 - Loading and testing dotnet core

I am a c# developer by day, and although I can kind of work in Python, I dont like it.
Since dotnet core now runs on most Linux systems including the Raspberry Pi, I thought I would give it a go and see how it works. Only problem might be talking to the GPIO pins on the board but I am sure I can find some type of solution

  1. Loading dotnet Core onto the RPi
    I followed the instructions on this page with a few minor changes after I ran into trouble.
    These are the commands I used:
    $ apt-get install wiringpi

    $ sudo apt-get -y update

    $ sudo apt-get -y install libunwind8 gettext

    $ wget https://dotnetcli.blob.core.windows.net/dotnet/Sdk/2.1.300/dotnet-sdk-2.1.300-linux-arm.tar.gz

    $ wget https://dotnetcli.blob.core.windows.net/dotnet/aspnetcore/Runtime/2.1.0/aspnetcore-runtime-2.1.0-linux-arm.tar.gz

    $ sudo mkdir /opt/dotnet

    $ sudo tar -xvf dotnet-sdk-2.1.300-linux-arm.tar.gz -C /opt/dotnet/

    $ sudo tar -xvf aspnetcore-runtime-2.1.0-linux-arm.tar.gz -C /opt/dotnet/

    $ sudo ln -s /opt/dotnet/dotnet /usr/local/bin
  2. SSH Deploy to the rescue.
    I have VS2017 installed on a windows PC, and decided to use that for my development environment but there is a problem ... How do I easily get the code from my Windows PC onto the RPI to test. Luckily I found this program called SshDeploy. This made my life much easier.
    Following the instructions on the site I installed it added the xml tags to my project and tested it. Each time after a successful compile it auto sends the program data to the pi.
  3. Finding a Nuget Package for the GPIO pins.
    Next thing was to proof that I can communicate with the GPIO pins correctly. I did a search on NUGET for GPIO and went to each web site to see how much info there was in case I got stuck. I settled on  Unosquare Raspberry IO.Lots of help and a good description. What sold me on the solution was the ability to use callbacks so no need for a while (true) loop.
  4. Testing the GPIO pins.
    I created a new dotnet core 2.1 command line program, followed the steps of SSH Deploy and added this code:
    using System;
    using Unosquare.RaspberryIO;
    using Unosquare.RaspberryIO.Gpio;

    namespace TestGpio
        class Program
            static void Main(string[] args)
                Console.WriteLine("Gpio Callback Test 3");

                var pin = Pi.Gpio.Pin00;
                pin.PinMode = GpioPinDriveMode.Input;
                pin.InputPullMode =


                pin.RegisterInterruptCallback (


                Console.WriteLine("Press any key to exit...");

            static void Pin00Callback()
                var pin = Pi.Gpio.Pin00;
                var state = pin.ReadValue();
                Console.WriteLine("Pin Activated..." + state);
  1. I compiled the code, and confirmed it SFTPed it to the Raspberry Pi.
  2. I logged into the RaspBerry Pi with my user name and went to the deployed folder and verified the files were recent.
  3. Then I ran the program using this command:
    $ sudo dotnet TestGpio.dll
  4. This is the output:

No comments: