Saturday, 23 February 2013

Connecting via VNC to Raspberry PI from the Google Nexus 7

Posted by Gary Newell  |  at  22:09 9 comments

Introduction

This guide provides a step by step method of connecting the Google Nexus 7 Android tablet to the Raspberry PI using VNC.

You will need


For this tutorial you will need a Raspberry PI running Raspbian Wheezy. (Read this guide which shows how to set up the Raspberry PI).

You will also need a working internet connection on the Raspberry PI. (Read this guide which shows how to set up a wireless connection on the Raspberry PI).

You will also need a Google Nexus 7 Tablet PC (Android 4.1 Jellybean) - 16 GB.

Installing the server

  1. Boot the Raspberry PI (Doesn't matter if you have X started or not)
  2. Make sure you have an internet connection.
  3. If you booted into the Graphical Desktop double click the LXTerminal icon on the desktop.
  4. Type the following into the terminal window that opens:

    sudo apt-get install tightvncserver
  5. A message will appear asking whether you are sure you want to install the tightvncserver. Press y.
  6. A bunch of text will now scroll up the screen telling you what is being installed and how long there is to go.
  7. When the software has finished installing enter the following command to start the server:

    vncserver :1 -geometry 1200x720 -depth 16 -pixelformat rgb565
  8. The first time you run the above command you will not have any settings saved so you will be asked for a password which the user must use to connect to the PI via the VNC. You will be asked to repeat the same password.

    Note that if you choose a password longer than 8 characters it is truncated.
  9. You will now be asked if you want to set a readonly password. If you do then select yes and enter a readonly password.
  10. A message will now appear stating that settings have been saved in /home/pi/.vnc. You now have a running server.
So what did we actually do here?

In steps 1 to 3 we booted the PI, connected to the internet and opened a terminal window.
In steps 4 to 6 we installed the tightvncserver software
In step 7 we started the vnc server
In steps 8 and 9 we set the password a user should use to log in via VNC

In step 7 I said we started the server but what does the command vncserver :1 -geometry 1200x720 -depth 16 -pixelformat rgb565 actually mean.

The vncserver part is obviously starting the server. 

By default the vnc server starts running on port 5900. The :1 is the display number. What this means is that when you run the vnc client you need to specify port 5901. If you set the display number to :2 you would need to specify port 5902 and so on.

The -geometry part of the command specifies the screen resolution to use within the client and the -depth specifies the colour depth.

Installing the client

There are loads of VNC clients available on the Android platform but for this guide I have chosen AndroidVNC. It is free and it works.

  1. On the Google Nexus 7 open the Play Store.
  2. Click the search icon and type VNC Viewer.
  3. The first result should be android-vnc-viewer. Click the icon for this app.
  4. Click the install button

Connect to the Raspberry PI


  1. The first thing you need to know is your IP address on the Raspberry PI. To get this enter the following in a terminal window:

    ifconfig
  2. If you are connect wirelessly the IP address will be next to the wlan0 entry and will be something like 192.168.1.x (where x is the last number).
  3. Open AndroidVNC (it will be one of the installed apps)
  4. Leave the nickname field blank.
  5. Enter the password you entered in step 8 of "Installing the server".
  6. In the Address field enter the IP address from step 2.
  7. In the port number enter 5901.
  8. Click the connect button.
If everything has gone smoothly you should have a Google Nexus 7 connected to the Raspberry PI.


Run the VNC server at startup

I'm not sure running the VNC server when booting is a good idea by the way. If you are never going to use your Raspberry PI with a monitor then I guess there is a case for starting the VNC server every time you boot but if you generally use your Raspberry PI with a monitor but occasionally plan to use another device to connect to the PI then I would use SSH to connect to the PI and start the VNC server from within the SSH session. Read the next section to see how to do this.

To run the VNC server at startup following these steps:

  1. Open a terminal window by clicking the LXTerminal icon on the desktop.
  2. Run the following command

    sudo nano /etc/init.d/vncserver
  3. Enter the following script into the window (partly copied from http://www.abdevelopment.ca/blog/start-vnc-server-ubuntu-boot)

    #!/bin/sh -e
    export USER="pi"
    # parameters for tightvncserver
    DISPLAY="1"
    DEPTH="16"
    GEOMETRY="1200x720"
    NAME="VNCserver"
    OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
    . /lib/lsb/init-functions
    case "$1" in
    start)
    log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:{$DISPLAY}"
    su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
    ;;
    stop)
    log_action_begin_msg "Stopping vncserver for user '${USER}' on localhost:{$DISPLAY}"
    su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    esac
    exit 0
  4. The script above now needs to be made executable. To do this type the following command into the terminal window:
    sudo
    chmod +x /etc/init.d/vncserver

  5. Now run the following command

    sudo update-rc.d vncserver defaults.
  6. At this stage it is worth testing that you have entered the script correctly. To do this run the following command:

    sudo /etc/init.d/vncserver start
  7. If there are errors when you run the command in step 6 check that you have the same commands as in step 3 above.
  8. If there are no errors try and connect to the Raspberry PI using your Google Nexus. 
  9. The final test is to reboot your Raspberry PI and then attempt to connect to the Google Nexus.
So what did we do in this section?

In step 1 we opened a terminal window.
In step 2 we opened a text file called vncserver which is a shell script that will start when the Raspberry PI starts.
In step 3 we created the script that is run. 
In step 4 we made the script executable. (otherwise it really is just a text file),
In step 5 we turned it into a service that starts at boot up. 
In steps 6 and 7 we test the server by starting it manually.
In step 8 we rebooted the Raspberry PI,
In step 9 we try connecting to it.

