Reader small image

You're reading from  Learn Robotics Programming - Second Edition

Product typeBook
Published inFeb 2021
PublisherPackt
ISBN-139781839218804
Edition2nd Edition
Concepts
Right arrow
Author (1)
Danny Staple
Danny Staple
author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple

Right arrow

Chapter 4: Preparing a Headless Raspberry Pi for a Robot

In this chapter, you will learn why the Raspberry Pi controller on a robot should be wireless and headless, what headless means, and why it's useful in robotics. You will see how to set up a Raspberry Pi directly as a headless device, and how to connect to this Raspberry Pi once on the network, and then send your first instructions to it. By the end of the chapter, you will have your own ready-to-use Raspberry Pi without needing to connect a screen, keyboard, or wired network to it, so it can be mobile in a robot.

We'll cover the following topics in this chapter:

  • What is a headless system and why is it useful in a robot?
  • Setting up Wi-Fi on the Raspberry Pi and enabling SSH
  • Finding your Raspberry Pi on the network
  • Using PuTTY or SSH to connect to your Raspberry Pi
  • Configuring Raspberry Pi OS

Technical requirements

To complete the exercises in this chapter, you will require the following:

  • A Raspberry Pi, preferably a 3A+ (but a Pi 3 or 4 will do)
  • A USB power supply capable of 2.1 amps with a Micro-USB cable
  • The MicroSD card you prepared in the previous chapter
  • A Windows, Linux, or macOS computer connected to the internet and able to read/write to SD cards
  • A text editor on your computer – VS Code is a suitable multiplatform option
  • PuTTY software on Windows (SSH software is already available on Mac and Linux desktops)

The GitHub link for the code is as follows:

https://github.com/PacktPublishing/Learn-Robotics-Programming-Second-Edition/tree/master/chapter4

Check out the following video to see the Code in Action: https://bit.ly/3bErI1I

What is a headless system, and why is it useful in a robot?

A headless system is a computer designed to be operated from another computer via a network, at times or in places where keyboard, screen, and mouse access to a device is inconvenient. Headless access is used for server systems, for building robots and making gadgets:

Figure 4.1 – A Raspberry Pi tethered to a screen, keyboard, and mouse

Figure 4.1 shows a system with a head where a user can sit in front of the device. You need to attach a screen, keyboard, and mouse to your robot, and hence it is not very mobile. You may be able to attach/detach them as required, but this is also inconvenient. There are portable systems designed to dock with Raspberry Pis like this, but when a robot moves, you'll need to disconnect it or move with the robot.

At some events, I have seen robots with tiny onboard screens, controlled by a wireless keyboard and mouse. However, in this book, we use a...

Setting up Wi-Fi on the Raspberry Pi and enabling SSH

Now you've seen what you get with a headless system, let's modify the SD card so the Raspberry Pi starts up ready to use as a headless device. We need to set up Wi-Fi first:

  1. Remove and reinsert the MicroSD card we made earlier into your computer so that the computer can recognize the new state of the drive.
  2. Now you will see the card shows up as two disk drives. One of the drives is called boot; Windows will ask whether you want to format the other drive. Click Cancel when Windows asks you. This part of the SD card holds a Linux-specific filesystem that Windows cannot read.
  3. Now, in boot, create two files as follows. I suggest using an editor such as VSCode for plain text files, seeing file extensions, and making empty files:
    • ssh: An empty file with no extension.
    • wpa_supplicant.conf: This file contains your Wi-Fi network configuration as shown here:
      country=GB
      update_config=1
      ctrl_interface=/var/run/wpa_supplicant...

Finding your Pi on the network

Assuming your SSID and PSK are correct, your Raspberry Pi is now registered on your Wi-Fi network. However, now you need to find it. The Raspberry Pi uses dynamic addresses (DHCP). Every time you connect it to your network, it may get a different address. Visiting the admin page on your Wi-Fi router and writing down the IP address works in the short term. Doing that every time the address changes is frustrating, and may not be available in some situations.

Luckily, the Raspberry Pi uses a technology known as mDNS (Multicast Domain Name System), so nearby computers can find it. A client computer will broadcast a local message to ask for devices with the name raspberrypi.local, and the Raspberry Pi will respond with the address to find it. This is also known by the names Zeroconf and Bonjour. So, the first thing you'll need to do is ensure your computer can do this.

If you are using macOS, your computer will already be running the Bonjour software...

Using PuTTY or SSH to connect to your Raspberry Pi

Earlier, we added a file to our Raspberry Pi boot named ssh. This activates the SSH service on the Pi. As mentioned before, SSH is an abbreviation for secure shell, intended for secure network access. In this case, we are not specifically targeting the secure encryption capabilities, but are using the remote networking capability to send instructions and files to and from the Raspberry Pi without having physical access to it.

Important note

If you already use an SSH client, note that not all of the Windows command-line SSH clients support mDNS.

PuTTY is a handy tool for accessing SSH and is available for Windows, Linux, and Mac. Its installation information for these operating systems can be found at https://www.ssh.com/ssh/putty/.

Once you have PuTTY installed from the preceding link, let's get it connected to your Raspberry Pi. Follow along:

  1. Start PuTTY. You will see a screen like in Figure 4.3. In the Host...

Configuring Raspberry Pi OS

Now we are connected, let's do a few things to prepare the Raspberry Pi for use, such as changing the user password and changing the hostname to make the Pi more secure.

We can perform many of these tasks with the raspi-config tool, a menu system to perform configuration tasks on Raspberry Pi OS. We start it with another tool, sudo, which runs raspi-config as root, a master user. Refer to the following command:

sudo raspi-config

The raspi-config interface will appear, as shown in Figure 4.5:

Figure 4.5 – The raspi-config tool

Now we've accessed raspi-config, we can use it to change some of the settings on the Raspberry Pi.

Renaming your Pi

Every fresh Raspberry Pi image is called raspberrypi. If there is more than one of those in a room, your computer will not be able to find yours. It's time to think of a name. For now, we'll use myrobot, but I am sure you can think of something better...

Summary

In this chapter, you've seen how to free a Raspberry Pi from a screen and keyboard by making it headless. You set up an SD card to connect to your Wi-Fi and to enable SSH so you could connect to it. You've used raspi-config to personalize your Pi and secure it with your own password. You then made the first small steps in looking around the Linux system it has running on it. You also ensured the Raspberry Pi is up to date and running the most current software. Finally, we saw how to safely put the Pi into shutdown mode, so that filesystem damage does not occur when you unplug it.

You have now learned how to make a Raspberry Pi headless. You have seen how to keep it upgraded and connected to your network and the Pi is ready to start building with. You can use this to build Raspberry Pi-powered gadgets, including robots.

In the next chapter, we look at ensuring you don't lose valuable code or configuration when things go wrong. We will learn about what can...

Assessment

  1. What other gadgets or projects could you build with a headless Raspberry Pi?
  2. Try giving your Raspberry Pi a different hostname and connecting to this locally with PuTTY and mDNS.
  3. Try other Linux commands on the Raspberry Pi, such as cd, ls, cat, and man.
  4. Shut down the Raspberry Pi correctly after trying these.

Further reading

Please refer to the following to get more information:

  • Internet of Things with Raspberry Pi 3, Maneesh Rao, Packt Publishing: This book uses a wired headless Raspberry Pi for the demonstrations and experiments in it.
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Robotics Programming - Second Edition
Published in: Feb 2021Publisher: PacktISBN-13: 9781839218804
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple