Posted by: lluppes | March 19, 2015

.NET on a Raspberry Pi 2???

The Raspberry Pi has been a great hit, selling over 5 million devices since its introduction in 2012.  People have used it in schools to teach children about programming, and hobbyists have been building lots of interesting projects with it. One of the great things about the Pi ecosystem is that everything is very inexpensive.  The latest Raspberry Pi 2 has a 900 MHz quad-core processor for only $35 — wow!  You can buy all of your components and have a fully functioning computer for less than $100.

However, one of the problems (although some people might say that’s an advantage!) is that it’s a Linux computer and uses languages that are not familiar to the millions of experienced .NET developers out there right now.  Sure – we could all learn Python or Scratch or whatever the latest small device language is… but why?

What if we could use the .NET languages we know and love and use most of our installed code base and then deploy that on these little tiny computers?  There are millions of .NET programmers out there with billions of lines of tested code in their project libraries.  I believe you can use that code easily on the Pi, and I’ll show you how you can do it in just a few short steps in this guide.

<TL;DR>    (a.k.a.  Too Long – Didn’t Read!)

These scripts are available on https://github.com/lluppes/pisharp.
A free eBook with all this info and more is available at
https://raw.githubusercontent.com/lluppes/pisharp/master/RaspberryPiNetDeveloper_v1_Beta.pdf

</TL;DR>

Divide and Conquer

I’ll break this down into two blog posts because doing it justice in one makes it two long:

Part 1 – A Beginner’s Guide to setting up your Raspberry Pi (this post)
Part 2 – Installing Mono and ASP.NET vNext on your Raspberry Pi

Time to Buy Some Goodies

You need to see this for yourself and getting started is relatively inexpensive.  Here what I would recommend that you start with for your shopping list:

Raspberry Pi 2 Model B (1 Gig) $ 35.00
http://www.mcmelectronics.com/product/83-16530

32G microSD Card (You could go smaller, but why? To save $5?) $ 13.99
http://www.amazon.com/gp/product/B003WGJYCY/

Mini Wireless Keyboard (It’s really cool!) $ 13.99
http://www.amazon.com/gp/product/B00KF9IVKC/

USB WiFi Adapter (Less wires is better, right?) $ 14.99
http://www.amazon.com/gp/product/B003MTTJOY/

Plastic Case $ 9.49
http://www.amazon.com/gp/product/B00OBBFP0O/

Total $87.46

There are also a couple of things that you will need that you probably already have laying around your office:

  • A USB power supply with a micro USB cable (an old phone charger will probably work just fine – you need a 5V adapter).
  • You will need a spare monitor of some kind that supports HDMI. You don’t really need a dedicated monitor – you only really need it during the setup, so you can share with your computer if needed.  If your monitor only has a DVI connection, you can buy an HDMI to DVI adapter
  • HDMI – DVI Adapter Cable http://www.amazon.com/gp/product/B001TH7T2U/  $7.99

Once you have these components, you’re all set to go.

Installing the Operating System

Once you have that little box of Raspberry in your hot little hands, it won’t do any until you install an operating system – these little computers don’t even have a hard drive.  To get rolling, you will need a formatted microSD Card.  You might think you could just format it in Windows Explorer, but you are better off using a utility that is designed to format SD cards optimally.

You can download a free formatter from
https://www.sdcard.org/downloads/formatter_4/
Install and open the utility, insert and select your microSD card, and begin formatting.

There are several operating versions available at http://www.raspberrypi.org/downloads/.
If you feel like you must be a real geek, go ahead and install one of the many Unix variants.  Be aware though that as of the time of this document, not all of them work on the Raspberry Pi 2, so I’d stick with Raspbian for now.  If you just want to get started quickly, simply download the NOOBS install – it’s already prepared to make this quick and easy.  All you have to do is unzip the downloaded file to your PC, then copy all of the files on to your newly formatted SD card and you are ready to boot up your Pi.

Initial Power Up

Connect all of your new hardware, put microSD card with the extracted NOOBS files into your Pi, and then power it up.  You will see a NOOBS installation menu – choose Raspbian from that menu:

piNoobsInstaller

The installer defaults to the Great Britain locale (because the Pi was created there), so you may want to change the language and keyboard to US instead of GB.  The one issue I found with using the GB keyboard that made me change it is that I could not find a way to enter the “\” character. (Maybe there is a way, but I couldn’t find it!)

Once you click “Install” the Pi will go to work rebuilding your SD card with the operating system.

piInstallingOS

After a few minutes, you’ll be ready to reboot and configure your Raspberry.  The first time it boots up you will automatically be greeted with the Config Tool.

piRaspConfig

