Getting started with the Raspberry Pi 2, for .NET developers

So you got your shiny new Raspberry Pi 2 (or the slightly older, original Raspberry Pi), and you want to do something with it as a .NET developer. Of course you’ve read the news yesterday that Windows 10 will run on the Raspberry Pi 2, but today it’s not yet available. However, even today it is possible to leverage your .NET super powers and write some code that runs on the Pi. In this short walkthrough I’ll show you how to get started.

Things you will need:

  • 1 Raspberry Pi or a Raspberry Pi 2 (obviously)
  • 1 4GB or bigger SD card (for the Pi) or MicroSD card (for the Pi 2), this will be the Pi’s “hard disk drive”
  • 1 UTP cable to connect the Pi to your router/hub/switch (so it can connect to the internet)
  • 1 Windows laptop/desktop/hybrid/tablet (of course not necessary to use the Pi, but I’ll use the Windows machine in my guide to write the SD card)
  • Optionally: USB mouse and keyboard, HDMI screen. As mentioned, this is optional since you can also run your Pi headless (without monitor attached). In that you’ll connect to it via remote prompt (SSH) and/or remote desktop (RDP, VNC …). In this guide I’ll show you both options.

Step 1: download and write the Raspbian Linux image

Since Windows 10 is not yet available for the Pi, we’ll use the typical Raspbian Linux operating system, which is based on the Debian Wheezy distro. Go to http://www.raspberrypi.org/downloads/ and download the latest zipped Raspbian image. When done, extract the ZIP file, which should contain 1 .IMG file.

Now we need to write the .IMG file to the SD card. The easiest way to do this on a Windows machine is by downloading and installing the free Win32 Disk Imager tool. When you run it, select the extracted .IMG file, select the drive which corresponds with your SD card reader and click Write. The writing of the image file will take a couple of minutes, so it’s a great time to get a coffee.

step1_win32diskimager

Step 2: Boot the Pi for the first time

When the image is written, insert it to your Raspberry Pi and plug in the UTP cable. If you’ll be using a mouse, keyboard and HDMI screen (instead of the headless setup): plug in those things as well.  Check the Raspberry Pi site to see where can find all the connections on your board. Finally give the Pi some power via the Micro USB cable. After a minute or 2 the Pi will be booted. If you have connected a screen, you’ll be able to see the progress. When you are running it headless, the waiting is less exciting and always feels a lot longer. 🙂

Step 3: (optional) Connect to your Pi remotely

If you have connected a mouse, keyboard and screen you can skip this step. To be able to connect to your Pi remotely, first we’ll need to figure if it’s successfully booted and connected to your network. By default your Pi will have the hostname raspberrypi, so on your Windows machine open a command prompt and type ping raspberrypi. If everything is ok, you should see the Pi replying (and you’ll know its IP address). If the hostname doesn’t work, you can also check your router’s Local Network client list to find the IP address.

step2_ping

On your Windows machine, well need to install an SSH client. I’m a big fan of the free Bitvise SSH Client, but there are other clients as well, like Putty for example. Once your SSH client of choice is installed, make it connect to raspberrypi (or the IP address) on port 22 with username pi and default password raspberry.

When successfully connected, automatically an SSH remote command prompt will open and you can move to the next step.

Step 4: Configure & update your Raspberry Pi

The first command we’ll execute, either using the attached keyboard/screen or the remote SSH prompt) is sudo raspi-config which will configure Pi. (FYI: sudo is the command to run an application with elevated privileges.)

When the config tool has started, select 1 Expand Filesystem and hit enter. This will make sure the entire size of your SD card will be used. It’s not necessary to reboot when prompted. Secondly it’s probably wise to change the pi user’s password using option 2 in the config menu. Next, move to the fourth config option Internationalization Options, to change your locale, timezone and keyboard layout (unless you are ok with the defaults). Finally you can choose Finish to close the config tool (you can always run it again). When prompted choose Yes to reboot the Pi.

