Remote Controlling the Raspberry Pi from an iPad or iPhone

I recently published a popular post detailing a way to successfully connect your Raspberry Pi to your iPad. When I first set this up my main goal was to use the iPad as a useful display option and make use of the built in keyboard. This would make me more popular at home as I wouldn’t need to steal the keyboard from the main PC or force the kids to watch me programming on the wide screen telly.

The earlier tutorial demonstrates how to set up and SSH and VNC connection between your Raspberry Pi and your iPad when both are on the same local network – i.e. you are stuck at home. When I discovered the joy of the Raspberry on my iPad, I wanted to take it with me so I could access during my lunch break at work. This requires a little more faff as I need to tell my router to allow access to the Raspberry Pi from an external network.

I’ve recently changed my network configuration and had to go through the process of setting this all up again, so I’ve taken the opportunity to publish another stage 2 tutorial explaining the process for setting up the network to allow remote control of a Raspberry Pi while away from home.

Starting completely from scratch you must initially set up the system to connect via SSH and VNC while at home using the local network

First Stage – Set the Connection between iPad and Raspberry Pi from the Home Network

The first tutorial on How to Control the Raspberry Pi from an iPad took you through all the stages from:

  • Downloading the relevant apps Fing (free), WebSSH (free) and VNC Viewer (free for a short time)
  • Enabling SSH (Secure Shell Access) on the Raspberry Pi
  • Connecting via SSH on the iPad or iPhone
  • Setting up a Static IP address on the Raspberry Pi
  • Installing a VNC Server on the Raspberry Pi
  • Setting the VNC Server to run on boot
  • Connecting via VNC on the iPad or iPhone

I suggest you refer to the original tutorial to work through these stages.

Second Stage – Setting up the Connection between iPad and Raspberry Pi from an External Network or 3G

In this section I will provide the step by step stages for setting up remote access:

  • Determine Local and External IP address
  • Determine the VNC port used by your Raspberry Pi
  • Set up Port Forwarding on your Router
  • Test whether the ports are open
  • Connect remotely via SSH
  • Connect remotely via VNC

During the set up I will mostly be connected on the same local network but in order to test the remote connection I need to switch to an external network. To save me rushing off to a neighbours house I just flick over to a 3G mobile connection on my iPhone which means I tend to flit between the iPhone and iPad during the set up. Its also worth noting that you need to be on the same network connection to use Fing to its full advantage – so my router has a 2G and 5G option and as my Raspberry Pi uses the 2G wifi I want to make sure all my other devices are uses the same connection.

How to Determine Local and External IP address

Using my local SSH and VNC connection I enter the following script into the terminal to reveal my local IP address:

ifconfig

20140125-182307.jpgNote that this is even easier to do using the Fing app on your iPhone – ensure you are on the same wifi network as the Raspberry Pi and hit the refresh button on the Fing screen to display al the devices on your network and the relevant local IP addresses. We’ll use this again later, so its really worth installing.

To get your external IP address you need to go to What’sMyIP. You will only have one IP address per router so you can access this from any of your devices.

Joy both of these addresses down.

How to Determine the VNC port used by your Raspberry Pi

I’m not entirely sure what a port is but your device will use a different port for different functions and you need to know the right one in order to allow external access to it. SSH uses port 22 VNC uses a range of ports in the format 59xx with 5900 being the most common. When I set up the VNC server on my Raspberry Pi in the first tutorial, it set up 5901 and this caused me quite a lot of consternation when setting port forwarding for 5900 didn’t seem to work.

This is an optional extra step but it could save you a lot of bother.

20140125-182318.jpgOpen Fing on the iPhone (or iPad) and refresh to view the devices on the network. Select the Raspberry Pi and then click on Scan Services. This confirms that ssh is on 22 and I’ve managed to set up 2 VNC servers on 5901 and 5902.

Jot down these numbers as these will be the ports we need to set up in the Port Forwarding stage.

How to Set up Port Forwarding on your Router

Here’s a quick introduction to port forwarding from Lifehacker:

