Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi for  Python Programmers  Cookbook

You're reading from   Raspberry Pi for Python Programmers Cookbook Over 60 recipes that harness the power of the Raspberry Pi together with Python programming and create enthralling and captivating projects

Arrow left icon
Product type Paperback
Published in Oct 2016
Publisher
ISBN-13 9781785288326
Length 510 pages
Edition 2nd Edition
Languages
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with a Raspberry Pi Computer FREE CHAPTER 2. Starting with Python Strings, Files, and Menus 3. Using Python for Automation and Productivity 4. Creating Games and Graphics 5. Creating 3D Graphics 6. Using Python to Drive Hardware 7. Sense and Display Real-World Data 8. Creating Projects with the Raspberry Pi Camera Module 9. Building Robots 10. Interfacing with Technology A. Hardware and Software List
Index

Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding)

An SSH (Secure SHell) is often the preferred method for making remote connections, as it allows only the terminal connections and typically requires fewer resources.

An extra feature of SSH is the ability to transfer the X11 data to an X Windows server running on your machine. This allows you to start programs that would normally run on the Raspberry Pi desktop, and they will appear in their own Windows on the local computer, as follows:

Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding)

X11 Forwarding on a local display

X Forwarding can be used to display applications, which are running on the Raspberry Pi, on a Windows computer.

Getting ready

If you are running the latest version of Raspbian, SSH and X11 Forwarding will be enabled by default (otherwise, double-check the settings explained in the How it works… section).

How to do it…

Linux and OS X have built-in support for X11 Forwarding; but if you are using Windows, you will need to install and run the X Windows server on your computer.

Download and run xming from the Xming site (http://sourceforge.net/projects/xming/).

Install xming, following the installation steps, including the installation of PuTTY if you don't have it already. You can also download PuTTY separately from http://www.putty.org/.

Next, we need to ensure that the SSH program we use has X11 enabled when we connect.

For Windows, we shall use PuTTY to connect to the Raspberry Pi.

In the PuTTY Configuration dialog box, navigate to Connection | SSH | X11 and tick the checkbox for X11 Forwarding. If you leave the X display location option blank, it will assume the default Server 0:0 as follows (you can confirm the server number by moving your mouse over the Xming icon in the system tray when it is running):

How to do it…

Enabling X11 Forwarding within the PuTTY configuration

Enter the IP address of the Raspberry Pi in the Session settings (you may also find that you can use the Raspberry Pi's hostname here instead; the default hostname is raspberrypi).

Save the setting using a suitable name, RaspberryPi, and click on Open to connect to your Raspberry Pi.

You are likely to see a warning message pop up stating you haven't connected to the computer before (this allows you to check whether you have everything right before continuing).

How to do it…

Opening an SSH connection to the Raspberry Pi using PuTTY

For OS X or Linux, click on Terminal to open a connection to the Raspberry Pi.

To connect with the default pi username, with an IP address of 192.168.1.69, use the following command; the -X option enables X11 Forwarding:

ssh -X pi@192.168.1.69

All being well, you should be greeted with a prompt for your password (remember the default value for the pi user is raspberry).

Ensure that you have Xming running by starting the Xming program from your computer's Start menu. Then, in the terminal window, type a program that normally runs within the Raspberry Pi desktop, such as leafpad or scratch. Wait a little while and the program should appear on your computer's desktop (if you get an error, you have probably forgotten to start Xming, so run it and try again).

How it works…

X Windows and X11 is what provides the method by which the Raspberry Pi (and many other Linux-based computers) can display and control graphical Windows as part of a desktop.

For X11 Forwarding to work over a network connection, we need both SSH and X11 Forwarding enabled on the Raspberry Pi. Perform the following steps:

  1. To switch on (or off) SSH, you can access the Raspberry Pi Configuration program under the Preferences menu on the Desktop and click on SSH within the Interfaces tab, as shown in the following screenshot (SSH is often enabled by default for most distributions to help allow remote connections without needing a monitor to configure it):
    How it works…

    The Interfaces tab in the Raspberry Pi Configuration tool

  2. Ensure that X11 Forwarding is enabled on the Raspberry Pi (again most distributions now have this enabled by default).
  3. Use nano with the following command:
    sudo nano /etc/ssh/sshd_config
    
  4. Look for a line in the /etc/ssh/sshd_config file that controls X11 Forwarding and ensure that it says yes (with no # sign before it), as follows:
    X11Forwarding yes
    
  5. Save if required by pressing Ctrl + X, Y, and Enter and reboot (if you need to change it) as follows:
    sudo reboot
    

There's more…

SSH and X 11 Forwarding is a convenient way to control the Raspberry Pi remotely; we will explore some additional tips on how to use it effectively in the following sections.

Running multiple programs with X11 Forwarding

If you want to run an X program, but still be able to use the same terminal console for other stuff, you can run the command in the background with & as follows:

leafpad &

Just remember that the more programs you run, the slower everything will get. You can switch to the background program by typing fg and check for background tasks with bg.

Running as a desktop with X11 Forwarding

You can even run a complete desktop session through X11, although it isn't particularly user-friendly and VNC will produce better results. To achieve this, you have to use lxsession instead of startx (in the way you would normally start the desktop from the terminal).

An alternative is to use lxpanel, which provides the program menu bar from which you can start and run programs from the menu as you would on the desktop.

Running PyGame and Tkinter with X11 Forwarding

You can get the following error (or similar) when running the PyGame or Tkinter scripts:

_tkinter.TclError: couldn't connect to display "localhost:10.0"

In this case, use the following command to fix the error:

sudo cp ~/.Xauthority ~root/
You have been reading a chapter from
Raspberry Pi for Python Programmers Cookbook - Second Edition
Published in: Oct 2016
Publisher:
ISBN-13: 9781785288326
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image