Once the Pi is rebooted, connect to the command line again (either via SSH or locally). We need to make sure our Pi is running the latest and greatest packages, so execute the following commands:

  • sudo apt-get update (to make sure your source list is up-to-date)
  • sudo apt-get upgrade (to upgrades all installed packages)

This is quite important since if you source list of packages is not up to date, the apt-get install command could fetch older versions of the packages you going to install in the next steps!

Step 5: (optional) Enable Remote Desktop connections and/or VNC

When you are running headless, it’s very interesting to be able to connect to the GUI via the Remote Desktop Protocol (RDP). When you are using a connected screen, you’ll be able to see the GUI on the screen of course, but you can still enable RDP. So in the remote SSH command prompt, or in the local prompt via the keyboard/screen, type sudo apt-get install xrdp. When prompted hit enter or Y to confirm the installation of the XRDP package.

Now we are ready to connect to the Pi using RDP: on your Windows machine you can open the Remote Desktop Connection application and tell it to connect to raspberrypi (or its IP address). When prompted enter the pi username and your chosen password. If everything goes ok, you should be able to see the Raspbian desktop.

Instead of RDP, it’s also possible to use VNC to connect to the GUI remotely (and in some scenarios I actually prefer this since VNC leaves your session open when you disconnect the client, so you can easily connect to it again). To install a VNC server (optional for this guide) type sudo apt-get install tightvncserver. When this is done, you need to start the VNC server by typing vncserver (the first time you run it, it’s going to ask you to choose a password). On your Windows machine you can download the VNC Viewer (or another VNC client) to connect. By default, the tightvncserver will be running desktop 1 on port 5901, so from the VNC client connect to raspberrypi:5901.

Step 6: Install Mono and MonoDevelop

If you want to run .NET code on the Linux OS, we need to install the excellent open source Mono .NET Framework. To make sure you will get the latest version of Mono, we need to add additional package repositories. First add the Mono Project GPG signing key by typing in the (remote or local) command prompt

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

Then type

echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list

for the first repository. Now enter

echo "deb http://download.mono-project.com/repo/debian wheezy-apache24-compat main" | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list

for the second repository. Once added, we need to execute the sudo apt-get update and sudo apt-get upgrade commands again (like we did in Step 4). Cool, now we are finally ready to install Mono: enter

sudo apt-get install mono-complete

When prompted hit enter or Y to confirm the installation of the complete Mono framework. Give it some time to complete, it takes like 5 minutes.

Of course you can use your trusted Visual Studio to write .NET code for the Pi, but there is also an open source IDE from the Mono project: MonoDevelop. Let’s install this one as well, just because it’s fun. 🙂 In the prompt type sudo apt-get install monodevelop. Again when prompted hit enter or Y and wait for the install to complete. Additionally we need to do the same for the XTERM package, so type sudo apt-get install xterm and confirm again.

Step 7: Use MonoDevelop to write some C#

Now let’s use the MonoDevelop IDE to write the traditional Hello World console application. If you’ve done step 5, you can connect via RDP to the Raspbian GUI. If you are using a connected screen, type startx in the command prompt to start the GUI. In the Start menu, you can find the MonoDevelop IDE in the Programming category.

step7_start

Once started, you can choose File, New, Solution to create a new Console Project (don’t forget to give it a name). The template already gives us the Console.WriteLine, so you can just hit F5 to start a debug session and see the output. Pretty cool isn’t it?

Step 8: (optional) Use Visual Studio to write code

If you want to write a bit more code, you probably want to use the goodness of Visual Studio, instead of the limited (but nice!) MonoDevelop IDE. It’s actually very easy: on your Windows machine fire up Visual Studio! Remember Microsoft recently made Visual Studio Community edition available for free, individual developers, small organisations, students … so there is no reason to not use it!

