A robot which is permanently hooked up to a monitor, keyboard, mouse and mains power, is not very cool. In fact, it will look plain silly. You'd have to chase it around the house to type stuff in, and the power cable to the mains socket would have to be over 784 meters long*.
*Figure grossly exaggerated for dramatic effect.
That's why remote control is the future. No, put down that TV remote you use to watch Lego Movie for the hundredth time. We are going to battery power Rosie's brain, and log into it remotely from another computer - over Wi-Fi. And as you have already learnt here, the insides of Rosie's brain do not look like how you were expecting (a wondrous kingdom of castles, dragons, unicorns and sweet shops are no where to be found).
You will need to have these:
- Computer (we are using a Windows 10 laptop). Steps will be identical for Macs, except you can skip the use of Putty and simply launch 'Terminal.app'.
- Raspberry Pi 3 running Raspbian on SDHC card, with power supply
- USB keyboard and mouse
- Monitor (with HDMI connection), and a HDMI cable
- Rechargeable USB battery pack
You will need to have done these things:
You will need to do this:
- Connect your Pi to a USB battery pack if you want it to be truly mobile
- Install Putty (https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) on your Windows computer. You can skip this step if using a Mac.
- Enable and configure Wi-Fi on your Pi
- Note down IP address of your Pi
- Set password for 'pi' user
- Enable Secure Shell (SSH)
- Remotely connect to your Pi using SSH, from your computer using Putty. Terminal.app can be used if using a Mac.
What do you get after all this?Sorry to disappoint, but supplying power is the easy bit. Buy / borrow / DO NOT STEAL a rechargeable battery pack (the type you use to charge your phone on an expedition counting exotic raccoons in a forest). Then charge it, attach it to your Pi using the built-in micro USB port and your Pi will remain powered for hours. Power - ticked.
But to log into your Pi remotely, there are a few other things that come in to play. Plenty of interesting technology is involved too.
- First of all, the Pi needs to be on 'the network'. This will be our home network, complete with Wi-Fi and Internet. Just like your computers, mobile phones, robotic pet hamster, and pretty much every other device your family has bought over the last few years, Rosie will be invited to use our home Wi-Fi network. Watch how many Netflix shows you stream, Rosie!
- SSH stands for Secure Shell. And it's used everywhere by everyone in the IT industry to access Linux-based devices remotely - securely - over a network. The connection is encrypted, and it will allow you to remotely log in to the Pi over the Wi-Fi connection and run commands as if you were directly connected to it.
This is simply too much detail:If you are using a Windows computer, you will need to install Putty (https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) on the computer from which you will be managing the Pi. This is because unlike other Operating Systems, for example a Mac running iOS, Windows has no default program that can be used to start a SSH connection. Once downloaded, installed and launched, Putty looks like this:
Everyone uses Putty so you might as well.
Now all other steps need to take place on the Pi. So let's leave our computer alone, and return to the clunky keyboard we still have attached to the Pi. It's time to boldly embark on our trip for Remote Access using Secure SHell (or project t-r-a-s-h for short).
Return to the Pi terminal. We want to run a Linux command.
ip addr show...checks what IP details the Pi has.
Slow down you say. Why are we checking for intoxicated pandas, or indignant ponies? It is because IP actually stands for something a lot more boring: Internet Protocol. It is how devices talk to each other over modern networks. Over your home network. Over your school network. Even across continents, over the Internet. And specifically, IP addresses are used to identify a unique device on a network. Bit like someone's home address, or telephone number. So the Pi needs to have an IP address on your home network in order for us to communicate with it, remotely.
Does your Pi have it? Of course not. These devices don't magically configure themselves you know! Hidden in the output of ip addr show command should be 'inet' followed by an IP address (in the format X.X.X.X) under the 'wlan0' entry. The w in wlan0 stands for wireless, hence this is what we're looking at.
If you must look outside your terminal, there is also an icon in the top right that suspiciously looks like it's politely telling you the same thing:- 'sorry sir / madam, there appears to be no connection tonight'.
Now before you take it too personally - about your Pi not having an IP address - take a few breaths and let's get it onto your home network. Like when you connected your cute animatronic ferret to the Wi-Fi network when it arrived from Amazon, you will need to have a few details at hand:
- Wi-Fi network name, sometimes called SSID
- Wi-Fi network password
- Encryption protocol (they have names like WPA-PSK, and WEP
Take a few deep breaths. Things are about to get a little complicated. And it is also a little self-inflicted, as we want to stick to using just commands in the terminal. Armed with Wi-Fi details, we need to edit a file to let Pi know about this network, and to join it after it next restarts. Here are a few more Linux commands:
cd /etc/wpa_supplicant/...move to the directory (/etc/wpa_supplicant/) which contains the file with Wi-Fi configurations.
ls...do you see the 'wpa_supplicant.conf' file listed?
sudo nano wpa_supplicant.conf...found it? Good, let's edit it.
Now, what's with 'sudo' and 'nano'? Firstly, 'nano' is a Linux text editor. We need it to edit files. But 'sudo' is a little bit more complicated to explain. All you need to know for now is that we have to use it at the beginning of a command when running commands that require more privileges. It temporarily gives you more power to do stuff than you normally would have. You know, you can't trust everyone to do everything these days you know.
The nano screen now allows you to edit the wpa_supplicant.conf file.
Move around using arrow keys, and enter text using your keyboard. We want to enter our Wi-Fi details, like this:
Once you're happy with the lines you've added, save the file and exit nano using control-x (control and x keys pressed together), 'y' key at next prompt and 'enter' when confirming the filename.
Last step is to restart the Pi. Back in the terminal, simply type reboot.
reboot...sometimes machines just need a gentle reboot.
Pi will go dead. And stay dead. Until it starts up again. Phew. Thought we'd just lost you for a second there.
Now let's check for inglorious prunes and incontinent parrots again. Actually, let's just check to see if we now have an IP address.
ip addr show...checks what IP details the Pi now has.
Yes, it looks like it! In this case, it is 192.168.200.8. But who gave it its IP address? Most likely, it was your home broadband router. Dynamic Host Configuration Protocol (DHCP) is almost always configured on a home broadband router, and is used to assign out IP addresses to those who request it - like to your furry bionic rodent now biting at your ankles.
Make sure you note down the IP address you see under the 'wlan0' entry. You will need it, to connect to your Pi.
The icon in the top right (outside of the command terminal) now suspiciously looks like it's telling you that the Pi has a connection. It's either that, or it's warning you of a man with mountainous hair.
This is actually a momentous moment. Rosie's brain can now talk to the world around it. The world can talk to her. She is connected. Go and get a drink, watch the Lego Movie for the hundred and first time, and come back refreshed so that we can wrap up project trash.
Refreshed? Good. Now that Pi is on the home network wirelessly, we want to make sure Rosie will be secure, and that she can't be hacked to roam around the house causing mischief (like photographing credit cards in people's wallets and using them to buy Justin Bieber tickets). We definitely don't want that. So let's set a password.
whoami...shows you the user account you are currently logged in as, which should be 'pi'.
sudo passwd pi...allows you to change the 'pi' user account password. Choose something secure or Rosie will be off to see Justin!
Always set a complex password. One that isn't easily guessable. Once you enable SSH, your beloved Pi is available on the network. Anyone who can guess your password can login to it, and cause lots of mischief.
Now, do you remember SSH? It's the 'sh' in project trash. Secure Shell. And we need to enable it.
sudo raspi-config...launches the Raspberry Pi Software Configuration Tool.
It looks a little like this:
Choose '5 Interfacing Options', then 'P2 SSH', then 'Yes' when asked 'Would you like the SSH server to be enabled'? Of course we do, otherwise we simply wouldn't be here.
And now the real fun begins. We can log into the Pi - securely - over Wi-Fi, using the password we set earlier.
Go back onto the computer on which you installed Putty. Enter the IP address of the Pi you recorded earlier in the 'Host Name' field in Putty. You don't need to touch any other setting.
When you press 'Open' you are likely to encounter a very scary error message. This is because we've never connected to this Pi remotely before from this computer. We know it's Rosie. We trust Rosie. So we can respond 'Yes' and trust this device in the future.
All this work and what do you see? Actually, the very same thing you saw earlier when you were on the Pi, except there is no beautiful desktop now. Just the dull looking terminal, waiting for someone to type something in.
We can now finally say bye bye to the keyboard, mouse and monitor attached to the Pi. Rosie's brain is now self-contained. And battery powered. We can control it from anywhere in the house. And it might just fit in a plastic box, ready to be moved around on wheels.
Update - August 2019Wow, has it really been 2 years since we first wrote this post? How time flies when we are having fun!
...And in terms of connecting a Raspberry Pi running Raspbian OS to a Wi-Fi network, it turns out that very little has changed over this time.
However, we've had occasions when we needed to have our Pi's Wi-Fi configuration support more than one network. For example, what if we visit the office, or use our smartphone as a hotspot?
No big deal. We simply add another network entry in our file.
We have also come across a more sophisticated tool - wicd - to do everything Wi-Fi more interactively. It's possible to install the package like this:
sudo apt-get install wicd-curses sudo wicd-curses
Is it a little more exciting to see the list of all possible networks before we connect to one? We suppose it is. For our simple purposes, however, editing the text file served us just fine, a majority of the time.