Book Image

Raspberry Pi for Python Programmers Cookbook - Second Edition

Book Image

Raspberry Pi for Python Programmers Cookbook - Second Edition

Overview of this book

Raspberry Pi cookbook for Python Programmers is a practical guide for getting the most out of this little computer. This book begins by guiding you through setting up the Raspberry Pi, performing tasks using Python 3 and introduces the first steps to interface with electronics. As you work through each chapter you will build up your skills and knowledge and apply them as you progress throughout the book, delving further and further into the unique abilities and features of the Raspberry Pi. Later, you will learn how to automate tasks by accessing files, build applications using the popular Tkinter library and create games by controlling graphics on screen. You will harness the power of the built-in graphics processor by using Pi3D to generate your own high quality 3D graphics and environments. Connect directly to the Raspberry Pi’s hardware pins to control electronics from switching on LEDs and responding to push buttons right through to driving motors and servos. Learn how to monitor sensors to gather real life data and to use it to control other devices, and view the results over the Internet. Apply what you have learnt by creating your own Pi-Rover or Pi-Hexipod robots. Finally, we will explore using many of the purpose built add-ons available for the Raspberry Pi, as well as interfacing with common household devices in new ways.
Table of Contents (18 chapters)
Raspberry Pi for Python Programmers Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Hardware and Software List
Index

Connecting remotely to the Raspberry Pi over the network using VNC


Often, it is preferable to remotely connect to and control the Raspberry Pi across the network, for instance, using a laptop or desktop computer as a screen and keyboard, or while the Raspberry Pi is connected elsewhere, perhaps even connected to some hardware it needs to be close to.

VNC is just one way in which you can remotely connect to the Raspberry Pi. It will create a new desktop session that will be controlled and accessed remotely. The VNC session here is separate from the one that may be active on the Raspberry Pi's display.

Getting ready

Ensure that your Raspberry Pi is powered up and connected to the Internet. We will use the Internet connection to install a program using apt-get. This is a program that allows us to find and install applications directly from the official repositories.

How to do it…

First, we need to install the TightVNC server on the Raspberry Pi with the following commands. It is advisable to run an update command first to get the latest version of the package you want to install as follows:

sudo apt-get update
sudo apt-get install tightvncserver

Accept the prompt to install and wait until it completes. To start a session, use the following command to start a session:

vncserver :1

The first time you run this, it will ask you to enter a password (of no more than eight characters) to access the desktop (you will use this when you connect from your computer).

The following message should confirm that a new desktop session has been started:

New 'X' desktop is raspberrypi:1

If you do not already know the IP address of the Raspberry Pi, use hostname –I and take note of it.

Next, we need to run a VNC client, VNC Viewer is suitable program, which is available at http://www.realvnc.com/ and should work on Windows, Linux, and OS X.

When you run VNC Viewer, you will be prompted for the Server address and Encryption type. Use the IP address of your Raspberry Pi with :1. That is, for the IP address 192.168.1.69, use the 192.168.1.69:1 address.

You can leave the Encryption type as Off or Automatic.

Depending on your network, you may be able to use the hostname; the default is raspberrypi, that is raspberrypi:1.

You may have a warning about not having connected to the computer before or having no encryption. You should enable encryption if you are using a public network or if you are performing connections over the Internet (to stop others from being able to intercept your data).

There's more…

You can add options to the command line to specify the resolution and also the color depth of the display. The higher the resolution and color depth (can be adjusted to use 8 to 32 bits per pixel to provide low or high color detail), the more data has to be transferred through the network link. If you find the refresh rate a little slow, try reducing these numbers as follows:

vncserver :1 –geometry 1280x780 –depth 24

To allow the VNC server to start automatically when you switch on, you can add the vncserver command to .bash_profile (this is executed each time the Raspberry Pi starts).

Use the nano editor as follows (the -c option allows the line numbers to be displayed):

sudo nano -c ~/.bash_profile

Add the following line to the end of the file:

vncserver :1

The next time you power up, you should be able to remotely connect using VNC from another computer.