Create a new Console Application (targeting .NET 4.5.1), write the code and build the project. Now we just need to copy the compiled .EXE of the Console Application to the Raspberry Pi. Using the Bitvise SSH Client, this is pretty easy since it contains an SFTP client as well. From the Bitvise SSH Client main window, click on the New SFTP window button which will then show you at the left side your local Windows file system and on the right side the Raspbian file system. Copy the .EXE file for example to the Raspbian Desktop folder.

step8_sftp

Once copied, go back to the GUI session (either locally or via RDP) and double click on the copied .EXE, which should show your code in action.

If you prefer to run the Console Application in the command prompt, navigate to the location of the .EXE file and type mono NameOfYour.exe.

Where to go from here?

I showed you how to get up and running with the Raspberry Pi, leveraging your existing .NET skills. Now you know the basics, you can try for example to host an ASP.NET application using OWIN (see my walkthrough). Really the sky is the limit! If you’ve got feedback, a question, a cool example, … feel free to drop a comment below.

(this post was updated on Feb. 9th, to be precise the update/upgrade was added to Step 4)
(this post was updated on Feb. 11th, to be precise the additional repositories were added to Step 6)

  36 comments for “Getting started with the Raspberry Pi 2, for .NET developers

  1. February 12, 2015 at 9:55 pm

    Hi Jan,

    as always… great post.

    I would like to make 1 small correction to your posting. The double quotes (“) in your post are messed up. With the messed up double quotes the xamarin list won’t get loaded.

    Perhaps you can post the copy/past lines next time in a code block

    Bas

    (posted from my rPi2 while installing mono 🙂 )

    • February 12, 2015 at 10:30 pm

      Hi Jan,

      one small addendum:

      I received an error on attempting to build that the filename has not been set. Details of error received are as follows.

      System.InvalidOperationException: File name has not been set at System.Diagnostics.Process.Start_common….

      If you receive this error, you need to apply the following fix: MonoDevelop has apparently a dependency to xterm. Go to terminal and “sudo apt-get install xterm”. From then on, you can build your solutions 🙂

      • Russell
        December 17, 2015 at 1:25 am

        Thanks for this Bas, your comment helped me get things working!

  2. February 23, 2015 at 3:58 pm

    Jan,

    Did you manage connecting to a sql server azure db from mono/linux ?
    I’m getting error : SSL encryption for data sent between client and server is not implemented.

    • jan
      February 23, 2015 at 9:01 pm

      No, never tried. I’d rather recommend to put the SQL DB behind e.g. a Mobile Service. See my latest post.

      • Mauricio
        March 7, 2016 at 3:25 pm

        Hi Jan, do you know if the repositories are valid for Raspi 3 and its lastest Raspbian release?

  3. Thomas Schmidt
    February 24, 2015 at 6:41 pm

    Hi Jan,
    what I did was create a sources share on my NAS. I targeted VS there and on my RasPi I mounted the share via Samba. So I can immediately test run my compiled exe.
    Thomas

    • jan
      February 24, 2015 at 8:32 pm

      Cool Thomas, I’m playing around with automatic builds/deployments to automate stuff.

  4. February 28, 2015 at 11:23 am

    Thanks for the tutorial!
    Only problem I had was that I couldn’t telnet into the Pi using the raspbian wheezy distro until I’d connected a keyboard and changed the password to password.

    Any idea what the default password is with a new distro?

    • February 28, 2015 at 11:44 am

      oops. I see you say the password is raspberry, not password.

  5. February 28, 2015 at 11:23 am

    Thanks for the tutorial!
    Only problem I had was that I couldn’t telnet into the Pi using the raspbian wheezy distro until I’d connected a keyboard and changed the password to password.

    Any idea what the default password is with a new distro?

    • February 28, 2015 at 11:44 am

      oops. I see you say the password is raspberry, not password.

  6. March 6, 2015 at 11:27 am

    Jan,
    It’s a pitty that docker doesn’t play well together with the raspberry pi otherwise one could do similar stuff as in this article I just finished:
    http://blog.opinionatedapps.com/webapi-aspnet-vnext-mono-via-docker-on-ubuntu-linux-part-2/

    cheers

  7. March 6, 2015 at 11:27 am

    Jan,
    It’s a pitty that docker doesn’t play well together with the raspberry pi otherwise one could do similar stuff as in this article I just finished:
    http://blog.opinionatedapps.com/webapi-aspnet-vnext-mono-via-docker-on-ubuntu-linux-part-2/

    cheers

  8. Jim
    April 6, 2015 at 5:24 pm

    Thank you so much for the great post. Very clear to follow.
    Gave it a try with VS and worked like a charm. This does leave a burning new question now. How would one get access to the I/O through doing things this way. For instance say I have a LCD tied to the Pi, how in C# would I be able to throw things on the LCD ?

  9. Jim
    April 6, 2015 at 5:24 pm

    Thank you so much for the great post. Very clear to follow.
    Gave it a try with VS and worked like a charm. This does leave a burning new question now. How would one get access to the I/O through doing things this way. For instance say I have a LCD tied to the Pi, how in C# would I be able to throw things on the LCD ?

  10. Martin
    May 7, 2015 at 10:41 am

    I used Raspberry PI 2 tipe B with Kali Linux arm

    I have already install until the step on install mono-complete
    But, when i install monodevelop with apt-get install monodevelop, i have this error

    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:

    The following packages have unmet dependencies:
    monodevelop : Depends: libgnome2.24-cil (>= 2.24.0) but it is not installable
    Recommends: libglade2.0-cil-dev but it is not going to be installed
    Recommends: libgtk2.0-cil-dev but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.

    How to fix this?
    Thans

    • Bhakthi
      October 26, 2015 at 4:48 pm

      Hi Martin,
      I don’t think you can use Pi 1 for this as it uses ARMv6. Mono requires ARMv7 architecture. if you want to use this on Pi 1, you will have to re-compile mono to match ARMv6 architecture.

      • Russell
        December 17, 2015 at 1:30 am

        Hi Bhakthi,

        I am using mono on raspberry pi model B+ at the moment and it is working fine.

        • Russell
          December 17, 2015 at 1:32 am

          I am using Debian though…

  11. Shawn
    August 18, 2015 at 3:22 am

    I am a bit of a newbie here but what am I missing? How can you just move the compiled files from windows over to the Pi without recompilation for Linux? Are the .exe and dlls just intermediate language that Mono will JIT for us? If so that is pretty cool 😉

  12. JG
    August 19, 2015 at 7:17 pm

    Works AOK! Thanks – Jos

  13. Gusman
    March 2, 2016 at 2:17 pm

    Just a note, the second repo is only needed if you’re going to use Apache + mod_mono

  14. Simon L
    April 21, 2016 at 1:26 pm

    Does Mono support UWP application ?
    I developed an app for windows iot but this hasn’t MTP drivers support so now i want try to install Rasbian.

  15. TheLogan
    July 13, 2016 at 5:06 pm

    I got an error during the installation of mono
    dpkg returned an error code (1)

    I didn’t think much of it and just ran the installer again (though this time the installer did nothing much), and when I finally came to the end of the guide I transferred a tiny console program to raspi and it does nothing much. When I doubleclick the exe file it opens a console window up that closes instantly (this is the mono terminal), when I try to run it by writing

    mono file.exe

    it returns “Illegal instruction”, seems mono is bugging out completely, I’m going hunting for a way to remove mono again and try to re-install it, I’ll post again if that works

    • TheLogan
      July 13, 2016 at 5:22 pm

      Well, I’ve found out how to remove programs, interestingly I get the same error this time around

      E: Sub-process /usr/bin/dpkg returned an error code (1)

      Not sure if that then means that it has not entirely removed it or what?

Leave a Reply

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