NETWORKING: PIN THE TAIL ON THE HEADLESS RASPBERRY PI

eager to get deeper into robotics after dipping my toe in the water with my BB-8 droid, I purchased a Raspberry Pi 3 model B. The first step was to connect to it. but while it has built-in 802.11n wireless, I at first didn’t have a wireless access point, though I eventually did get one. That indicated I went through different ways of finding it and connecting to it with my desktop computer. definitely there are others seeking to do the same so let’s take a look at the secret incantations used to connect a Pi to a computer directly, and indirectly.

Why connect to the Pi from my desktop computer? After all, there are small monitors and keyboards that hackers frequently use to make Pi laptops and Pi tablets. Since I was going to be embedding the Pi in various robots,  I saw no need to purchase a separate monitor and keyboard for it, but I did look a little into doing so.

My desktop’s monitor uses a VGA connector but the VGA-to-HDMI adapter I gotten along with the Pi didn’t work. Also, my desktop’s ancient KeyTronic keyboard uses a PS/2 connector so that wasn’t usable either. though PS/2-to-USB adaptors do exist, my research showed that I’d need the one that has USB smarts in it, as my keyboard predates USB and has no such electronics of its own. (But it does have sweet, deep tactile keys!) My Pi would remain headless, and I’d program it by connecting to it from my desktop computer.

Connecting directly by means of Ethernet Cable

My home modem was also ancient, having no wireless. It also had only one Ethernet port. That indicated I could connect my desktop computer either to the modem or to the Raspberry Pi but not both at the same time. So on unboxing the Pi, the only way to connect it to my computer was to unplug the Ethernet cord from the modem and plug it into the Pi instead. That indicated no Internet access while working with the Pi, hardly ideal, but good enough to see if the Pi worked at all.

But for my computer to speak to the Pi, I needed the Pi’s IP address. For those not familiar with IP addresses, they consist of 4 numbers ranging from 0 to 255 separated by dots. De exemplu:

169.254.95.208

It’s really a 32-bit number but it’s written that way for readability.

Network and Sharing center in Windows

IP address and subnet mask

Going to the Windows control panel on my desktop and then the Network and Sharing Center, I could see that a network now existed.

Clicking on view status and going deeper I found some beneficial information. The first was the IP address of the desktop computer, 169.254.95.208. The IPv4 tells me that it’s using Internet Protocol version 4. The IP addresses in IPv6 differ at least in that they’re four times as long.

Further, the presence of the IPv4 Subnet Mask, 255.255.0.0 told me that a subnet, or subnetwork, had been formed between the two machines. A subnet is a group of machines making up a smaller network that may or may not be a part of a larger one. Their IP addresses all start with the same binary values, though just how lots of bits at the beginning they have in common varies. how do you know which bits are common?

Using the subnet mask
That’s where the subnet mask comes in. To find out which bits, take the subnet mask and do a bitwise and with the IP address of a maker on that subnet. As the diagram shows, the result in my case was 169.254.0.0, indicating that the Pi’s IP address must start with 169.254. another way of writing the subnet mask is in CIDR notation. In CIDR notation the IP address is followed by a / and then the number of shared bits. In this case it would be 169.254.0.0/16. The last two numbers are zeros but they need not be considering that the 16 tells you what to keep. This CIDR notation will come in helpful shortly.

Though the 169.254 was shared, that still left 65,536 (256×256) possible combinations for the two remaining numbers. To scan the network, going through every possible combination of numbers, I used nmap, downloadable from nmap.org.

‘nmap -sn’ scan results
As shown in the snapshot, I ran it in a cygwin window though there are GUI versions available too. I used the following command line.

nmap -sn 169.254.0.0/16

Note that the subnet mask is given using CIDR notation. The section about the Raspberry Pi that you see in the snapshot appeared after around 10 minutes, indicating that it took that long to scan from 169.254.0.0 to 169.254.37.77, the Pi’s IP address. even though that was what I was looking for, I left it going anyway and the full scan took just over forty-four minutes. during that time it also found my desktop computer, the host from which it was doing the scan. had the Pi’s IP address been 169.254.255.255 then it would have taken the full forty-four minutes before finding it.

