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!

Saturday, August 17, 2013

Deluxe Raspberry Pi




In this post I'll give you some quick tips and tricks that make Wheezy even better.

In my last post I walked through installing and configuring Raspbian Wheezy. Now that you're up and running, I'd like to show you some cool tricks and provide a couple of scripts to make Wheezy even more useful.

Silly Putty
If you read my previous post you might recall that I recommended enabling an ssh connection to your Pi using the configuration menu. If you did this, now might be a good time to install an ssh client so you can remotely connect to your Pi from another PC. This is also quite useful if you'd like to run your Pi without the need to view it's display. For example, I'm writing this post on a laptop, while my Pi is running in another room. But, that doesn't prevent me from accessing it. I'm simply using the free Putty ssh client for Windows to remotely connect to my Pi and run all of the terminal commands. Setting up Putty is straightforward, you simply need to find your Pi's address to connect, which you can do either from your router's admin page or by running the ifconfig command in the Pi's terminal window and getting the IP address in the inet addr line. Next you'll want to enter this IP address in the Host Name (or IP address) field, as shown below. Press Open and you should see a terminal window where you can enter the user id pi and the password you setup previously during configuration.





Hot Off The Press
Now that you can connect to the Pi (both remotely or directly) it's a good time to make sure the software and firmware are up to date. You can update the Wheezy distribution, along with associated Pi utilities by running the following command in the terminal window. 

      sudo apt-get update && sudo apt-get install raspberrypi* raspi-config

Enter Y when you're prompted to continue. This process will take several minutes or more to complete, depending on the number of updates downloaded.

Next you can update the Pi's firmware by installing a very handy utility called rpi-update. Type the following terminal commands to install the utility and run it:

     sudo apt-get install rpi-update
     sudo rpi-update
     sudo reboot     (if all looks well)

Share And Share Alike
My home setup has a shared network (NAS) drive. I thought it would be handy to connect the Pi to this drive so I could display photos, videos and exchange files between other networked PCs. If you'd like to do something similar you can do so by adding an entry to the fstab configuration file to mount the drive and make it visible to the Pi's file system. The steps below describe how to do this, where the example IP address of the drive is 192.168.1.100 and default directory is /Public. Also, this example mounts the drive at the /mnt/n mount point in the filesystem. Obviously you can use any drive name or letter in place of n, but you should place this under the standard /mnt directory.

Enter the commands below in a terminal window:
      
      sudo mkdir /mnt/n
      sudo nano /etc/fstab 

Add something similar to this line in fstab, where the IP address and default directory refer to your network, or shared drive:  //192.168.1.100/Public /mnt/n cifs auto,password= 0 0

Press Ctrl-o to save the file, then Ctrl-x to exit
Enter this command: sudo reboot

Once you're rebooted enter the df command in a terminal window and you should see output similar to this:

     //192.168.1.100/Public 968428672 290909184 677519488  31% /mnt/n

Information, Please
Perhaps you've wanted to find out information about your Pi's configuration, such as the current firmware version, speed settings, temperature and so forth. Below I've provided a handy script sysinfo that displays all of this useful information. 

In a terminal window enter this command: sudo nano

Then enter each of the following lines:


#!/bin/bash
echo === Current freq / max / min ===
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq
echo
echo === Temp ===
vcgencmd measure_temp
echo
echo === Version ===
vcgencmd version
echo
echo === CPU Info ===
cat /proc/cpuinfo

Once you've entered the lines press Ctrl-o  and enter the name /usr/bin/sysinfo, then press Ctrl-x to exit
In the terminal window enter: sudo chmod 777 /usr/bin/sysinfo
Now you can run the command sysinfo at the terminal window any time you'd like and you'll see output similar to the following:

=== Current freq / max / min ===
800000
900000
700000

=== Temp ===
temp=47.6'C

=== Version ===
Feb  7 2013 16:46:17
Copyright (c) 2012 Broadcom
version 367974 (release)

=== CPU Info ===
Processor       : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS        : 464.48
Features        : swp half thumb fastmult vfp edsp java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xb76
CPU revision    : 7

Hardware        : BCM2708
Revision        : 000f
Serial          : 00000000xxxxxxxx

