Tuesday, August 27, 2013

Beautiful Raspberry Pi


In this post I'll  show you how to remotely connect to your Pi using VNC.

Previously I showed you how to setup an ssh connection to your Pi using Putty. Putty/ssh is a great combination if you want to run terminal commands on your Pi. But, if you're running a graphical desktop you might also want a client that allows you to access the Pi's graphical screen. In the Linux world this type of connection is referred to as a VNC (Virtual Network Computing) connection. At a high level VNC specifies a protocol for transferring screen information and events between a VNC Server and Client. If you're interested in the technical details you might want to look at the Wikipedia overview here. For our purposes, I'll walk you through installing a VNC server on the Pi, then we'll walk through installing a VNC client under Windows and Mac.

Ready To Serve
The first part of setting up VNC for the Pi involves installing server software to allow the Pi to serve up a copy of its graphical desktop. The most popular VNC server for the Pi is TightVNC. It is free for non-commercial (i.e. standalone) use. Please refer to the web site if you have questions about usage. For most of us the free GNU license version is applicable, so we'll look at how to set it up on the Pi.

The first thing we need to do is download and install the server. You can do this by opening a terminal window and entering the following commands:

     sudo apt-get update
     sudo apt-get install tightvncserver

Once you've done this the necessary packages will be downloaded to install the TightVNC server. The next step is to start the server by typing the following command in the terminal window:

      vncserver :1 

This command starts the VNC server and gives the screen a default resolution, in the case of TightVNC the default is usually 1024x768. You can specify your own settings if you wish by adding the geometry parameter as shown below. You can also specify color depth as also shown below.

      vncserver :1 -geometry 1024x800 -depth 24

Unfortunately, if you wish to change these settings again you'll need to reboot (sudo reboot) and enter the command again. There doesn't appear to be and easy way to restart the TightVNC server without restarting Raspbian. For further details regarding command-line options please refer to the TightVNC link I provided previously.

Start Me Up
You'll need to run the vncserver command above each time you start Raspbian, unless you specify a way for this command to automatically run during startup. Fortunately, this is fairly easy to do if you've chosen the configuration option to automatically start the desktop when you startup Raspbian (please refer to my previous post about configuring Raspbian if you have any questions). You can start by opening a terminal window and entering the following commands:

     cd /home/pi/.config
     sudo mkdir autostart
     cd autostart
     sudo nano tightvnc.autostart
     
Add the following lines:
     
     [Desktop Entry]
      Type=Application
      Name=TightVNC
      Exec=vncserver :1
      StartupNotify=false

Then press Ctrl-O write the file, then Ctrl-X to exit. Now when you restart the Pi (sudo reboot) the VNC server should automatically start. 

It's Not Our Policy
You may get the error below when you restart the Pi after entering the VNC startup information. If you do you can resolve this by going to start menu, choosing Preferences  then Desktop Session Settings and unchecking the LXPolKit checkbox. You'll need to restart Raspbian once you do this for it to take effect. If all goes well the error should not appear again once you reboot.


It's Good To Have Connections
Now that we have a VNC server running it's time to install a VNC client so we can connect to the Pi from another computer. There are multiple ways to do this, depending on the operating system you're using. For Windows you can install RealVNC Viewer, which has a 32-bit and 64-bit Windows client.  Another option is TightVNC, which also has a Windows client. In this post I'll focus on setting up RealVNC, which is my preference of the the two because it lets you setup resizeable, scalable windows. For those using a MAC I'll also show you how to use the Mac's built-in VNC client.

Once you download and install VNC Viewer you're presented with a connection screen, where you'll enter the Pi's IP address, along with the port corresponding to the virtual screen you wish to display. For example, you might enter 192.168.1.101:5901, where 5901 is the VNC port used for the first screen (vncserver :1). Once you've specified the appropriate connection information press the Connect button. If all goes well, you should see your Pi's desktop, as shown in the screen shot I've provided below:



Note that by default RealVNC's window uses scrollbars when you resize it, but you can change it to a scalable window by choosing Options from the toolbar at the top of the RealVNC window, then checking the Scale to window size option.

Macs-imize
If you're using a Mac you can also download and configure RealVNC Viewer, as I mentioned above. But, there's really no need to download a VNC client because it is already included with Tiger and later versions of OS X. To connect to the Pi you simply need to right-click on the Finder icon in the dock and choose Connect to Server. In the Server Address field you'll need to enter a connection string similar to the one I mentioned above, but prefixed with vnc://. For example: 

     vnc://192.168.1.101:5901

This instructs the Mac to connect to the VNC server at the specified IP address and display virtual screen 1 (port 5901). If all goes well, you should see something similar to the screen shot below:



The Standard Disclaimer
One thing to keep in mind about VNC, versus remote desktop software is that VNC interacts with a virtual screen. In other words, you get a unique copy of a desktop which you can interact with. So, for example, if you open a terminal window using VNC you'll only see the terminal window in VNC, not on the actual display hooked up to the Pi (assuming you have the Pi connected to a display, which you don't need at all with VNC! More on this below). Obviously, there are some global items that affect both the physical and virtual screens. For example, you might wish to create a new desktop folder like the Tools folder in my screen shot. Items such as a desktop folder show up on the physical display along with any virtual screens you see in VNC. That's because there is only one instance of  the desktop and start menu. Since they're shared, any changes you make to these global items are automatically reflected across all displays.

The Headless Pi
As I previously mentioned, if you're running VNC you don't need a physical display attached to your Pi, nor a keyboard our mouse for that matter! That's because VNC allows you to use the client computer's devices to send these commands to the Pi through its virtual connection. This type of setup (with no display or input devices) is referred to as a headless installation. It's particularly useful in cases where you want to use the Pi remotely or use it as a media server. For most purposes a VNC connection works well, but there are some cases where VNC won't work. These generally involve applications that directly access the Pi's hardware. One such example is the Omxplayer (here's a great overview http://elinux.org/Omxplayer), that uses the Pi's GPU to play MPEG files. You can run Omxplayer as normal from VNC, but you will only see video output on the physical screen. That's because Omxplayer  uses the Pi's GPU for hardware-accelerated graphics. So, a good rule of thumb is that VNC works with most applications.

In my next post I'll talk about using a boot loader and USB stick to quickly switch between multiple OSs. Until then, happy baking!

No comments:

Post a Comment