18/02/2015

Operate the Raspberry Pi B+ or 2 using Remote Desktop

This article is for those interested in using the Pi for .NET development but there is useful information about how to use tour Pi remotely using Remote Desktop or via alternative options. I reblog the article here with due respect for the authors, Jen Teilans, copyright....

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 toraspberrypi (or the IP address) on port 22 with username pi and default password raspberry.
step3_sshclient
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.)
step3_config
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 optionInternationalization 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.
Additionally we need to do the same for the XTERM package, so type sudo apt-get install xterm and confirm again. 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.
step5_client
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 toraspberrypi: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 typeecho “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.
Cool, now we are finally ready to install Mono: enter sudo apt-get install mono-complete. When prompted hitenter 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 typesudo apt-get install monodevelop. Again when prompted hit enter or Y and wait for the install to complete.

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 FileNewSolution 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?
step7_hello

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.
step8_hello
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.

Check back to the original post for update information and comments.

No comments: