Book Image

Getting Started with Python for the Internet of Things

By : Tim Cox, Steven Lawrence Fernandes, Sai Yamanoor, Srihari Yamanoor, Prof. Diwakar Vaish
Book Image

Getting Started with Python for the Internet of Things

By: Tim Cox, Steven Lawrence Fernandes, Sai Yamanoor, Srihari Yamanoor, Prof. Diwakar Vaish

Overview of this book

This Learning Path takes you on a journey in the world of robotics and teaches you all that you can achieve with Raspberry Pi and Python. It teaches you to harness the power of Python with the Raspberry Pi 3 and the Raspberry Pi zero to build superlative automation systems that can transform your business. You will learn to create text classifiers, predict sentiment in words, and develop applications with the Tkinter library. Things will get more interesting when you build a human face detection and recognition system and a home automation system in Python, where different appliances are controlled using the Raspberry Pi. With such diverse robotics projects, you'll grasp the basics of robotics and its functions, and understand the integration of robotics with the IoT environment. By the end of this Learning Path, you will have covered everything from configuring a robotic controller, to creating a self-driven robotic vehicle using Python. • Raspberry Pi 3 Cookbook for Python Programmers - Third Edition by Tim Cox, Dr. Steven Lawrence Fernandes • Python Programming with Raspberry Pi by Sai Yamanoor, Srihari Yamanoor • Python Robotics Projects by Prof. Diwakar Vaish
Table of Contents (37 chapters)
Title Page
Copyright and Credits
About Packt
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/