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

Configuring your network manually


If your network does not include a DHCP server or it is disabled (typically, these are built into most modern ADSL/cable modems or routers), you may need to configure your network settings manually.

Getting ready

Before you start, you will need to determine the network settings for your network.

You will need to find out the following information from your router's settings or another computer connected to the network:

  • IPv4 address: This address will need to be selected to be similar to other computers on the network (typically, the first three numbers should match, that is, 192.168.1.X if netmask is 255.255.255.0), but it should not already be used by another computer. However, avoid x.x.x.255 as the last address since this is reserved as a broadcast address.

  • Subnet mask: This number determines the range of addresses the computer will respond to (for a home network, it is typically 255.255.255.0, which allows up to 254 addresses). This is also sometimes referred to as the netmask.

  • Default gateway address: This address is usually your router's IP address, through which the computers connect to the Internet.

  • DNS servers: The DNS server (Domain Name Service) converts names into IP addresses by looking them up. Usually, they will already be configured on your router, in which case you can use your router's address. Alternatively, your Internet Service Provider (ISP) may provide some addresses, or you can use Google's public DNS servers at the addresses 8.8.8.8 and 8.8.4.4. These are also called nameservers in some systems.

For Windows, you can obtain this information by connecting to the Internet and running the following command:

ipconfig /all

Locate the active connection (usually called Local Area Connection 1 or similar if you are using a wired connection, or if you are using Wi-Fi, it is called wireless network connection) and find the information required, as follows:

The ipconfig/all command shows useful information about your network settings

For Linux and Mac OS X, you can obtain the required information with the following command (note that it is ifconfig rather than ipconfig):

ifconfig

The DNS servers are called nameservers and are usually listed in the resolv.conf file. You can use the less command as follows to view its contents (press Q to quit when you have finished viewing it):

less /etc/resolv.conf

How to do it…

To set the network interface settings, edit /etc/network/interfaces using the following code:

sudo nano /etc/network/interfaces

Now, perform the following steps:

  1. We can add the details for our particular network, the IP address number we want to allocate to it, the netmask address of the network, and the gateway address, as follows:

    iface eth0 inet static
      address 192.168.1.10
      netmask 255.255.255.0
      gateway 192.168.1.254
    
  2. Save and exit by pressing Ctrl + X, Y, and Enter.

  3. To set the nameservers for DNS, edit /etc/resolv.conf using the following code:

    sudo nano /etc/resolv.conf
    
  4. Add the addresses for your DNS servers as follows:

    nameserver 8.8.8.8
    nameserver 8.8.4.4
    
  5. Save and exit by pressing Ctrl + X, Y, and Enter.

There's more…

You can configure the network settings by editing cmdline.txt in the BOOT partition and adding settings to the startup command line with ip.

The ip option takes the following form:

ip=client-ip:nfsserver-ip:gw-ip:netmask:hostname:device:autoconf
  • The client-ip option is the IP address you want to allocate to the Raspberry Pi

  • The gw-ip option will set the gateway server address if you need to set it manually

  • The netmask option will directly set the netmask of the network

  • The hostname option will allow you to change the default raspberrypi hostname

  • The device option allows you to specify a default network device if more than one network device is present

  • The autoconf option allows the automatic configuration to be switched on or off