Now the script may look complicated but essentially it is building the same tightvncserver command used in the first section.

If you specify start when running the script as in step 6 then the tightvncserver command will run. If you specify stop when running the script then the server will stop.
If you specify restart when running the script the server will stop and then start.

The default option is to start.

My preferred method for connecting to the PI

I'm not that keen on running the VNC server every time I start up the PI. I think it is better to make the decision to start it when you want to.

If you followed the section to start the VNC server every time then first of all you will need to reverse step 5.

The way to do that is by running the following command from a terminal window

sudo update-rc.d -f vncserver remove

The problem now is that to be able to VNC onto the Raspberry PI you need to be able to start the server. The problem with that is that you need to be on the PI to do that.

I recommend therefore installing an SSH client onto your Google Nexus 7.

  1. On the Google Nexus 7 open the Play Store.
  2. Click the search icon and type SSH Client
  3. The client I chose was called "Connectbot". Click the icon for this app.
  4. Click the install button
To connect to the Raspberry PI from connectbot:
  1. Run connectbot
  2. In the bar at the bottom type pi@192.168.1.x (where x is the last number of your IP address)
  3. Now enter the password for your pi user
  4. Once you are connected run sudo /etc/init.d/vncserver start
Your VNC server should have started again and you can run AndroidVNC to connect to it again.

Summary

You should now be able to use your Raspberry PI without having to use a HDMI cable connected to a monitor or television. In theory you can now plug it in anywhere in the house as long as you have a wireless signal to the router.

You can use most of these instructions for connecting to the Raspberry PI from any computer. You just need to have a VNC Viewer on the client computer and an SSH client.

Thankyou for reading.


About the Author

Gary Newell started the Everyday Linux User blog in 2010 and has written reviews on dozens of different Linux based operating systems. He has also written a number of tutorials.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

Related posts

9 comments:

  1. Thanks for the great post! Just a note that there's a typo with your "OPTIONS" export line

    {$DEPTH} should be ${DEPTH}

    ReplyDelete
    Replies
    1. Thankyou for letting me know. I have fixed it now.

      Delete
  2. Thanks a lot for this! Exactly what I was looking for, you saved me a lot of time. Worked just as described.

    My setup for the record:
    Samsung Galaxy Note II --> USB tethering --> Raspberry Pi

    Used a laptop to find the DHCP generated IP, then on the phone used androidVNC to connect. Now I can do coding on the train >;)

    ReplyDelete
  3. This specific HDMI leads can handle promises as much as 1080p. Most of these cables tend to be HDCP compliant. The particular spec handles high-bandwidth, uncompressed online video and also multi-channel electronic sound at the same time, all in a lead.
    http://www.dueltek.com.au/collections/hdmi-cables

    ReplyDelete
  4. Thanks, fascinating post :-)
    Unfortunately when I connect my Nexus 7 (KitKat) I get an error on the tablet screen showing a static pi screen with error GDBus.Error:org.freedesktop.PolicyKit1.Error.Failed: An authentication agent already exists for the given subject
    Any help appreciated.
    Thanks,
    Andy

    ReplyDelete
    Replies
    1. Hi Andy.

      Ok so I use a Nexus 7 (KitKat) to connect to my Raspberry Pi so those two variables are the same. The difference is therefore either the operating system on your PI or the model.

      Are you using model B? Which operating system have you got on the PI? I am using Raspbian and I am using the latest build of Raspbian.

      Just trying to work out what the differences are between your setup and mine at the moment.

      Regards

      Gary

      Delete
    2. Thanks Gary,
      From your description, things 'appear' to be identical.

      I'm using model B
      Stock Wheezy with all updates
      HDMI Monitor
      Wireless LAN
      Nexus 7 with stock KitKat

      I've now discovered I can simply Click the error away. (OK)
      Maybe I'm expecting too much of the setup, I had expected it to function similarly to a second monitor under windows?
      Best,
      Andy


      Delete
  5. I get the following error when running at boot up or testing with the sudo /etc/init.d/vncserver start command

    /etc/init.d/vncserver: 5: /etc/init.d/vncserver: syntax error: ")" unexpected

    I've checked and rechecked the code to makes sure I copied it correctly. I'm completely new to Raspbian and the Raspberry Pi. Any help would be appreciated. I've been able VNC in on my ASUS tablet following the manual methods using the command below.

    vncserver :1 -geometry 1200x720 -depth 16 -pixelformat rgb565.

    So I know I've installed tightvncserver correctly

    Thanks

    ReplyDelete
  6. I am using Microsoft Remote Desktop Client to connect to Raspberry Pi from my Nexus 5 phone. It is available for free on Playstore as RD Client. Use this command to install RDP support on Raspberry PI sudo apt-get install xrdp

    ReplyDelete

Feel free to comment on any of the blog posts. Please try to be constructive.

Offensive messages will be removed as will blatant adverts for misleading products and sites.

Thanks for visiting my blog

    Popular Posts

    Total Pageviews

    Subscribe

    Enter your email address:

    Delivered by FeedBurner

    Popular This Month

    What are other people buying?

    Zorin OS Buy
    PCLinuxOS Buy
    Lubuntu Buy
    Linux Mint Buy
    Simplicity Linux Buy
    Kubuntu Buy
    Kali Buy

    Followers

    Feedburner Followers

Blogger templates. Proudly Powered by Blogger.
back to top Google