Getting started with C# on Intel Edison (Yocto Linux)

[Reading Time: 5 minutes]

Intro

Hi there,

after Intel rejected Edison and Co. I was not sure, what to do with the neat piece of hardware, I am owning. Despite of not getting any Support and updates and so forth it is still Hardware, that has WiFi, Bluetooth, SD Card,… onboard. So it has enough capabilities to build cool things. So I decided, not to throw away πŸ˜‰ and see, what I can do with it from another perspective.

With that in mind, I asked myself, if it isn’t possible, to get my favorite dev setup running on Intel Edison: .Net Framework with C#

For half a year, I gave it already a try, but it was a little bit tricky, by downloading the whole package and compiling it on the Edison (Install Mono by hand [“David’s Random Projects and Documents Web Page”]). In my case, there were problems with storage and compile errors. But then, there was a package update for use with opkg, so that I was able, to get Mono Environment installed and usable. Read here, how things work…

Install Mono

Please make sure, that you added these unofficial packages to your
/etc/opkg/base-feeds.conf

src/gz all http://repo.opkg.net/edison/repo/all
src/gz edison http://repo.opkg.net/edison/repo/edison
src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32

After that, type

opkg update
opkg upgrade

(it upgrades all packages, it can eat up your free space!)
in to your bash.

Having that done, only

opkg install mono

is needed and everything is ready to use (takes a moment, to download and configure).

 

Testing Mono installation

You should have a look to your mono version, that is now installed.

Mono –version

The output should display something similar to this, where Mono compiler version is 4.2.2. that maps to .NetFramework 4.6 (I thought, please correct me, if I am wrong).

Writing code

After that, you could write your first test app in C#

Create a test folder (like in screenshot above)

Mkdir mono_test
cd mono_test
vi tests.cs

 (typing β€šI’ for Insert | for Save: ESC β€š:’ after that β€šx’)


using System;

namespace Tests{
    public class Programm{
    static void Main(string[] args){
        Console.WriteLine("This is my first C# App on Intel Edison! :-");
        }
    }
}

For compiling your first App, type:

mcs tests.cs

This builds the code to tests.exe

ls -lh

shows up tests.exe in your folder

running your app is easy; just type:

mono tests.exe

and this is the result:

Adding Hardware access (GPIO,…)

But writing C# Apps only on Intel Edison is not that, what the device is made for, so I needed some access to the underlying hardware. At first I tried to access the mraa libs from Intel by P/Invoke, but thankfully that guy here Mayuki Sawatari wrote an assembly, that has everything in it (γ‚γ‚ŠγŒγ¨γ†γ”γ–γ„γΎγ™, すごいです。).

I tried to get everything compiled at my Edison under Yocto Linux, but that was not possible, therefore I cloned it to my Windows machine and opened the solution file with Visual Studio 2017. The build was successful and I could copy over the resulting Dll to my home directory on Edison, where I now have this available for further development.

git clone https://github.com/mayuki/MraaSharp.git

Again, create a file (e.g. pinTest.cs) and copy this code here (It’s slightly modified version from what you can find inside that Git Repository – I adapted it to my Intel Edison Arduino Breakout Board):

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using MraaSharp;

namespace PinTests
{
    class Program
    {
        static void Main(string[] args)
        {
            Mraa.Initialize();
            Console.WriteLine("Version: {0}", Mraa.Version);
            Console.WriteLine("PlatformName: {0}", Mraa.PlatformName);

            var gpio = new Gpio(MraaIntelEdison.Gp128, MraaGpioDir.Out);
            Console.WriteLine("Gpio Pin: {0}", gpio.Pin);
            Console.WriteLine("Gpio PinRaw: {0}", gpio.PinRaw);

            while (true)
            {
                gpio.Write(MraaGpioValue.High);
                Thread.Sleep(1000);
                gpio.Write(MraaGpioValue.Low);
                Thread.Sleep(1000);
            }
        }
    }
}

This Code is a working Blink example, which blinks onboard LED every second.

Finally

Although Intel Edison is discontinued, it is working and with fresh development tools, it can be sweet to hack some usefull things with it.

The upside with this is, you Can also Code on your Windows machine and test your Code by mocking mraa before releasing it to the device. Welcome to easy DevOps πŸ˜‰

So, if you own one and like to give this here a try, leave a comment or share your Projects.

 

cheers

By Thomas

As Chief Technology Officer at Xpirit Germany. I am responsible for driving productivity for our customers by a full stack of dev and technology in modern times. But I not only care for technologies from Microsofts stack like Azure, AI, and IoT, but also for delivering quality and expertise with DevOps

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.