Raspberry Pi 3 Cookbook for Python Programmers
上QQ阅读APP看书,第一时间看更新

How to do it...

First, we need to enable ICS on our network devices. In this case, we will be sharing the internet, which is available on Wireless Network Connection through the Ethernet connection to Raspberry Pi.

For Windows, perform these steps:

  1. Return to the list of network adapters, right-click on the connection that links
    to the internet (in this case, the WiFi or Wireless Network Connection device), and click on Properties:
Locating your wired network connection
  1. At the top of the window, select the second tab (in Windows XP, it is called Advanced; in Windows 7 and Windows 10, it is called Sharing), as shown in the following screenshot:
Selecting the TCP/IP properties and noting the allocated IP address
  1. In the Internet Connection Sharing section, check the box for Allow other network users to connect through this computer's Internet connection (if present, use the drop-down box to select the Home networking connection: option as Ethernet or Local Area Connection). Click on OK and confirm whether you previously had a fixed IP address set for Local Area Connection.

For macOS X, to enable the ICS, perform the following steps:

  1. Click on System Preferences and then click on Sharing.
  2. Click on Internet Sharing and select the connection from which we want to share the internet (in this case, it will be the Wi-Fi AirPort). Then select the connection that we will connect Raspberry Pi to (in this case, Ethernet).

For Linux to enable the ICS, perform the following steps:

  1. From the System menu, click on Preferences and then on Network Connections. Select the connection you want to share (in this case, Wireless) and click on Edit or Configure. In the IPv4 Settings tab, change the Method option to Shared to other computers.

The IP address of the network adapter will be the Gateway IP address to be used on Raspberry Pi, and will be assigned an IP address within the same range (it will all match, except the last number). For instance, if the computer's wired connection now has 192.168.137.1, the Gateway IP of Raspberry Pi will be 192.168.137.1 and its own IP address might be set to 192.168.137.10.

Fortunately, thanks to updates in the operating system, Raspbian will now automatically allocate a suitable IP address to join the network and set the gateway appropriately. However, unless we have a screen attached to Raspberry Pi or scan for devices on our network, we do not know what IP address Raspberry PI has given itself.

Fortunately (as mentioned in the Networking and connecting your Raspberry Pi to the internet via the LAN connector recipe in the There's more... section), Apple's Bonjour software will automatically ensure hostnames on the network are correctly registered. As stated previously, if you have a Mac OS X, you will have Bonjour running already. On Windows, you can either install iTunes, or you can install it separately (available from https://support.apple.com/kb/DL999). By default, the hostname raspberrypi can be used.

We are now ready to test the new connection, as follows:

  1. Connect the network cable to Raspberry Pi and the computer's network port, and then power up Raspberry Pi, ensuring that you have re-inserted the SD card if you previously removed it. To reboot Raspberry Pi, if you edited the file there, use sudo reboot to restart it.
  2. Allow a minute or two for Raspberry Pi to fully power up. We can now test the connection.
  1. From the connected laptop or computer, test the connection by pinging with the hostname of Raspberry Pi, as shown in the following command (on Linux or OS X, add -c 4 to limit to four messages or press Ctrl + C to exit):
ping raspberrypi  

Hopefully, you will find you have a working connection and receive replies from the
Raspberry Pi.

If you have a keyboard and a screen connected to Raspberry Pi, you can perform the
following steps:

  1. You can ping the computer in return (for example, 192.168.137.1) from Raspberry Pi Terminal as follows:
sudo ping 192.168.137.1 -c 4  
  1. You can test the link to the internet by using ping to connect to a well-known website as follows, assuming you do not access the internet through a proxy server:
sudo ping www.raspberrypi.org -c 4  

If all goes well, you will have full internet available through your computer to Raspberry Pi, allowing you to browse the web as well as update and install new software.

If the connection fails, perform the following steps:

  1. Repeat the process, ensuring that the first three sets of numbers match with Raspberry Pi and the network adapter IP addresses.
  2. You can also check that when Raspberry Pi powers up, the correct IP address is being set using the following command:
hostname -I  
  1. Check your firewall settings to ensure your firewall is not blocking internal network connections.