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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi 3 Cookbook for Python Programmers

You're reading from   Raspberry Pi 3 Cookbook for Python Programmers Unleash the potential of Raspberry Pi 3 with over 100 recipes

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher
ISBN-13 9781788629874
Length 552 pages
Edition 3rd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Steven Lawrence Fernandes Steven Lawrence Fernandes
Author Profile Icon Steven Lawrence Fernandes
Steven Lawrence Fernandes
Tim Cox Tim Cox
Author Profile Icon Tim Cox
Tim Cox
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting Started with a Raspberry Pi 3 Computer FREE CHAPTER 2. Dividing Text Data and Building Text Classifiers 3. Using Python for Automation and Productivity 4. Predicting Sentiments in Words 5. Creating Games and Graphics 6. Detecting Edges and Contours in Images 7. Creating 3D Graphics 8. Building Face Detector and Face Recognition Applications 9. Using Python to Drive Hardware 10. Sensing and Displaying Real-World Data 11. Building Neural Network Modules for Optical Character Recognition 12. Building Robots 13. Interfacing with Technology 14. Can I Recommend a Movie for You? 15. Hardware and Software List 16. Other Books You May Enjoy

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

An Secure Shell (SSH) 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 Raspberry Pi desktop, and they will appear in their own Windows on the local computer, as follows:

X11 forwarding on a local display

X11 forwarding can be used to display applications which are running on 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 Raspberry Pi.

In the PuTTY Configuration dialog box, navigate to Connection | SSH | X11 and tick the checkbox for Enable 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):

Enabling X11 forwarding within the PuTTY configuration

Enter the IP address of Raspberry Pi in the Session settings (you may also find that you can use 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):

Opening an SSH connection to Raspberry Pi using PuTTY

For OS X or Linux, click on Terminal to open a connection to 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 [email protected]  

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 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 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 Raspberry Pi. Perform the following steps:

  1. To switch on (or off) SSH, you can access 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):
The advanced settings menu in the raspi-config tool
  1. Ensure that X11 forwarding is enabled on Raspberry Pi (again, most distributions now have this enabled by default).
  2. Use nano with the following command:
sudo nano /etc/ssh/sshd_config  
  1. 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  
  1. 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 X11 forwarding is a convenient way to control 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 3 Cookbook for Python Programmers - Third Edition
Published in: Apr 2018
Publisher:
ISBN-13: 9781788629874
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