Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Getting Started with Python for the Internet of Things

You're reading from   Getting Started with Python for the Internet of Things Leverage the full potential of Python to prototype and build IoT projects using the Raspberry Pi

Arrow left icon
Product type Course
Published in Feb 2019
Publisher
ISBN-13 9781838555795
Length 732 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (5):
Arrow left icon
Tim Cox Tim Cox
Author Profile Icon Tim Cox
Tim Cox
Prof. Diwakar Vaish Prof. Diwakar Vaish
Author Profile Icon Prof. Diwakar Vaish
Prof. Diwakar Vaish
Sai Yamanoor Sai Yamanoor
Author Profile Icon Sai Yamanoor
Sai Yamanoor
Steven Lawrence Fernandes Steven Lawrence Fernandes
Author Profile Icon Steven Lawrence Fernandes
Steven Lawrence Fernandes
Srihari Yamanoor Srihari Yamanoor
Author Profile Icon Srihari Yamanoor
Srihari Yamanoor
+1 more Show less
Arrow right icon
View More author details
Toc

Table of Contents (37) Chapters Close

Title Page
Copyright and Credits
About Packt
Contributors
Preface
1. Getting Started with a Raspberry Pi 3 Computer FREE CHAPTER 2. Dividing Text Data and Building Text Classifiers 3. Using Python for Automation and Productivity 4. Predicting Sentiments in Words 5. Detecting Edges and Contours in Images 6. Building Face Detector and Face Recognition Applications 7. Using Python to Drive Hardware 8. Sensing and Displaying Real-World Data 9. Building Neural Network Modules for Optical Character Recognition 10. Arithmetic Operations, Loops, and Blinky Lights 11. Conditional Statements, Functions, and Lists 12. Communication Interfaces 13. Data Types and Object-Oriented Programming in Python 14. File I/O and Python Utilities 15. Requests and Web Frameworks 16. Awesome Things You Could Develop Using Python 17. Robotics 101 18. Using GPIOs as Input 19. Making a Gardener Robot 20. Basics of Motors 21. Bluetooth-Controlled Robotic Car 22. Sensor Interface for Obstacle Avoidance 23. Making Your Own Area Scanner 24. Basic Switching 25. Recognizing Humans with Jarvis 26. Making Jarvis IoT Enabled 27. Giving Voice to Jarvis 28. Gesture Recognition 29. Machine Learning 30. Making a Robotic Arm 1. Other Books You May Enjoy Index

Networking directly to a laptop or computer


It is possible to connect Raspberry Pi LAN port directly to a laptop or computer using a single network cable. This will create a local network link between the computers, allowing all the things you can do if connected to a normal network without the need for a hub or a router, including connection to the internet, if Internet Connection Sharing (ICS) is used, as follows:

Note

Make use of Raspberry Pi, with just a network cable, a standard imaged SD card, and power.

ICS allows Raspberry Pi to connect to the internet through another computer. However, some additional configuration is required for the computers to communicate across the link, as Raspberry Pi does not automatically allocate its own IP address.

