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

Networking and connecting your Raspberry Pi to the Internet via the LAN connector


The simplest way to connect the Raspberry Pi to the Internet is by using the built-in LAN connection on the Model B. If you are using a Model A Raspberry Pi, a USB-to-LAN adapter can be used (refer to the There's more… section of the Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle recipe for details on how to configure this).

Getting ready

You will need access to a suitable wired network, which will be connected to the Internet, and a standard network cable (with an RJ45 type connector for connecting to the Raspberry Pi).

How to do it…

Many networks connect and configure themselves automatically using Dynamic Host Configuration Protocol (DHCP), which is controlled by the router or switch. If this is the case, simply plug the network cable into a spare network port on your router or network switch (or wall network socket if applicable).

Alternatively, if a DHCP server is not available, you shall have to configure the settings manually (refer to the There's more… section for details).

You can confirm this is functioning successfully with the following steps:

  1. Ensure that the two LEDs on either side of the Raspberry Pi light up (the left orange LED indicates a connection and the green LED on the right shows activity by flashing). This will indicate that there is a physical connection to the router and the equipment is powered and functioning.

  2. Test the link to your local network using the ping command. First, find out the IP address of another computer on the network (or the address of your router perhaps, often 192.168.0.1 or 192.168.1.254). Now, on the Raspberry Pi terminal, use the ping command (the -c 4 parameter is used to send just four messages; otherwise, press Ctrl + C to stop) to ping the IP address as follows:

    sudo ping 192.168.1.254 -c 4
    
  3. Test the link to the Internet (this will fail if you usually connect to the Internet through a proxy server) as follows:

    sudo ping www.raspberrypi.org -c 4
    
  4. Finally, you can test the link back to the Raspberry Pi by discovering the IP address using hostname -I on the Raspberry Pi. You can then use the ping command on another computer on the network to ensure it is accessible (using the Raspberry Pi's IP address in place of www.raspberrypi.org). The Windows version of the ping command will perform five pings and stop automatically, and will not need the -c 4 option.

If the aforementioned tests fail, you will need to check your connections and then confirm the correct configuration for your network.

There's more…

If you find yourself using your Raspberry Pi regularly on the network, you won't want to have to look up the IP address each time you want to connect to it.

On some networks, you may be able to use the Raspberry Pi's hostname instead of its IP address (the default is raspberrypi). To assist with this, you may need some additional software such as Bonjour to ensure hostnames on the network are correctly registered. If you have an OS X Mac, you will have Bonjour running already. On Windows, you can either install iTunes (if you haven't got it) which also includes the service, or you can install it separately (via the Apple Bonjour Installer available from https://support.apple.com/kb/DL999). Then you can use the hostname, raspberrypi or raspberrypi.local, to connect to the Raspberry Pi over the network. If you need to change the hostname, then you can do so in the Raspberry Pi configuration tool, shown previously.

Alternatively, you may find it helpful to fix the IP address to a known value by manually setting the IP address. However, remember to switch it back to use DHCP when connecting on another network.

Some routers will also have an option to set a Static IP DHCP address, so the same address is always given to the Raspberry Pi (how this is set will vary on the router itself).

Knowing your Raspberry Pi's IP address or using the hostname is particularly useful if you intend to use one of the remote access solutions described later on, which avoids the need for a display.