© Warren Gay 2018
Warren GayAdvanced Raspberry Pihttps://doi.org/10.1007/978-1-4842-3948-3_2

2. Preparation

Warren Gay1 
(1)
St. Catharine’s, Ontario, Canada
 

While it is assumed that you’ve already started with the Raspberry Pi, there may be a few more things that you want to do before working through the rest of this book. For example, if you normally use a laptop or desktop computer, you may prefer to access your Pi from there.

If you plan to do most or all of the projects in this book, I highly recommend using something like the Adafruit Pi T-Cobbler (covered later in this chapter). This hardware breaks out the GPIO lines in a way that you can access on a breadboard.

Static IP Address

The standard Raspbian image provides a capable Linux system, which when plugged into a network, uses DHCP (dynamic host configuration protocol) to automatically assign an IP address to it. If you’d like to connect to it remotely from a desktop or laptop, then the dynamic IP address that DHCP assigns is problematic.

There are downloadable Windows programs for scanning the network. If you are using a Linux or Mac host, you can use nmap to scan for it. The following is an example session from a Devuan Linux, using the nmap command. Here a range of IP addresses are scanned from 1–250:
root@devuan:~# nmap -sP 192.168.1.1-250
Starting Nmap 6.47 ( http://nmap.org ) at 2018-06-01 19:59 EDT
Nmap scan report for 192.168.1.1
Host is up (0.00026s latency).
MAC Address: C0:FF:D4:95:80:04 (Unknown)
Nmap scan report for 192.168.1.2
Host is up (0.044s latency).
MAC Address: 00:1B:A9:BD:79:02 (Brother Industries)
Nmap scan report for 192.168.1.77
Host is up (0.15s latency).
MAC Address: B8:27:EB:ED:48:B1 (Raspberry Pi Foundation)
Nmap scan report for 192.168.1.121
Host is up (0.00027s latency).
MAC Address: 40:6C:8F:11:B8:AE (Apple)
Nmap scan report for 192.168.1.80
Host is up.
Nmap done: 250 IP addresses (4 hosts up) scanned in 7.54 seconds
root@devuan:~#

In this example, the Raspberry Pi is identified on 192.168.1.77, complete with its MAC address (these appear above the line where “Raspberry Pi Foundation” is reported). While this discovery approach works, it does takes time and is inconvenient.

If you’d prefer to change your Raspberry Pi to use a static IP address, see the “Wired Ethernet” section in Chapter 8 for instructions.

Using SSH

If you know the IP address of your Raspberry Pi, discovered it with nmap, or have the name registered in your hosts file, you can log into it using SSH. In this example, we log in as user pi on a host 192.168.1.77 from a Devuan Linux box:
[email protected]'s password:
Linux raspberrypi 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
Last login: Fri Jun  1 20:07:24 2018 from 192.168.1.80
$

Files can also be copied to and from the Raspberry Pi, using the scp command. Do a man scp on the Raspberry Pi for usage information.

It is also possible to display X Window System (X-Window) graphics on your laptop/desktop, if there is an X-Window server running on it. (Windows users can use Cygwin for this, available from www.cygwin.com.) Using Linux as an example, first configure the security of your X-Window server to allow requests. Here I’ll take the lazy approach of allowing all hosts by using the xhost command (on a Linux box that is not a Pi, or is another Pi):
$ xhost +
access control disabled, clients can connect from any host
$
Now log into the remote Pi using ssh with the -Y option:
[email protected]'s password:
Warning: No xauth data; using fake authentication data for X11 forwarding.
Linux raspberrypi 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
Last login: Fri Jun  1 20:14:40 2018 from 192.168.1.80
$
From the Raspberry Pi session, we can launch xpdf so that it opens a window on the local Linux box:
$ xpdf &
If that fails, try exporting a DISPLAY variable on the remote (pi) to inform the software where the X-Window server and screen exist:
$ export DISPLAY=192.168.1.80:0

Here, I’ve specified the Devuan Linux address (alternatively, an /etc/hosts name could be used) and pointed the Raspberry Pi to use Linux’s display number :0. We run the xpdf command in the background so that we can continue to issue commands in the current SSH session. In the meantime, the xpdf window will open on the Linux screen, while the xpdf program runs on the Raspberry Pi.

This doesn’t give you graphical access to the Pi’s desktop, but for developers, SSH is often adequate. If you want remote graphical access to the Raspberry’s desktop, one option is to use VNC.

VNC

If you’re already using a laptop or your favorite desktop computer, you can conveniently access your Raspberry Pi’s graphical desktop over the network. Once the Raspberry Pi’s VNC server is configured, all you need is a VNC client on your accessing computer. This eliminates the need for a keyboard, mouse, and HDMI display device connected to the Raspberry Pi. In other words, you can run the Pi “headless.”

Getting VNC working requires a bit of setup. Raspbian Linux has taken measures to make it easy. If not set up correctly, the VNC viewer will just provide a black screen when you try to log in.

To use VNC, you must have the desktop software installed (GUI). This will also make it easier for you to get it configured. If you have a Raspbian Lite distribution installed, it will not include the necessary desktop server software.

Start up the graphical desktop, then from the Raspberry icon (at top left), pull down the menu and select “Preferences,” and choose “Raspberry Pi Configuration.” That should bring up a dialog box like the one in shown in Figure 2-1.
../images/326071_2_En_2_Chapter/326071_2_En_2_Fig1_HTML.jpg
Figure 2-1

Raspberry Pi Configuration dialog. Note how the Boot: selection has the radio button “To Desktop” checked.

You may already have the “Boot” option set to “To Desktop” but otherwise click that now. This will cause the desktop software to start after a boot so that you can connect to it through VNC.

After you have configured the desktop to start after a reboot, you also need to enable the VNC server as shown in Figure 2-2, by clicking on the “Interfaces” tab.
../images/326071_2_En_2_Chapter/326071_2_En_2_Fig2_HTML.jpg
Figure 2-2

VNC is enabled in the Interfaces tab of the dialog box

In the Interfaces dialog, click the VNC radio button labeled “Enable.” Click OK at the bottom right to save your settings. Then reboot your Pi. Allow time for a reboot and for the graphic desktop to start.

VNC Viewers

To access the VNC server, a corresponding VNC viewer is needed on the client side. One solution is to use the free realvnc viewer from:

https://www.realvnc.com/en/connect/download/viewer/

From that website, you’ll find download links for your favorite desktop, as depicted in Figure 2-3. Ignore the site’s reference to “VNC Connect.”
../images/326071_2_En_2_Chapter/326071_2_En_2_Fig3_HTML.jpg
Figure 2-3

The various download options for the VNC Viewer. Disregard the “VNC Connect” message on this page.

Download and install as appropriate for your desktop platform.

When you start the viewer, you will get a small dialog box similar to the one shown in Figure 2-4. Icons will exist (like the one shown) once you have successfully logged in.
../images/326071_2_En_2_Chapter/326071_2_En_2_Fig4_HTML.jpg
Figure 2-4

The inital VNC Viewer dialog and one previously used icon for login

Once you have successfully connected and logged in, you should have your Pi desktop displayed for you. Figure 2-5 illustrates a desktop window (on a Mac).
../images/326071_2_En_2_Chapter/326071_2_En_2_Fig5_HTML.jpg
Figure 2-5

A connected Raspberry Pi VNC session on a Mac

If your VNC viewer connects and seems to hang, be patient. The rate of your VNC screen updates will depend upon the ability of your network to transfer that graphic data. I found that I was able to use VNC with the Pi 3 B+ using a WIFI connection, without too much delay.

While the VNC facility is great for providing remote graphical desktop access, it probably goes without saying that the performance won’t be suitable for viewing video or fast action games.

Black VNC Screen

If you change the configuration of the Pi to boot to command-line mode (see Figure 2-1 button “To CLI”) instead of the desktop, you will experience a black screen when you use VNC Viewer (see Figure 2-6). Selecting command-line mode causes the desktop software not to run, even though you may have VNC enabled (Figure 2-2 “Enable”).
../images/326071_2_En_2_Chapter/326071_2_En_2_Fig6_HTML.jpg
Figure 2-6

A black VNC Viewer screen indicates that the desktop software is not running

To regain VNC desktop access, you must change the configuration to “To Desktop” (Figure 2-1), and reboot. The alternative is to go to the command line (console or ssh) and start the X server manually. From the console login, simply do:
$ startx
From an ssh session, you'll need to be root (using sudo):
$ sudo -i
root@pi3bplus:~# startx

After giving the server software time to start up, the VNC Viewer should be able to attach to your desktop. Of course, if you log that ssh session out, the server will also exit and terminate desktop access.

Breadboard Setup

It is possible to work without a breadboard and Pi T-Cobbler adapter, but you will find that experiments are much more fun with a quality breadboard and adapter. A recommended T-Cobbler can be purchased from Adafruit ® and is illustrated in Figure 2-7 alongside a cheap copy.

https://www.adafruit.com/product/2028
../images/326071_2_En_2_Chapter/326071_2_En_2_Fig7_HTML.jpg
Figure 2-7

Two Pi T-Cobbler breadboard adapters. Notice how the unit from China (left) requires a cable twist. The Adafruit unit (right) is recommended.

Summary

With these details out of the way, the remainder of the book can focus on the various resources that the Raspberry Pi has to offer. The Pi has much to offer, so let’s get started.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.138.230