Just Chilling
If you'd just like to quickly check the system temperature you can do so using the temp script I've provided below.

In a terminal window enter: sudo nano

Then add the following lines:


#!/bin/bash
/opt/vc/bin/vcgencmd measure_temp

Then press Ctrl-o and name the file /usr/bin/temp, then press Ctrl-x to exit
Then enter the following command: sudo chmod 777 /usr/bin/temp

Now you can simply run the command temp in a terminal window and see the current system temperature as below:

temp=47.6'C

Install Some Paneling
Maybe you'd prefer to see the system temperature all of the time, rather than simply when you run a command. This is especially useful if you're overclocking and using the Pi to do processor intensive activities. Fortunately, this is easy to do. You simply need to add the Temperature Monitor to the status panel at the bottom of the screen. You can do this by right-clicking on an empty area of the status bar. This brings up a menu where you can choose Add/Remove Panel Items. Next, choose the Panel Applets tab and press the Add button. Scroll through the list and find the Temperature Monitor applet then press Add. You can also set the location of the temperature indicator by using the Up and Down buttons. I'd recommend pressing the Up button at least once so the temperature display is to the left of the Shutdown button. You should see something similar to this status bar below:



Time At Last
You may have noticed the clock in my status bar screen shot above shows 12-hour time. If you're like most of us, you're probably used to saying it's 2PM rather for 1400 hours. But, whatever floats your boat. If you fall into the former category then you might find this tip very useful. The panel's clock applet displays the current time using the strftime format string. You can look up the details if you'd like, but here's a handy format string that changes the time and date to a standard US format. Just right-click on the clock in the panel and choose Digital Clock Settings. Enter %l:%M%P (note the first sequence is %lower-case L, case matters) in Clock Format and %D in Tooltip Format (mm/dd/yy). You might also want to select Bold checkbox. Press Close and presto! you can now tell night from day.


In my next post I'll talk about how to remotely connect to the Pi's graphical desktop using VNC. Until then, happy baking!

Monday, August 12, 2013

Cooking Raspberry Pi

In this post I discuss installing and configuring the Raspbian "Wheezy" distribution.

My previous posts discussed the hardware you need to get your Pi setup. Now it's time to focus on installing an operating system on our Pi and configuring it. First, we need to choose which operating system we want to install. As of this writing there are several options, but I'm going to focus on the Raspbian "Wheezy" distribution. Rapsbian is a Debian-based linux distribution that happens to be the most popular OS choice for the Pi.

Pick An OS, Any OS
Installing Raspbian is a two-step process. First, you need to select the appropriate version to download. You can find the link to download Raspbian Wheezy here. On this page you'll find multiple ways to download the distribution. The simplest is to download the NOOBS.zip. You can follow the directions for doing so on the link provided. In this post I'll focus on the second option of installing the raw normal Wheezy image. Note that you'll also see references to a soft-float version of Wheezy. You don't need to be concerned with this unless you plan to use Java with the Pi. In that case you'll need to install the soft-float Wheezy version because it plays nicely with Oracle's Java JVM. I don't recommend using this version otherwise, since its performance isn't as good as the normal Wheezy version.

The Write Stuff
Once you've download and unzipped the distribution you'll need need to use a special tool to transfer the img file to an SD card. Once such tool for Windows is Win32DiskImager . In this post I'll focus on how to create an SD card using Windows. If you're working with a Mac you can find similar instructions for creating SD card images using the Mac's dd Unix command by searching the Internet. The basic concepts are the same, you start by downloading an image file, then use a low-level utility to write the bootable operating system image to an SD card. 

The first step is to start Win32DiskImager. Once the screen appears, you'll see something similar to the image below. Click the Folder icon next to the Image File field and select the .img file you downloaded and unzipped previously. Next you'll want to select the SD card in the Device dropdown. Make sure you select the correct SD card before you begin writing data because this process erases all data from the selected card. Once you're satisfied with your selection, press the Write button to begin preparing the card. This process may take 30 minutes or more, depending on the speed of the SD card you're using and the transfer speed of the card writer.