We will use the ICS to share a connection from another network link, such as a built-in Wi-Fi on a laptop. Alternatively, we can use a direct network link (refer to the Direct network link section under the There's more... section) if the internet is not required or if the computer has only a single network adapter.

Note

Although this setup should work for most computers, some setups are more difficult than the others. For additional information, see www.pihardware.com/guides/direct-network-connection.

Getting ready

You will need Raspberry Pi with power and a standard network cable.

Note

Raspberry Pi Model B LAN chip includes Auto-MDIX (Automatic Medium-Dependent Interface Crossover). Removing the need to use a special crossover cable (a special network cable wired so that the transmit lines connect to receive lines for direct network links), the chip will decide and change the setup as required automatically.

It may also be helpful to have a keyboard and monitor available to perform additional testing, particularly if this is the first time you have tried this.

To ensure that you can restore your network settings to their original values, you should check whether it has a fixed IP address or the network is configured automatically.

To check the network settings on Windows 10, perform these steps:

  1. Open Settings from the start menu, then select Network and Internet, then Ethernet, and click on Change adapter options from the list of Related Settings.

To check the network settings on Windows 7 and Vista, perform the following steps:

  1. Open Network and Sharing Center from the Control Panel and click on Change adapter settings on the left-hand side.
  2. To check the network settings on Windows XP, open Network Connections from the Control Panel.
  3. Find the item that relates to your wired network adapter (by default, this is usually called Ethernet or Local Area Connection, as shown in the following screenshot):

Locating your wired network connection

  1. Right-click on its icon and click on Properties. A dialog box will appear, as shown in this screenshot:

Selecting the TCP/IP properties and checking the settings

  1. Select the item called Internet Protocol (TCP/IP) or Internet Protocol Version 4 (TCP/IPv4) if there are two versions (the other is Version 6), and click on the Properties button.
  2. You can confirm that your network is set by using automatic settings or a specific IP address (if so, take note of this address and the remaining details as you may want to revert the settings at a later point).

To check the network settings on Linux, perform the following steps:

  1. Open the Network Settings dialog box and select Configure Interface. Refer to the following screenshot:

Linux Network Settings dialog box

  1. If any settings are manually set, ensure you take note of them so that you can restore them later if you want.

To check the network settings on macOS X, perform the following steps:

  1. Open System Preferences and click on Networks. You can then confirm whether the IP address is allocated automatically (using DHCP) or not.

 

  1. Ensure that if any settings are manually set you take note of them so you can restore them later if you want to. Refer to the following screenshot:

OS X Network Settings dialog box

If you just need to access or control Raspberry Pi without an internet connection, refer to the Direct network link section in the There's more...section.

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.

How it works...

When we enable ICS on the primary computer, the operating system will automatically allocate a new IP address to the computer. Once connected and powered up, Raspberry Pi will set itself to a compatible IP address and use the primary computer IP address as an Internet Gateway.

By using Apple Bonjour, we are able to use raspberrypi hostname to connect to Raspberry Pi from the connected computer.

Finally, we check whether the computer can communicate over the direct network link to Raspberry Pi, back the other way, and also through to the internet.

There's more...

If you do not require the internet on Raspberry Pi, or your computer has only a single network adapter, you can still connect the computers together through a direct network link. Refer to the following diagram:

Connecting and using Raspberry Pi with just a network cable, a standard imaged SD card, and power

Direct network link

For a network link to work between two computers, they need to be using the same address range. The allowable address range is determined by the subnet mask (for example, 255.255.0.0 or 255.255.255.0 would mean all IP addresses should be the same except for the last two, or just the last number in the IP address; otherwise, they will be filtered).

To use a direct link without enabling ICS, check the IP settings of the adapter you are going to connect to and determine whether it is automatically allocated or fixed to a specific IP address.

Most PCs connected directly to another computer will allocate an IP address in the range 169.254.X.X (with a subnet mask of 255.255.0.0). However, we must ensure that the network adaptor is set to Obtain an IP address automatically.

For Raspberry Pi to be able to communicate through the direct link, it needs to have an IP address in the same address range, 169.254.X.X. As mentioned before, Raspberry Pi will automatically give itself a suitable IP address and connect to the network.

Therefore, assuming we have Apple Bonjour (mentioned previously), we only need to know the hostname given to Raspberry Pi (raspberrypi).

See also

If you don't have a keyboard or screen connected to Raspberry Pi, you can use this network link to remotely access Raspberry Pi just as you would on a normal network (just use the new IP address you have set for the connection). Refer to the Connecting remotely to Raspberry Pi over the network using VNC and Connecting remotely to Raspberry Pi over the network using SSH (and X11 Forwarding) recipes.

There is lots of additional information available on my website, https://pihw.wordpress.com/guides/direct-network-connection, including additional troubleshooting tips and several other ways to connect to your Raspberry Pi without needing a dedicated screen and keyboard.

You have been reading a chapter from
Getting Started with Python for the Internet of Things
Published in: Feb 2019
Publisher:
ISBN-13: 9781838555795
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image