Chapter 9. Using the Network
In this chapter, you will learn about using the BeagleBone's network connection to stream data to web services and control hardware remotely through web interfaces. We will cover the following topics:
TCP/IP and TCP servers for remote hardware access
HTTP servers for remote hardware access
Streaming data to remote storage and visualization services
The BeagleBone has an Ethernet, or IEEE 802.3, connection. This provides a physical layer over which TCP/IP is implemented. TCP/IP, or Transmission Control Protocol/Internet Protocol, is a suite of networking layers that makes up the base of what we call the Internet. It is named after its two most important layers, TCP, which provides error-checked data transfer between endpoints, and IP, which provides routing of packets between hosts based on their IP addresses.
Pretty much all modern operating systems provide interfaces called network sockets, which are created and used to abstract an Internet connection using a model like TCP/IP. A socket is defined by its protocol (for example, TCP), its local IP address, and its local port number. A port is a software abstraction that allows any number of processes on a computer with a single IP address to exchange data over the network independently of each other, by each having their own unique ID, called a port number. Once a connection has...
HTTP, or Hypertext Transfer Protocol, is an application protocol that's implemented on top of the TCP/IP suite, and is what the World Wide Web (WWW) is built on. The HTTP protocol defines the request-response structure, in which a client (such as a web browser) requests a resource from a server whose address is given by a Uniform Resource Locator (URL), and the server responds with a resource, such as an HTML file. So instead of opening a TCP socket and keeping it open while passing raw TCP/IP packets, as in the preceding example, the HTTP client connects to the server over a TCP socket and sends an HTTP request packet, then the server sends back an HTTP response packet, and the socket is closed.
PyBBIO includes a library called BBIOServer
, which provides an API for creating simple HTML pages for web based user interfaces using HTTP. Let's run a simple example:
There are a number of services targeting the Internet of Things market that provide a place to store and view data over the Internet. These services typically include a simple HTTP API for streaming data to them, as well as a web interface for viewing the stored data, both as raw samples and in various visual ways. The idea is that you'd have a device like a BeagleBone on a local network, say at your home, and it would be routinely sampling some sort of sensor, say measuring temperature. Every time it reads a new temperature value, the BeagleBone will send it to the remote service's database over the Internet. Then you would be able to go to a website from anywhere, say if you were traveling, and look at the current temperature in your home, as well as track your home's temperature in the past. This is not the most exciting example, but even this could be practical as you could know right away if your heating has stopped working, and you could call to have it serviced before...
In this chapter, you learned how to take advantage of the BeagleBone's network connection to communicate with external hardware remotely, as well as to stream data to remote servers for storage and viewing.
In the next chapter, we will build upon what you've learned throughout the book to create some larger and more useful programs.