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. Zum Beispiel:
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 DesktopKitt für SSH-Terminal
VNC-Desktop.
Nun, da ich es angeschlossen hatte, brauchte ich ein Terminal, das eine sichere Shell (SSH) in den PI auf dem Desktop-Computer ausführt. Das Einrichten des Terminals war einfach darin, das populäre Putty-Programm herunterzuladen und zu leiten. Im Session-Bereich füllte ich die IP-Adresse des PI in die IP-Adresse, den Port 22 an und stellte sicher, dass SSH ausgewählt wurde. Das öffnete ein Terminalfenster mit einer Anmeldeaufforderung. Der Standard-Benutzername ist PI und das Passwort ist Himbeere. Natürlich wurde das erste, was ich tat, das Passwd-Dienstprogramm auszuführen, um das Passwort zu ändern.
Alternativ können Sie stattdessen ein grafisches Terminal mit VNC erhalten. Dies ist ein grafischer Desktop, der in einem Fenster auf Ihrem Desktop-Computer ausgeführt wird, aber ein Terminal in der PI ist.
Anschließen mittels Ethernet mit einem drahtlosen Router
Es dauerte nicht lange, nachdem ich mein Pi hatte, dass ich eine Entscheidung getroffen habe, zu einem WLAN-Router / Fibre-Modem-Combo zu wechseln. Als Bonus enthält dieses Upgrade auch mehrere Ethernet-Ports. Ich habe meinen Desktop-Computer eingerichtet, um drahtlos zu verwenden, aber ich konnte nicht dasselbe für den PI tun. Ich musste eine Konfiguration auf dem PI ausführen, um das WLAN eingerichtet zu lassen, und ich musste also mit Ethernet zuerst eine Verbindung herstellen.
Router-Netzwerkinformationen in einem Browser
Die Verbindung zum Router mit Ethernet war viel einfacher als die oben abgedeckte Direktverbindungsmethode, in der das Senden, dass kein Scaning beteiligt war. Sobald ich mit einem Ethernet-Kabel das PI angeschlossen hatte, habe ich einfach einen Browser auf meinem Desktop-Computer angerufen und die IP-Adresse des Routers in die URL-Leiste des Browsers angelegt. Dies ist eine Adresse wie 192.168.1.1 oder 192.168.2.1, aber mit http: // davor, obwohl einige Browser nicht das http: // benötigen. Überprüfen Sie Ihr Router / Modem-Handbuch.
Was Sie sehen, hängt von Ihrem Modem ab, aber meine zeigte unten einen Netzwerkdiagramm mit der IP-Adresse des PI, 192.168.2.14. Damit konnte ich ein Terminal mit Putty wie oben beschrieben einsetzen.
Wenn Sie Ethernet zwischen Ihrem PI und dem Modem verwenden möchten, dann sind Sie fertig. Aber ich wollte drahtlos gehen, damit meine Roboter ohne ein nachlaufendes Ethernet-Kabel herumwandern konnten. Nun, da ich ein Terminal in den Pi hatte, könnte ich zum nächsten Schritt fortfahren.
Drahtlos verbinden.
Das Raspberry Pi 3-Modell B verfügt über drahtlose eingebaute, aber wenn Sie eine Version ohne Wireless haben, können Sie auch einen drahtlosen Dongle erhalten, der sich an einen der USB-Anschlüsse der PI anschliegt. Bevor Sie sich drahtlos an den kopflosen Pi herstellen können, müssen Sie weiterhin mit einem der oben genannten Methoden mit einem der oben genannten Methoden herstellen und eine Konfiguration auf dem PI.
Ich habe ein Terminal angeregt und wie oben beschrieben angemeldet. Ich musste meinen drahtlosen Netzwerknamen und Ihr Netzwerkkennwort in die Konfigurationsdatei /etc/wpa_supplicant/wpa_supplicant.conf setzen. Aus Sicherheitsgründen wollte ich jedoch nicht, dass das Passwort in einem einfachen Text ist.
Verwenden von WPA_PASSPRASE und Editing WPA_SUPPLICANT.CONF
Bearbeiten wpa_supplicant.conf.
Also rannte ich wpa_passwanne “bobsnetname” “bobsnetpassword” und markierte den Ausgang, der in diesem Terminal den hervorgehobenen Text in die Zwischenablage kopiert. “BOBSNETNAME” und “BOBSNETPASSWORD” sind natürlich fiktiv.
Ich habe dann /etc/wpa_supplicant/wpa_supplicant.conf bearbeitet und mit der rechten Maustaste aus der Zwischenablage eingefügt. Der eingefügte Text enthält noch eine Zeile mit dem Kennwort in einem klaren Text. Es wurde jedoch kommentiert, aber trotzdem sichtbar, also habe ich es gelöscht.
Nach dem Speichern und dem Beenden des Editors habe ich das Ethernet-Kabel getrennt. Ein laufendes Programm namens WPA-Supplicant hätte nur wenige Sekunden nach der Datei geändert haben, nachdem die Datei geändert wurde, um ihn zu bemerken und das WLAN-Gehen zu erhalten. Und so, nachdem ich ein paar Sekunden gewartet hatte, rannte ich google.com. Es funktionierte. Der PI wurde drahtlos verbunden.
Hatte es nicht so gewesen, hätte ich es manuell neu starten können, indem er sudo wpa_cli rekonstituiert läuft. Dies meldet auch den Schnittstellennamen, WLAN0 in meinem Fall.
Laufen ‘ifconfig wlan0’
Um es zusätzlich zu testen, und um die IP-Adresse des PI zu erhalten, rannte ich ifconfig WLAN0 und suchte nach der IP-Adresse neben dem Inet Addr. Es war da und damit konnte ich mich mit dem Pi verbinden und mit der Programmierroboter beginnen.
Das waren meine Erfahrungen in der Verbindung mit meinem Himbeer-Pi, aber wir wissen, dass es viele Himbeer-PI-Benutzer auf Hackaday gibt. Welche Methoden haben Sie verwendet, welche Fallen haben Sie gefunden und welche Ideen haben Sie, um aus ihnen herauszukommen? Teilen Sie uns in den Kommentaren mit, damit andere auch nicht in sie fallen.