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.

Update - August 2019

Wow, 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.


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

MOST VISITED (APPARENTLY)

LoRa-Wan Kenobi

In the regurgitated words of Michael BublĂ©: It's a new dawn .  It's a new day .  It's a new Star Wars film .  For me .  And I'm (George Lucas, and I'm) feeling good .  Unfortunately for Canadian Mike, the Grammy that year was won by the novelty disco classic with the famous refrain: We love IoT, even in Planet Tatooine * . *Not true. Clearly, the Star Wars producers didn't sincerely mean the last Jedi the previous time around.  Return of the Jedi, released during the decade that spearheaded cultural renaissance 2.0 with the mullet and hair-metal , was less economic with the truth.  Either way, we're going to take inspiration from the impressive longevity of the money-spinning space-opera and reboot our franchise with some Jedi mind tricks.  Except this particular flick doesn't require an ever-growing cast of unrecognisable characters, unless ASCII or UTF counts.  In place of an ensemble gathering of Hollywood stars and starlets, we will b

Battle of BLEtain

The trolling . The doxing . An army of perplexing emojis. And endless links to the same - supposedly funny - viral video of a cat confusing a reflection from a dangling key for a golden hamster, while taking part in the mice bucket challenge. Has social media really been this immense force for good? Has it actually contributed significantly to the continued enlightenment of the human (or feline) race? In order to answer these poignant existential questions about the role of prominent platforms such as Critter, StinkedIn and Binterest, employing exceptional scientific rigour equal to that demonstrated by Theranos , we're going to set up a ground-breaking experiment using the Bluetooth Low Energy feature of MicroPython v1.12, and two ESP32 development boards with inexplicable hatred for one another.  And let them hurl quintessentially British expressions (others call them abuse) at each other like two Wiltshire residents who have had their internet access curbed by the co

Hard grapht

You would all be forgiven for assuming that bar , pie and queue line are favourite pastimes of the British .  Yet, in fact – yes, we did learn this back in GCSE maths – they are also mechanisms through which meaningless, mundane data of suspect origin can be given a Gok Wan -grade makeover, with the prime objective of padding out biblical 187-page PowerPoint presentations and 871-page Word reports (*other Microsoft productivity tools are available).  In other words, documents that nobody has the intention of ever reading.  But it becomes apparent over the years; this is perhaps the one skill which serves you well for a lifetime in certain careers.  In sales.  Consultancy.  Politics.  Or any other profession in which the only known entry requirement is the ability to chat loudly over a whizzy graph of dubious quality and value, preferably while frantically waving your arms around. Nevertheless, we are acutely conscious of the fact that we have spent an inordinate amount