You don’t have to set most of these because NOOBS takes care of most of it, but you will want to change these three options:

  • #2 – Change User password – the default User Id is “pi” and password is “raspberry”. Since every noob knows this, if you want any kind of security, you really should change this (you can always do it later — just don’t forget!)
  • #3 – Enable Boot to Desktop – Select “Desktop Login as pi at the graphical desktop”, unless you always want to have to type a command at the command line to start the user interface.
  • #4 – If you changed the language to English on the install screen, you will need to select the Internationalisation Options and install the “en-US.UTF8 UTF8” locale, and then change the default keyboard on the follow up screen. It will then take a minute or two to generate the proper locale files.

Tab down to Finish and press enter and your Pi will reboot.  If you miss Option 3 and end up at the command line instead of the desktop, you can always start the GUI with the command “startx”. If you want to get back to this screen later, just open a command prompt and type “sudo raspi-config”

It’s Alive!

Congratulations – You now have a working Raspberry Pi!  Granted, it doesn’t do much yet, but it’s a start.  You should be seeing the home screen:

piRaspbianStartup

The first thing you will need to do is to get your network connection running.  If you connected an Ethernet cable hard line, it should be already up and working already.  You can open a web browser and browse to any website to verify this.

If you want to use WiFi, you will have to do a bit of configuration to get it up and running.  Open the Menu -> Preferences -> WiFi Config menu item.  On the “wpa_gui” window that pops up, do the following:

  • Select the Manage Networks Tab.
  • Select the Enabled radio button
  • Click Scan button to open Scan Results popup window
  • Select your network and then enter in your wireless key in the PSK textbox on the popup screen

Before you leave this screen, you will want to find your IP Address (you’ll want that later), located on the Current Status tab.  If you want to find it use a command line, use the “ifconfig” tool (a cousin to the Windows ipconfig command).  I usually add a “grep 255.255” command to filter it down to just the line you are interested in. (grep is a Unix search utility), like this:  “ifconfig | grep 255.255”   Once you have an IP address, open your web browser and go to some website to test out your connection.

If you want, you can change your Pi to use a Static IP address, which might make it easier down the line.  I don’t usually bother with this as I haven’t had an issue using DHCP on my home network because it almost always keeps the address, and I can always look it up if I need to.

Remote Desktop

I’ve found that the easiest way to work on the Pi is to do it with your normal desktop and use a remote desktop program to access to your Pi.  The standard Remote Desktop program in Windows won’t work, but there is an easy (and free!) program available called TightVNC.  You will need to install the server on your Pi, and then run a client on your Windows Desktop.

To install TightVNC on your Pi, open a command line window and enter the following commands:

sudo apt-get install tightvncserver autocutsel
tightvncserver

Here is an explanation of what the commands do:

sudo: asks the system for elevated rights
apt-get: is the Advanced Packaging Tool command (kinda like NuGet)
install: is the action for apt-get, which followed by the packages that you want apt-get to install
tightvncserver: is the name of the program you want to run

My favorite explanation of what the sudo command does is from Randall Munroe’s always excellent xkcd comic:

piSudohttp://xkcd.com/149/

When you issue the “tightvncserver” command, and the VNC Server starts for the first time, you will be prompted to supply a password, and then each time you start your VNC client you will be asked for this password.  You can say safely ignore and say No to the view-only password.  Note the message “New ‘X’ desktop is raspberrypi:1”.  The “1” tells you that VNC is listening on port 5901.  If you run this command again, it would start another program and reply with “raspberrypi:2”, which would mean it was running on port 5902.

To install the TightVNC Client on your Windows Desktop, download the program from http://www.tightvnc.com/download.php and load it on your system.  To connect to your Pi, enter a Remote Host of your IP address plus the “:5901” and click Connect, then you will be prompted to enter your VNC password.

You should now have a remote session open from your Pi on your desktop so you can use your normal mouse and keyboard and start the system update patching process.

Enable Copy/Paste in TightVNC

By default, one feature that is not enabled in TightVNC (that I think is sorely needed!) is the ability to copy and paste from your desktop into and out of your Pi.  To enable that, you will have to install one small feature.  If you followed my instructions earlier when you apt-get installed tightvncserver, you added another package named “autocutsel”.  To enable it, you will need to edit the pi\.vnc\xstartup file and add one extra line of configuration.  The “.vnc” directory is a hidden directory, which is indicated by having a period as the first character of the directory name.  Note that this directory actually won’t exist until you run tightvncserver for the first time.  Open the File Manager, and turn on hidden files under the view menu, then Navigate to the .vnc\xstartup and right click and open up with the text editor.  Find the line “xsetroot –solid grey” line and then add a new line after it that contains the “autocutsel –fork” command.

piEditVNCStartup

piVNCCopyPaste

Save the file and you should be all set. Stop and restart TightVNCServer and you should now be able to copy and paste from your desktop to your Pi to your heart’s content.

Initial Setup

As you read through this manual, you will see a lot of commands and files that will set up your Pi.  I’ve included them in this document so that you can see what they do, but you probably don’t want to type them all manually.  To help you out, I’ve published all the scripts out to a GitHub repository: https://github.com/lluppes/pisharp