By default, your local network is local and cut off from the rest of the internet. In most cases you have just one IP address that’s shown to the world, despite the many that your router distributes to your individual computers and devices locally. What port forwarding does is take a port on that shared IP address that’s available to the rest of the web and forwards it to one of your local machines. This lets people from outside access services on your local network.

So in order to access my Raspberry Pi from the outside world via SSH I need to tell my router that it is ok to accept external traffic on port 22 and forward that request to the Raspberry Pi (identified by the local IP address that we wrote down in step 1). If I want to connect to my Raspberry Pi through a VNC client I will need to set up port forwarding for port 5901.

NB. Other tutorials I’ve seen recommend forwarding the following ports for VNC: 5900, 5500 and 5800 so they might be the right ones for you but the previous step would help you determine for certain.

To set port forwarding you need to access your router controls. I am using the ASUS RT-N66U and that can be accessed by typing the IP address 192.168.1.1 in the browser address bar. When I previously accessed the internet via the Virgin Superhub the address was 192.168.0.1 If you are struggling to find the correct address just search for your router on the web.

You will need to explore the router access panel to find the correct area to enter port forwarding details or you can go to Port Forward which will almost certainly have the instructions you are looking for. Here’s an example from the site that shows the details for forwarding port 5800 on my router.

Port Forwarding for VNC

I repeated this for ports 22, 5900, 5901, 5902, 5500 and 5800.

Security considerations

By setting up port forwarding you have effectively bypassed your networks firewall and potentially opened access to anyone. I don’t know a lot about this area but I suppose it is best to keep your external IP address private but there is also password control for both SSH and VNC access to the Raspberry Pi.

How to Test whether the ports are open

Having set up the port fowarding for a host of ports you might find it beneficial to test which ones are open and therefore ready to accept external traffic. YouGetSignal.com offers a simple and free service to enable you test multiple ports. When I checked mine I found that 22, 5901 and 5902 were open – just as I was expecting from the Fing scan.

Now that I’ve successfully got open ports for both SSH and VNC I can move on to the next stage and try to connect my iPad and iPhone remotely.

How to Connect remotely via SSH

The instructions are the same for the iPhone and iPad but I will test this on the iPhone so I can easily switch to my mobile phone network in order to test external access.

Open your SSH app (I’m using WebSSH) and add a new connection and fill in the following fields:

Name – Any descriptor such as Remote Raspberry Pi
Host – this will be your external IP address
Port – this should already be completed and will be 22
User – if you haven’t amended your Raspberry Pi login details this will still be pi
Password – probably raspberry

Now you need to hit connect and hope for the best. Hopefully you will be rewarded with the command prompt. This will work with both home and remote connections.

How to Connect remotely via VNC

Open VNC Viewer on your iPhone (same instructions for iPad) and set up a new connection.

20140125-182828.jpgAddress: this is you external IP address followed by  a : and a one or two digit number representing your port such as 82.44.11.1:0 (mine ends :1 because its port 5901)
Name: Anything descriptive eg RPiExternal

If you hit connect know you should hopefully see the beautiful raspberry on your screen. Congratulations.

Next Steps

Having gone through that almighty palaver to set up your connections bewteen your Raspberry Pi and your iPhone and ipad you may not be particularly excited about the thought of your external IP address changing. Some routers allow you to set up a single DNS which ensures you have a static name to use for external connection rather than the numbered IP address which might change. If you us an ASUS router you can find more detail here.

Useful External Sources

Controlling the Raspberry Pi from an iPad

After the frustrations of the first few days I am now coming on in leaps and bounds with the Raspberry Pi. Today I managed to set up my iPad so it can take control of the RPi. This means that the Raspberry Pi can be tucked away in corner, running headless while the iPad seizes control so I can make use of its keypad and monitor.

This means that I can now log on to RPi from anywhere that I have internet access – my main desire for doing this is so that I can run python and start learning to program from the mobile convenience of my iPad.

Before you can connect remotely to your Raspberry Pi you need to have connected it to the internet. You can do this by plugging it into an ethernet cable or as I have done by using the Edimax Wireless Nano USB Adapter, which is a fantastic piece of kit, its tiny and was a doddle to install.

[GARD]

Apps Required to Connect to your Raspberry Pi from an iPad

