Dancing Lessons
If you want to use an Android app to invite the Raspberry Pi for a dance, you should bring WebSockets to the ball, because the TCP-based protocol makes for smooth communication with the graceful nano-computer.
If you want to use an Android app to invite the Raspberry Pi for a dance, you should bring WebSockets to the ball, because the TCP-based protocol makes for smooth communication with the graceful nano-computer.
The various small computers with relatively powerful microprocessors now on the market are attractive to developers, engineers, and makers alike. The processors provide fast and easy access to hardware buses via I2C, 1-Wire, or SPI, allowing users to combine the worlds of software and electronics.
Connecting data to the real world – for example, by reading external sensors or controlling actuators – calls for simple and efficient communication between a client and server, as provided by the WebSocket protocol. Basically, this technology is used when you want to realize fast and powerful, bi-directional communication in real time between web applications and clients.
In this article, I present two sample projects involving the operation of a Raspberry Pi as a WebSocket server to which a WebSocket client sends asynchronous broadcast messages. The projects are realized by means of a C implementation of the popular WebSocket protocol. With the use of software libraries, you can establish a handy communications channel without the need for a comprehensive knowledge of networking.
WebSockets fills a gap left by the HTTP protocol, which operates on a question-and-answer basis but has not been able to handle the needs of many applications and their developers for a long time.
In the beginning, HTTP developers tried to expand the protocol with active queries to create the impression of asynchronicity. This so-called polling method is a trick that sends multiple queries to the server to determine whether the data for the client is already readable. In practice, this method proves to be inefficient because it creates a great deal of traffic, which in turn burdens the network.
In seeking a solution to these problems, W3C and IETF developed a new standard in December 2011. The new standard consists of a WebSocket protocol that processes a bi-directional exchange of data via a TCP connection. As illustrated in Figure 1, the standard uses the typical TCP protocol, but it intercepts the connection between server and client during the typical HTTP transaction and requests a change and update in protocol.
The most important advantages of the WebSocket protocol include the following:
WebSockets were originally intended to take care of communication between Internet browsers and Internet services. However, the protocol soon saw application in other architectures, such as home automation projects, communication programs, or multiplayer games. In the example presented in this article, WebSockets are used to connect the Raspberry Pi single-board computer with Android mobile devices.
The article shows how to build two applications (Figure 2) on top of the WebSocket API. The WebSocket server acts as a simple echo server, whereas the WebSocket client runs on Android and sends and receives data as simple JSON objects to and from the server.
The program code implements the echo
function and the capability for sending asynchronous broadcast messages to all of the clients connected to the server. To avoid writing yet another article on GPIO monitoring, I assume that the server and client applications exchange asynchronous D-Bus signals, which in my example are generated by the udisks
daemon. Concretely, the server needs to send a message to the client application on the Android device as soon as a user plugs a USB stick into the Raspberry Pi. The message then appears on the Android device. Figure 2 shows the schematics of the design.
Figure 3 shows the main window in the application for the WebSocket client. It consists of a graphical interface with three EditBox
elements. The user inputs text, providing parameters such as an IP address, the number of ports, and a timeout in milliseconds to be used by the program. The socket connects to the Raspberry Pi when the Connect button is pressed. The user sends commands to the server via the EditBox
field in the lower part of the screen. Each message sent via this method appears in the middle of the screen.
Although the Internet application described here was developed and tested on a Raspberry Pi, readers without access to an ARM computer can also compile and test the project on a Linux computer [1]. Current versions of the source code for the server [2] and the client [3] are available on GitHub.
Pages: 8
Price $15.99
(incl. VAT)