To use these script files, first fetch the first master file from the repository by entering a wget command in the command line window on your Pi.  At this point you will have a file, but the operating system doesn’t know that it’s a script, so you have to go tell it to give the file execute permissions by running the “chmod” command. Finally, run that command file using the “sh”, which will in turn fetch the other files and automatically mark them as executable.

wget https://raw.githubusercontent.com/lluppes/pisharp/master/getScripts
chmod 755 getScripts
sh getScripts

Here is an explanation of what the commands do:

wget program that retrieves files from an http* source
chmod 755 marks a file as executable
sh starts a command line interpreter to run some commands

Auto-Start TightVNC

At this point, you have the TightVNCServer installed and running, but if you reboot, it won’t start up until you enter the “tightvncserver” command again.  If you want it to start up automatically (and you do!), do the following.  Create a new file in the init.d directory.  This is protected directory, so you have to use a sudo command to start your editor and get elevated privileges.  At this point most tutorials will spell out a “sudo nano…” command for you.  Why?  Because that’s the Unix admin way to do it, with the ubiquitous and obtuse Nano editor.  But we are .NET programmers and we know there are better tools available, so let’s use them!  Leafpad is the alter ego on the Pi of NotePad, so let’s use that. Enter the command:  “sudo leafpad” and then enter this script and save it as “/etc/init.d/vncboot”.

### BEGUN INIT INFO
# Provides: vncboot
# Required-Start: $remote_fs $syslog sudo etworking
# Required-Stop: $remote_fs $syslog sudo networking
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop TightVNCServer at boot
### END INIT INFO

export USER='pi'
case "$1" in
start)
  echo "Starting VNC Server"
  su $USER -c '/usr/bin/vncserver :1 -geometry 1280x800'
  ;;
stop)
  echo "Stopping VNC Server"
  pkill Xtightvnc
  ;;
*)
  echo "Usage: /etc/init.d/vncboot {start|stop}"
  exit 1
  ;;
esac
exit 0

Tip: You can fetch the contents of the file from GitHub and then simply copy it to the directory using these commands:  (if you ran the getScripts command earlier you already have it, so you can skip the first line)

wget https://raw.githubusercontent.com/lluppes/pisharp/master/1_vncboot
sudo cp /home/pi/1_vncboot /etc/init.d/vncboot

Once you have this file in place, you’ll need to mark it as an executable script with this command:

sudo chmod 755 /etc/init.d/vncboot

You can now start or stop the service manually by running the following commands. Make sure you do this at least once so that you set up the remote access password.

sudo /etc/init.d/vncboot start
sudo /etc/init.d/vncboot stop

I’m not sure why, but it seems to need a reboot here before you do the next update command or it doesn’t work…  so you should reboot your Pi now by executing the command: “sudo reboot”

Just having that file in the directory won’t make it auto-start.  To do that, you will have to enter the command:

sudo update-rc.d vncboot defaults

Reboot your Raspberry Pi, and you should be able to connect across the network using the VNC Viewer and now you won’t need an extra keyboard, mouse, and monitor for your Pi cluttering up your desktop anymore!

Applying System Patches and Updates

Like any new operating system, there are always patches and updates that you need to install when you first install an operating system.

Open a command prompt (LXTerminal) and run these commands:

sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install htop autoconf automake screen
sudo rpi-update
sudo reboot

You can fetch these commands from GitHub using these commands:

wget https://raw.githubusercontent.com/lluppes/pisharp/master/2_update_os
chmod 755 2_update_os
sh 2_update_os

Tip: If the script stops and asks you to confirm with a “Y/n”, make sure you enter an UPPER case Y, otherwise sometimes the script just stops.

Here is an explanation of what the commands do:

sudo: asks the system for elevated rights
apt-get: is the Advanced Packaging Tool command
update: will download software package lists
dist-upgrade: updates the software packages (this different than just using “upgrade” as it retrieves all the dependencies also)
rpi-update: will update the Raspberry firmware
install: will install a new program
htop autoconf automake screen: are useful utilities

Once everything is updated, reboot with a “sudo reboot” command.

Ready To Move On?

Your Pi should be all set to go and you’re all ready to starting doing something cool with it!

Part 2 of this series will detail how to properly install Mono on your Pi, and then get some .NET programs running, and how to configure and run ASP.NET vNext.

Advertisements

Responses

  1. […] 1 – A Beginner’s Guide to setting up your Raspberry Pi Part 2 – Installing Mono and ASP.NET vNext on your Raspberry Pi (this […]

  2. Hey amazing guide you have there.
    I would like to offer a suggestion concerning the Remote Desktop tool. I have been using the xrdp program on the Raspberry Pi which connects directly with a Remote desktop connection from a windows PC without the need of a third party program on the Windows PC.
    sudo apt-get install xrdp

    • I do like that suggestion! I’ll definitely take a look at that module.


What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: