Use an Android smartphone as a Raspberry Pi screen

Recycled

© Lead Image © Valery Kachaev, 123RF.com

© Lead Image © Valery Kachaev, 123RF.com

Article from Issue 221/2019
Author(s):

A simple Python program turns a disused smartphone into a wirelessly connected Raspberry Pi display.

New low-end $100 smartphones offer enough performance for everyday life, but the market for used devices is dismal. Instead of letting your smartphones and tablets gather dust, you can put their touchscreens to work and provide a better display than those of the same size marketed for the Raspberry Pi.

One disadvantage is the lack of a connection cable for the output from the Raspberry Pi to the smartphone. Fortunately, you have alternatives, such as a wireless connection, which I will show you how to set up in this article.

Another problem is the display size. Although the 800x480-pixel resolution of a smartphone, for example, is close to that of the official Pi display, the screen area is extremely small; a normal desktop simply cannot be displayed on devices smaller than a 10-inch tablet.

For many projects, you don't need a full desktop, though (e.g., if you only need to output a small amount of data or status information), and you can limit the controls to a couple of buttons. With very little effort, you can write programs with graphical interfaces that fit perfectly on the smartphone screen. In this article is an example program you can use as a model for your own projects.

Alternatives

One approach for displaying a graphical user interface on the smartphone is to split the application and display into two programs. For example, the Rasp Pi could run a program that serves up its output as a website, with the smartphone browser displaying the results.

The disadvantage of this solution is that, whereas a small web server can be integrated into any program – independent of the programming language and without too much overhead, thanks to a wide variety of program libraries – an attractive presentation also requires know-how in web design and JavaScript programming.

A variant of this approach uses an app on the smartphone instead of the web browser. Although this involves significantly more work, it also offers more freedom (e.g., communication over Bluetooth instead of a wireless network).

The smart Blue Dot [1] app, which sets up an Android device as a Bluetooth remote for Raspberry Pi, can be used as a project template, and you can integrate it directly into your own programs thanks to the matching Python library (Figure 1). The well-known Pi Control [2], which is a web-based application that provides status information about your Rasp Pi, is another example and requires you to be familiar with the Android or iOS environment, instead of web design.

Figure 1: Blue Dot lets you control a Raspberry Pi with a Python application.

If you only want to write a program, use the approach described in the following sections. The program runs on the Rasp Pi and outputs the graphical user interface to another computer on the network (i.e., a smartphone or tablet) rather than over its HDMI interface. Although not particularly efficient – both the user data and the user interface need to cross the wire – the programming overhead is manageable.

Old and Good?

Programs running on the Rasp Pi output the graphical user interface via the X server, an independent program that receives the graphics commands and takes care of the rest of the work.

X server is older than Linux itself and dates back to a time when computers were exclusively located in data centers. Special terminals in the office ran only an X server, which displayed programs with graphical interfaces. The server received the graphics commands over the network from the programs in the data center.

The combination of a Rasp Pi and smartphone uses the same approach. The Rasp Pi plays the part of the remote computer and the smartphone the part of the X terminal. Because you only want to see the output of a single program and are not interested in seeing the complete desktop interface, you do not even need to install the Raspbian Pixel desktop.

On the Pi side, you only need the X client. If you have installed Raspbian with a graphical user interface, everything you need is already on board. For the Lite version, you need a separate client, which also handles the installation of the sample program as a desirable side effect.

The ancient X architecture has suffered from various issues over the years, especially relating to security and performance. Modern computers (like the Rasp Pi) contain a powerful GPU that does the rendering itself, which does not make sense if you want to transfer the output across the wire and restricts you somewhat in your choice of program libraries: The output from directly rendered programs only displays locally.

X Server

For an Android smartphone, you need to install XSDL [3], a free and open source [4] X server, from the Playstore. In the lab, I even managed to install it on an ancient device with Android 2.3, but the program always crashed when launched. Through lack of experience and no apparent suitable app, I cannot give iOS users any recommendations.

When starting the X server, configure it using the button that occupies the entire top margin. Set the mouse emulation to Tablet and the left mouse button to Touch or hold. In the Video menu, configure the alignment to suit your needs for the project at hand (e.g., landscape, portrait, automatic).

The next configuration step looks a bit arcane: A screen appears that offers various resolutions. If you do not want to use the native resolution, then tap on the desired alternative. You can also select font scaling afterward. You can change both at each startup; in case of doubt, the server simply starts with the old configuration. Changing the resolution is only possible on displays with a very high resolution anyway.

The X server itself does not provide any interaction; after startup you will only see a blank screen with some instructions. More specifically, the server displays the IP address of the mobile device. Every application you launch then reverts to this background. For example, Figure 2 shows the Xeyes program from the x11-apps package that's just for fun: The eyes follow the mouse cursor.

Figure 2: The X server after startup. The Xeyes application is running in the upper left corner.

For the Rasp Pi to find the smartphone automatically later on, you need to assign a fixed IP address on the local network to the cellphone. You can either do this via your router by permanently assigning the IP address to the MAC address of the device or in the advanced network settings of the smartphone, which you can normally access with a long press on the network connection.

To avoid managing the lengthy IP address on the Raspberry Pi, you can enter it in the Pi's /etc/hosts file and assign a meaningful name to the smartphone.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Python’s Tkinter Library

    Use Tkinter to control your Rasp Pi projects from a smartphone or tablet.

  • Python IDEs

    A number of useful development environments are available for Python in Linux. We’ll show you around some popular Python IDEs that are more than just text editors.

  • VS Code for Rasp Pi

    Professional and casual Python developers save time and keystrokes in a Visual Studio Code development environment for remote coding of a Raspberry Pi.

  • ioBroker + Rasp Pi

    Control devices from different manufacturers of home automation devices from a single interface by combining free software and a Raspberry Pi.

  • Deluxe Web Radio

    An LCD in an unusual format, a self-designed housing, and matching software make a simple Raspberry Pi web radio the perfect centerpiece for any living room.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News