Apps to connect iPad to Raspberry PiThese are the apps that I’ve used to connect and all have proved effortless to setup and have so far served me well. Note the VNC Viewer is quite expensive so you might want to play around with some free versions before deciding to part with this much cash.

Fing (free) (Fing – Network Scanner)
WebSSH (free) (WebSSH)
VNC Viewer (£6.99) (VNC Viewer)

Using SSH to connect to your Raspberry Pi

Enable SSH
SSH (Secure SHell) provides access to the Pi’s command line interface. Before you can use it you need to enable SSH from the RPi config.txt file.

Open up the terminal and type

sudo raspi-config

From here you should select Advanced Options and then enable SSH. Now save and exit.

In order to connect to your Raspberry Pi you need to know the IP address that it is using. You can find this from the command (see instructions here)

ifconfig

but I have found it convenient to install the free iPhone or iPad app Fing which enables me to scan the home network to see all connections (will prove useful later).

Connect via SSH
Setting up SSH Connection with Raspberry PiDownload WebSSH (or similar) and add a new SSH connection.

The Host is the IP address used by your Raspberry Pi and identified using Fing. User and password are as set up in raspi-config. The default is pi raspberry

iPhone connected to Raspberry Pi via SSHIf you hit connect now you will be rewarded with access to the command line.

I find this extremely satisfying. One thing to bear in mind is that your Raspberry Pi will be using a dynamic IP address so every time you reboot it is likely to select a different address, which means you would need to amend your SSH connection details in the app.

You can get around that by forcing your RPi to use a static IP address.

How to Use a Static IP Address with your Raspberry Pi

[GARD]

There are detailed instructions on both Raspberry Shake and tuts+ but this is what I did:

From the terminal, enter the following code to open the nano text editor in order to change the connection details

$sudo nano /etc/network/interfaces

You will now be in the editor and can overwrite and add additional instructions.

Look for the line that reads

iface eth0 inet dhcp

and change the dhcp to static

iface eth0 inet static

Beneath this add the following lines of instructions:

address 192.168.0.11 <—-this should be your IP address as identified by Fing
netmask 255.255.255.0
network 192.168.0.1 <—you can also identify this in Fing, for me it was the address of my router
broadcast 192.168.100.255
gateway 192.168.100.254

In order to save this file you need to press CTRL O and then enter. CTRL X will close the nano editor. It is then worth opening a web browser to check that you are still connected to the internet and you haven’t messed everything up but entering the wrong addresses.

If you have messed something up, just retrace your steps and alter the details in the nano editor.

When you reboot, your IP address will be fixed and you won’t have to amend the SSH connection details.

Using VNC to Connect your Raspberry Pi from an iPad

The SSH protocol has just enabled us to connect to the command line of your Raspberry Pi but if you want to replicate the graphical desktop you will need to use VNC (Virtual Network Computing).

Install the VNC Server on the Raspberry Pi

To use this we need to install a VNCserver on the RPi. Follow these instructions from the terminal:

sudo apt-get install tightvncserver

follow the instructions and enter a username and password as instructed and then run the server by entering

tightvncserver

the VNC server won’t automatically run after you reboot (unlike SSH which will always be enabled). This could be a nuisance if you reboot regularly and if so you might want to follow the instructions below that force the VNC server to run at start up.

Connecting via VNC

Raspberry Pi running on an iPadDownload a VNC Viewer to your iPad, I’ve used VNC Viewer but there are cheaper and free versions available.

