Hack Raspberry Pi – How To Build Apps In C#, WinForms and ASP.NET Using Mono In Pi

By Anoop Madhusudanan

Vote on HN

imageRecently I was doing a bit of R&D related to finding a viable, low cost platform for client nodes. Obviously, I came across Raspberry Pi, and found the same extremely interesting. Now, the missing piece of the puzzle was how to get going using C# and .NET in the Pi. C# is a great language, and there are a lot of C# developers out there in the wild who are interested in the Pi.

In this article, I’ll just document my findings so far, and will explain how develop using C# leveraging Mono in a Raspberry Pi. Also, we’ll see how to write few minimal Windows Forms & ASP.NET applications in the Pie as well.

Step 1: What is Raspberry Pi?

Raspberry Pi is an ARM/Linux box for just ~ $30. It was introduced with a vision to teach basic computer science in schools. How ever, it got a lot of attention from hackers all around the world, as it is an awesome low cost platform to hack and experiment cool ideas as Pi is almost a full fledged computer.  More About R-Pi From Wikipedia.

The Raspberry Pi is a credit-card-sized single-board computer developed in the UK by theRaspberry Pi Foundation with the intention of promoting the teaching of basic computer science in schools. The Raspberry Pi has a Broadcom BCM2835 system on a chip (SoC),[3] which includes anARM1176JZF-S 700 MHz processor (The firmware includes a number of "Turbo" modes so that the user can attempt overclocking, up to 1 GHz, without affecting the warranty),[4] VideoCore IV GPU,[12] and originally shipped with 256 megabytes of RAM, later upgraded to 512MB.[13] It does not include a built-in hard disk or solid-state drive, but uses an SD card for booting and long-term storage.[14] The Foundation's goal is to offer two versions, priced at US$25 and US$35.

Another good introduction from Life Hacker is here

Step 2: Setting up your Development Environment

Let us have a quick look at setting up the development environment.

If you have a physical Raspberry Pi

Raspberry Pi boots from an SD Card. Basically, this means you can transfer an operating system image to an SD Card, and boot your Pi from there. The Raspberry Pi download page lists multiple OS images you can use to load your SD Card. You will need to unzip it and write it to a suitable SD card using the UNIX tool dd. Windows users should use Win32DiskImager. There is a simple guide about how to setup your SD card if you are wondering how to do this. Then, you can connect your Pi device to the TV using an HDMI cable, and can hook up an old USB keyboard to start hacking.

Though multiple images are available in the below mentioned download page, please note that at this point only the Soft-float Debian “wheezy” image can be used to install Mono with out issues. This is because Mono doesn’t support hard-float for Raspberry Pi (simplified version) and some runtime methods like DateTime won’t work if you are using a hard float version. So, you need to download the Soft-Float Debian image and transfer the image file to the SD card. If you feel a bit adventurous, you can try this hard-float port of Mono for the Pi, but it is not supported.

If you don’t have a physical Raspberry Pi

You can still download one of the images from the download page and boot from an emulator like Qemu, which supports ARM architecture.

Get If you are on Windows and if you are not familiar with Qemu, you can download this pre-packaged version of  Qemu + Raspberry Pi from here – How ever there is a problem. The Image file that comes with this download is the Hard float version. As we need Mono to work properly, download the Soft-Float Debian Wheezy image – and copy that to the Qemu folder, and modify run.bat in qemu folder to the below command line to point it to the correct image. (If you downloaded Qemu and Soft Float Debian Wheezy image separately, you can create a handy run.bat). Note that as of this writing, the image Soft float image is 2012-08-08-wheezy-armel.img – You need to modify this accordingly. The –cpu switch specify the CPU architecture, –hda attaches the image as the boot device and –m specifies the memory. I’m allocating 512 256 MB to my virtual machine. (Note: Updated the command line to use 256 memory based on the comment from Peter below)

qemu-system-arm.exe -M versatilepb -cpu arm1176 -hda 2012-08-08-wheezy-armel.img -kernel kernel-qemu -m 256 -append "root=/dev/sda2"

Step 3: Booting the Pie And Installing Mono

Once you boot the Pie, enter the username and password based on the image/distribution you are using (user/password is normally pi/raspberry).  Now, to install mono, we’ll use the apt-get package manager. Let us update and upgrade all the packages before we proceed with the install. Run apt-get with admin permissions, using sudo in the console/terminal.

Each of the below command will take a bit of time - so make sure you've a pressure ball around. And let the force be with you for a successful installation #lol. 
sudo apt-get update --fix-missing

sudo apt-get upgrade --fix-missing

sudo apt-get install mono-complete --fix-missing

Step 4: Writing C#

Assuming you’ve got the installation right - Time to write a little bit of C#. Mono has a CSharp command line (REPL), you can bring it up by typing CSharp. So, just play around. If you are setting this up to teach CSharp to kids in schools (original Raspberry Pi vision), the force is stronger in you than you think (Sorry, HBO is now re-telecasting all the old Star Wards stuff). What ever. Here is my CSharp console. Note that if your DateTime.Now is behaving madly, you never read my above paragraph and you’ve got the wrong OS image.



Step 5: Write And Compile Your Code – A Windows Forms app

Quit the CSharp console (quit) and start the Window manager by typing ‘startx’ – Never mind if you are already there. This should bring up the Raspberry Pi desktop.  Now, let us create a CSharp file using Leaf pad, a light weight editor in the Raspberry Pi. Either in Run or in a terminal window, type

sudo leafpad /opt/windowapp.cs
And type in some code. Let us create a quick Windows forms application to ensure this is possible in Mono in the Pi.



Now, let us compile the code using the mono compiler (gmcs). Ensure that you are providing the reference libraries we are importing using the /r switch. This should produce the windowapp.exe executable.



Cool. Now time to run it via Mono.  This should display a tiny window with width and height 100 – You can drag the corners to see the little Window we created. From here onwards, it is your normal .NET style. Add controls, build apps and have a blast.


Step 6: Installing The XSP Web Server For Some ASP.NET Love

Let us now install Mono XSP server so that we can run some ASP.NET code in the Pie, to use it as a tiny web server.  Let us get the XSP bits using apt-get. Fire up your terminal, and type

sudo apt-get install mono-xsp2 mono-xsp2-base asp.net-examples

Which should install XSP.


Cool, you got the XSP installed. Now, let us try some ASP.NET stuff in the Pi. Create a new aspx file in /opt folder using Leafpad, as we did earlier. Here is my aspx file, a simple aspx file I copied from Startdeveloper as I was lazy.


Save the file to your /opt folder, and And start XSP2 in the opt folder.  Run XSP2, and open the Internet->Browser and navigate to http://localhost:8080 – You see, it works.




In this post, we explored how to setup Mono to build and run .NET Win forms and Web applications in the Raspberry Pi. Build a computer to learn .NET and C# for under 40$, huh. Follow me in twitter @ https://twitter.com/amazedsaint – Happy Coding.

© 2012. All Rights Reserved. Amazedsaint.com