Skip to main content

Why-Fi-ght the Wi-Fi?


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:

  1. I’ve got Pi(3) brain

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.
  1. 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!
  2. 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.
Both things together, we can control Rosie remotely from our computer (only while in the house), without having to connect a keyboard, mouse, monitor or mains power to it.  Truly remotely controlled?  We're certainly getting closer!

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
These details are often found on the broadband router you have at home, either externally on a sticker, or available to view once you log into the router itself using its admin page.

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.



Comments

  1. Have you seen temporary pauses, of 1-5 seconds, in your SSH sessions? These pauses have been haunting my bot for years (under Wheezy, Jessie and Stretch). I "Don't Panic", but I wonder if [my] Raspberry Pies suffer freezer burn.

    ReplyDelete
    Replies
    1. Hello! SSH connections have generally been very reliable for us over Wi-Fi. Any intermittent Pi "freezes" (actually reboots) were a result of drawing too much power from the Pi's GPIO pins. Which is why we now prefer to externally power connected devices, such as motors, using batteries or mains. Have fun!

      Delete

Post a Comment

Popular posts from this blog

Tea minus 30

We're fast approaching Christmas time.  And if robots were to make one simple observation about the human species during the Christmas festivities, it's that they watch a lot of TV.  A LOT.  Often, accompanied by an inappropriate amount of greenhouse gas-producing food.  Stuff you don't normally eat during the remainder of the year - for good reason.

And most so-called shows on TV are boring to robots like Rosie.  After all, why watch a minor subspecies of the human race - celebrities - stumble awkwardly around the dance floor, dressed like a faulty, sparking circuit board?  Such branch of entertainment doesn't require robots to engage any of their proud circuitry.  Their processors remain idle.  Memory under-utilised.

But if robots are to be part of people's homes (and blend in), they need to look at least a little interested in some of this irrational nonsense.  Nobody likes a party pooper.  A killjoy.  And this is where a certain subgenre of TV entertainment co…

Beam me up, Rosie!

How do you get from A to B?

You can't, as As and Bs are just letters in the alphabet. But if A is your house, and B is a meerkat village at your favourite safari park, you'd probably use a device equipped with GPS.  Not to be confused with UPS, who will deliver you your chosen meerkat through the post. And why on Earth would Rosie Patrol need one? Precisely, it's because she is on Earth that she needs one. Because our planet is rather big. Big enough to get lost in. And we don't want to lose our friendly plastic boxes on wheels. And maybe, eventually when she's clever enough, she'll go and defeat baddies on her own. And return home afterwards for a well deserved Earl Grey tea.

Besides, why wouldn't we want to add another three letter acronym to Rosie Patrol's repertoire?
All superheroes need:One Raspberry Pi 3, running Raspbian OSComputer from which you are connecting to the Raspberry Pi Probably the most important bit: a GPS receiver thingmy. …

Raspberry bye, hello

Let us make this very clear from the onset of this exotic excursion.

This is not a case of Raspberry Bye. Our relationship with our favourite single-board computer hasn't at all soured. In fact, we've become wholly inseparable. There's been many months of undeniable fun that's been had with the venerable computer strangely named after an edible fruit. To the extent that our relationship requires a healthy break. And quite frankly, our Pis require a well earned summer holiday to do whatever it is that robots and computers do during their time off. Crash. Burn. Refuel (with questionable toxins). Not at all unlike their human counterparts. And ultimately, it would be nice if they could return to a brand new, adorable pet waiting for them at home, a likeable little companion that they can just get along with.

Well, we visited a pet shop, but couldn't find anything as small and smart as this adorable pup we stumbled up on while searching the Internet for a new, miniat…