Add a new connection. The address will be the static IP address that you set above but should also include the port number. So mine reads 192.168.0.11:1 (I believe 1 is the default port so should work for you as well.

The password will be whatever you set up when installing tightVNCserver.

Hit connect and you should be rewarded with a beautiful raspberry on your iPad screen.

Running VNC when your Raspberry Pi Starts Up

The details for this task are can be found at adfruit who have provided a very clear tutorial for running VNC at StartUp.

Resources

I’ve used some really useful tutorials to help me complete the task – here are the ones I found the most useful:

Update

I have just added a second tutorial that explains how to go further with the setup and allows you to access the Raspberry Pi from an external network for true remote control of a Raspberry Pi.

Festive Frustrations with the Raspberry Pi

The Raspberry Pi Tiny ComputerI was really excited to open the tiny box that revealed a raspberry pi starter kit but if my partner had realised quite how much strife it was going to cause, I know for a fact should would never have put it under the xmas tree.

I am writing from a place of calm now, a mere 36 hours after revealing a tiny solder board of components I have now been rewarded with a the image of a giant raspberry on the Toshiba widescreen telly. No Dr Who or Eastenders Christmas Special in this house, everyone is going to have to endure me learning how to use linux and programming a strange cat on Scratch.

[GARD]

Just in case anyone else is still out there trying to get the tiny computer to take its first breath, I thought it might be useful to make a few notes of where I went wrong or where peripherals let me down and then more importantly, how I got over it and finally got my raspberry pi to boot.

Problem 1 – Raspberry Pi would not boot

My raspberry pi starter kit came with a pre-installed SD card with the NOOBs installation pack on it. When I put it into the slot and switched on the raspberry pi it rewarded me with a rainbow splash screen but went no further.

Without having access to a command prompt I had no alternative but to reboot through the brute force method of yanking out the power cable and re-inserting. This has a tendency to mess up the SD card and is not recommended but what are your choices in this situation?

Putting the power cable back in took me straight back to the rainbow splash screen.

I then discovered the power of the 10 minute wait. It seems the raspberry pi will not immediately reboot and requires a cool down period. Not the 15 second cool down so favoured by your IT helpdesk staff but a full on, mind bogglingly dull 10 minutes. No idea why, but I’ve seen a comment about capacitors and heat.

Learnings:

  • Plug power cable in after all the other components – the SD card goes in before you power up
  • If you need to reboot by pulling out the cable, you may as well go and make yourself a cup of tea (and drink it) before coming back to plug the cable in again

Problem 2: Raspberry Pi boots to NOOBs, successfully installs Raspbian but will not boot to Raspbian

After much re-booting and cooling down and re-booting I managed to get my system to boot into NOOBs. Here I was able to select the recommended Raspbian operating system. It even went as far as telling me I had successfully installed Raspbian before the screen went blue and then black.

It seems that the problem is with the HDMI connection. NOOBs is apparently rather good at dealing with a whole host of HDMI options (no idea what I’m talking about here) but Raspbian is not so good. So I get a display on my Toshiba Regza widescreen while I’m in NOOBs but nothing at all in Raspbian.

Joy.

Have another cup of tea and reboot.
[GARD]

This takes me to the splash screen where I have an opportunity (if I’m quick) to enter the Recovery Mode. This could be useful as it allows me to select Raspbian again and then press e to edit the config file. Here I can tell it to enter HDMI safe mode. I say “could be useful” but its not at all useful as it doesn’t seem to save the instruction and when I’ve made yet another cup of tea and rebooted I’m back in the same place with another blank screen.

On further investigation I discover that I can amend the config.txt file on another computer, but not it seems, a windows PC (as they won’t read linux files). The instructions I read suggested that you either need a linux machine or a Virtual Machine running on your PC so you can boot into a linux OS. I have experience with VM’s and I fear it is another route littered with painful hurdles. Still, as I have virtual box installed on my Mac, I thought I’d give it a go.

Installing RaspbianFortunately I noticed that when I inserted my SD card into the mac it mounted 2 drives, one of which was called boot. In there was the config.txt. Opening this with a text editor allowed me to remove the #comment in the first section so that:

#hdmi_drive=1
became
hdmi_drive=1

I saved this file and went back to the raspberry pi to reboot – joy of joys – it booted, ran some complex looking install script and finished with:

pi@raspberrypi ~ $

The Giant Raspberry of SuccessI wondered what I was supposed to do next but on referring to my manual, I discover that is it. I had finally reached my destination.

Learnings:

  • Insert your SD into a mac (or PC running linux in a virtual machine) open the Boot drive, open the config.txt and remove the # that comments out the instruction to boot in HDMI mode 1
  • Reboot and watch with satisfaction as a giant raspberry appears on the screen
  • Type startx if you want to see a more visual representation of raspberry pi success.