Book Image

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

By : Steven Lawrence Fernandes, Tim Cox
Book Image

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

By: Steven Lawrence Fernandes, Tim Cox

Overview of this book

Raspberry Pi 3 Cookbook for Python Programmers – Third Edition begins by guiding you through setting up Raspberry Pi 3, performing tasks using Python 3.6, and introducing the first steps to interface with electronics. As you work through each chapter, you will build your skills and apply them as you progress. You will learn how to build text classifiers, predict sentiments in words, develop applications using the popular Tkinter library, and create games by controlling graphics on your screen. You will harness the power of a built in graphics processor using Pi3D to generate your own high-quality 3D graphics and environments. You will understand how to connect Raspberry Pi’s hardware pins directly to control electronics, from switching on LEDs and responding to push buttons to driving motors and servos. Get to grips with monitoring sensors to gather real-life data, using it to control other devices, and viewing the results over the internet. You will apply what you have learned by creating your own Pi-Rover or Pi-Hexipod robots. You will also learn about sentiment analysis, face recognition techniques, and building neural network modules for optical character recognition. Finally, you will learn to build movie recommendations system on Raspberry Pi 3.
Table of Contents (23 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

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 xmingfrom 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 Configurationdialog box, navigate to Connection | SSH | X11 and tick the checkbox for EnableX11 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/