The Pi’s IP address continued to be 169.254.37.77 across shutdowns and so scanning again was not necessary.

Starting Up A terminal and VNC DesktopPuTTY for SSH terminal

VNC desktop

Now that I had it connected, I needed a terminal running a safe shell (SSH) into the Pi on the desktop computer. Setting up the terminal was simply a matter of downloading the popular PuTTY program and running it. In the Session section, I filled in the Pi’s IP address, port 22, and made sure SSH was selected. That opened up a terminal window with a login prompt. The default username is pi and the password is raspberry. Of course the first thing I did was run the passwd utility to change the password.

Alternatively you can get a graphical terminal instead using VNC. This is a graphical desktop that runs in a window on you desktop computer but is really a terminal into the Pi.

Connecting by means of Ethernet using A Wireless Router

It wasn’t long after getting my Pi that I made a decision to switch to a wireless router / fiber modem combo. As a bonus, this upgrade also includes multiple Ethernet ports. I set up my desktop computer to use wireless, but I couldn’t do the same for the Pi just yet. I had to do some configuration on the Pi to set up the wireless, and so I had to connect using Ethernet first.

Router network information in a browser
Connecting to the router with Ethernet was far much easier than the direct-connection method covered above considering that no scanning was involved. Once I’d connected the Pi using an Ethernet cable, I simply brought up a browser on my desktop computer and typed the router’s IP address into the browser’s URL bar at the top. This is an address like 192.168.1.1 or 192.168.2.1, but with http:// in front of it, though some browsers don’t need the http://. check your router/modem manual.

What you see depends on your modem, but mine displayed a network graph at the bottom with the Pi’s IP address, 192.168.2.14. with that I was able to bring up a terminal using PuTTY as described above.

If you want to remain using Ethernet between your Pi and the modem then you’re done. but I wanted to go wireless so that my robots could wander around without a trailing Ethernet cable. now that I had a terminal into the Pi I could go on to the next step.

Connecting Wirelessly

The Raspberry Pi 3 model B has wireless built-in, but if you have a version without wireless then you can also get a wireless dongle that plugs into one of the Pi’s USB ports. Before you can connect to the headless Pi wirelessly, you’ll still need to connect to it using one of the above methods first, and do some configuration on the Pi.

I brought up a terminal and logged in as described above. I needed to put my wireless network name and network password into the configuration file /etc/wpa_supplicant/wpa_supplicant.conf.  But for safety reasons, I didn’t want the password to be in plain text.

Using wpa_passphrase and editing wpa_supplicant.conf
Editing wpa_supplicant.conf
So I ran wpa_passphrase “bobsnetname” “bobsnetpassword” and highlighted the output, which in this terminal, copies the highlighted text to the clipboard. “bobsnetname” and “bobsnetpassword” are of course fictitious.

I then edited /etc/wpa_supplicant/wpa_supplicant.conf and pasted from the clipboard by right-clicking. The pasted text still included a line with the password in clear text. It was commented out but nonetheless, visible, so I deleted it.

After saving, and exiting the editor I disconnected the Ethernet cable. A running program called wpa-supplicant should have taken only a few seconds after the file was changed to notice it and get the wireless going. And so after waiting a few seconds I ran ping google.com. A mers. The Pi was connected wirelessly.

Had it not been then I could have restarted it manually by running sudo wpa_cli reconfigure. This also reports the interface name, wlan0 in my case.

Running ‘ifconfig wlan0’
To additionally test it, and to get the Pi’s IP address, I ran ifconfig wlan0 and looked for the IP address beside the inet addr. It was there and with it I could connect to the Pi and start programming robots.

Those were my experiences in connecting to my Raspberry Pi, but we know that there are lots of Raspberry Pi users on Hackaday. What methods have you used, what traps have you found and what ideas do you have for getting out of them? let us know in the comments so that others don’t fall into them too.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post