Controlling the Raspberry Pi with Android via the WebSocket protocol

Lead Image © Cameron Ferrelle,

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.

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.

Figure 1: The handshake procedure for the WebSocket protocol.

The most important advantages of the WebSocket protocol include the following:

  • The protocol enables client-server architectures, which generate minimal network traffic. For example, polling is eliminated.
  • Asynchronous broadcast messages are easy to implement.
  • Users can select the format for uploaded data. For example, a user can choose among JSON and XML files or simple text messages.
  • Transfer channels that have been divided and optimized for the various data types are utilized (e.g., for text and binary files).
  • The protocol can handle encrypted connections.
  • Many libraries support the WebSocket protocol, and bindings exist for the best known programming languages, such as Java, JavaScript, C#, and PHP.
  • Implementing the protocol is quick and easy. In most cases, the libraries provide the programmer with a high degree of abstraction. This avoids the necessity of knowing details for building, formatting, and sending data frames.
  • Most of the well-known Internet browsers support WebSocket technology.

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.

Twice the Fun

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.

Figure 2: The Raspberry Pi used for the project presented in this article works as a WebSocket server and at the same time takes care of two tasks. First, it acts as a simple echo server. Second, it sends a message to the smartphone as soon as someone plugs in a USB stick.

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.

Figure 3: The user interface for the WebSocketsClient application.

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.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content