6 min read

In this article by Gökhan Kurt, author of the book Raspberry Pi Android Projects, we will make a gentle introduction to both Pi and Android platforms to warm us up. Many users of the Pi face similar problems when they wish to administer it. You have to be near your Pi and connect a screen and a keyboard to it. We will solve this everyday problem by remotely connecting to our Pi desktop interface. The article covers the following topics:

  • Installing necessary components in the Pi and Android
  • Connecting the Pi and Android

(For more resources related to this topic, see here.)

Installing necessary components in the Pi and Android

The following image shows you that the LXDE desktop manager comes with an initial setup and a few preinstalled programs:

Raspberry Pi Android Projects

LXDE desktop management environment

By clicking on the screen image on the tab bar located at top, you will be able to open a terminal screen that we will use to send commands to the Pi.

The next step is to install a component called x11vnc. This is a VNC server for X, the window management component of Linux. Issue following command on the terminal:

sudo apt-get install x11vnc

This will download and install x11vnc to the Pi. We can even set a password to be used by VNC clients that will remote desktop to this Pi using the following command and provide a password to be used later on:

x11vnc –storepasswd

Next, we can get the x11vnc server running whenever the Pi is rebooted and the LXDE desktop manager starts. This can be done through the following steps:

  1. Go into the .config directory on the Pi user’s home directory located at /home/pi:cd /home/pi/.config
  2. Make a subdirectory here named autostart:mkdir autostart
  3. Go into the autostart directory:cd autostart
  4. Start editing a file named x11vnc.desktop. As a terminal editor, I am using nano, which is the easiest one to use on the Pi for novice users, but there are more exciting alternatives, such as vi:
    nano x11vnc.desktop

    Add the following content into this file:

    [Desktop Entry]
    Encoding=UTF-8
    Type=Application
    Name=X11VNC
    Comment=
    Exec=x11vnc -forever -usepw -display :0 -ultrafilexfer
    StartupNotify=false
    Terminal=false
    Hidden=false
    
  5. Save and exit using (Ctrl+X, Y, <Enter>) in order if you are using nano as the editor of your choice.
  6. Now you should reboot the Pi to get the server running using the following command:
    sudo reboot

    After rebooting, we can now find out what IP address our Pi has been given in the terminal window by issuing the ifconfig command. The IP address assigned to your Pi is to be found under the eth0 entry and is given after the inet addr keyword. Write this address down:

    Raspberry Pi Android Projects

    Example output from ifconfig command

  7. The next step is to download a VNC client to your Android device.In this project, we will use a freely available client for Android, namely androidVNC or as it is named in the Play Store—VNC Viewer for Android by androidVNC team + antlersoft. The latest version in use at the writing of this book was 0.5.0.

    Note that in order to be able to connect your Android VNC client to the Pi, both the Pi and the Android device should be connected to the same network. Android through Wi-Fi and Pi through its Ethernet port.

Connecting the Pi and Android

Install and open androidVNC on your device. You will be presented with a first activity user interface asking for the details of the connection. Here, you should provide Nickname for the connection, Password you enter when you run the x11vnc –storepasswd command, and the IP Address of the Pi that you have found out using the ifconfig command. Initiate the connection by pressing the Connect button, and you should now be able to see the Pi desktop on your Android device.

In androidVNC, you should be able to move the mouse pointer by clicking on the screen and under the options menu in the androidVNC app, you will find out how to send text and keys to the Pi with the help of Enter and Backspace.

You may even find it convenient to connect to the Pi from another computer. I recommend using RealVNC for this purpose, which is available on Windows, Linux, and Mac OS.

What if I want to use Wi-Fi on the Pi?

In order to use a Wi-Fi dongle on the Pi, first of all, open the wpa-supplicant configuration file using the nano editor with the following command:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Add the following to the end of this file:

network={
    ssid="THE ID OF THE NETWORK YOU WANT TO CONNECT"
    psk="PASSWORD OF YOUR WIFI"
}

I assume that you have set up your wireless home network to use WPA-PSK as the authentication mechanism. If you have another mechanism, you should refer to the wpa_supplicant documentation. LXDE provides even better ways to connect to Wi-Fi networks through a GUI. It can be found on the upper-right corner of the desktop environment on the Pi.

Connecting from everywhere

Now, we have connected to the Pi from our device, which we need to connect to the same network as the Pi. However, most of us would like to connect to the Pi from around the world as well. To do this, first of all, we need to now the IP address of the home network assigned to us by our network provider. By going to http://whatismyipaddress.com URL, we can figure out what our home network’s IP address is. The next step is to log in to our router and open up requests to the Pi from around the world. For this purpose, we will use a functionality found on most modern routers called port forwarding.

Be aware of the risks contained in port forwarding. You are opening up access to your Pi from all around the world, even to malicious ones. I strongly recommend that you change the default password of the user pi before performing this step. You can change passwords using the passwd command.

By logging onto a router’s management portal and navigating to the Port Forwarding tab, we can open up requests to the Pi’s internal network IP address, which we have figured out previously, and the default port of the VNC server, which is 5900. Now, we can provide our external IP address to androidVNC from anywhere around the world instead of an internal IP address that works only if we are on the same network as the Pi.

Raspberry Pi Android Projects

Port forwarding settings on Netgear router administration page

Refer to your router’s user manual to see how to change the Port Forwarding settings. Most routers require you to connect through the Ethernet port in order to access the management portal instead of Wi-Fi.

Summary

In this article, we installed Raspbian, warmed up with the Pi, and connected the Pi using an Android device.

Resources for Article:

 


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here