Give It The Boot
Once card creation is complete you should have a working Wheezy SD that you can place into the Pi and boot with. As the Pi boots you'll see 100's of lines quickly scroll by. These are displayed as part of the normal Linux kernel startup. After a minute or so you should arrive at the Raspi-config screen shown below. The remainder of this post I will walk you through each of these menu options. 



The first option we'll look at is expand_rootfs . As you can probably guess by the name, this is used to expand the root file system. This is very important to do because if you don't do so your Pi will only have disk space equivalent to the Wheezy image you wrote to the card, rather than the entire SD card size. For example, if you install a 2GB Wheezy image on an 8GB SD card the root file system will only be 2GB. You essentially have 6GB of space that isn't available for use since it isn't allocated in the filesystem. So, go ahead and choose this option. Once you do so you'll see a few dozen lines flash by on the console. These are telling us that the partition manager is resizing the root file system. After completion you'll see a screen stating that the resizing was completed and will take effect on the next reboot. Don't worry about rebooting now. We'll finish with the other options and do the reboot last.

The next option we'll look at is overscan. If you're working with an HDMI monitor or TV you can use this option to control the appearance of the screen. You'll want to play around with Enable / Disable  until you find the option that properly shows all of the example content. For most displays Disable is generally the preferred setting.

Next you'll want to use configure_keyboard to select the proper keyboard you're using. Since the Pi is manufactured in the UK the default keyboard selection is an International version (typically the Generic 105-key (Intl) PC). If you're a US user you'll probably want to change this to the Generic 104-key PC keyboard then select English (US). You can take the default for the rest of the options that appear. This seems a bit arcane, but if you don't follow this pattern you'll find some very needed symbols such as the @ symbol won't be mapped properly when you press the key.

You can use change_pass to change the default password for the user pi. I highly recommend doing so, since it provides a measure of security if you're planning to use your Pi to store documents and so forth or to connect remotely using ssh.

Where Am I?
The change_locale options lets you set a default language. You'll only need to change this setting if you don't wish to select English as the default language. Next you can select your current timezone using the change_timezone option. Note that the Pi doesn't have a realtime clock, but the timezone is used to offset the time obtained by Internet-based time services that provide the current time/date in universal time. Users in the US will want to choose the US menu option, then select the appropriate timezone.

The memory_split option allows you to select the amount of memory you wish to allocate for video memory vs. core memory. If you're planning to  to play videos you'll want to allocate at least 128MB for GPU (video) memory. This is typically the fourth option in the menu and begins with 128. The overclock option lets you overclock the Pi's processor, in a somewhat safe fashion. I say somewhat safe because while it's designed to protect the Pi's processor from being pushed too hard, unfortunately it does little to prevent the SD card from being corrupted if the Pi becomes unstable and crashes from overclocking. I found that pushing the overclock above Medium (900MHz) resulted in corrupted SD cards quite often. When this happens you pretty much have to reinstall Wheezy from scratch. All of your configuration changes are lost. Your overclocking mileage may vary. 

The ssh option allows you to setup the Pi for remote access using ssh. I recommend enabling this option to allow an easy way to control your Pi from other computers on your network. In future posts I'll discuss how to use ssh, along with more advanced ways to access the Pi using a VNC client. The start_desktop option lets you control whether the graphical desktop is automatically started when the Pi boots. In most cases you'll probably want this to be the default option. The exception is cases where you use the Pi as a dedicated server that doesn't need a graphical interface.

Almost There
Last, but not least, the update menu option checks for updated versions of the configuration menu (raspi-config) that we've just walked though. It's probably a good idea to do this once in a while. You can do it a couple of ways, first by running the configuration menu again by entering the following command in the terminal window: sudo raspi-config. The second way is by running the following terminal command: sudo apt-get update,  followed by the command: sudo apt-get install raspi-config.

The final step in completing the install/configuration process is to reboot the Pi. You should exit the menu by tabbing to the Finish button and pressing Enter. This takes you to a command prompt where you can type the command: sudo reboot. This restarts the Pi and all of the options you've selected will now take effect.

In this post I went through the most common options for configuring the Pi for first-time use. You can find more detailed instructions regarding each configuration option by referring to the Raspi-config page on the elinux.org site.

In my next post I'll provide some handy scripts and tips to help you find your way around Wheezy. Until then, happy baking!