Book Image

Hands-On Robotics with JavaScript

By : Kassandra Perch
Book Image

Hands-On Robotics with JavaScript

By: Kassandra Perch

Overview of this book

JavaScript has an effective set of frameworks and libraries that provide support for embedded device programming and the robotics ecosystem. You’ll be able to put your JavaScript knowledge to work with this practical robotics guide. The book starts by guiding you in setting up an environment to program robots with JavaScript and Rasberry Pi 3. You will build beginner-level projects, such as a line-following robot, and then upgrade your robotics skills with a series of projects that help you get to grips with the Johnny-Five library. As you progress, you’ll learn how you can improve your projects by enabling advanced hardware components and programming concepts. You’ll even build an advanced AI-enabled robot, connect its NodeBots to the internet, create a NodeBots Swarm, and explore Message Queuing Telemetry Transport (MQTT). By the end of this book, you will have enhanced your robot programming skills by building a range of simple to complex projects.
Table of Contents (19 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

Installing the operating system


In order to get started with Johnny-Five and the Raspberry Pi, we will need to set up the Raspbian operating system by burning it to a microSD card. Then, we'll need to edit some files on the SD card in order for the Raspberry Pi to boot with Wi-Fi and the ability to SSH in. Lastly, we'll need to boot up the Raspberry Pi and get some settings in place before finally installing Node.js, Johnny-Five, and Raspi-IO.

Downloading Raspbian Lite

The following steps will show you how to download Raspbian Lite:

  1. The first step is downloading the Raspbian Lite image so we can burn it to our microSD card. The best place to get the image from is https://www.raspberrypi.org/downloads/raspbian/, as shown in the following screenshot:

A screenshot of the Raspbian download page, with both Full and Lite download links

  1. Select RASPBIAN STRETCH LITE (or whichever version is the current one), which will replace the word STRETCH. Give yourself some time for this step to complete; although Raspbian Lite is much smaller than Raspbian Full, it is still several hundred megabytes, and can take time to download!

Note

If you're preparing to run a class, hackathon, or some other event using the Raspberry Pi and Raspbian, it's best to predownload it and place it on a flash drive to hand around, as conference and event Wi-Fi can be a bit slower than normal, or even drop in and out, so be prepared!

Burning the image to an SD card

Luckily, the tools for burning OS images to SD cards have evolved arcane command-line tools that can overwrite your computer's hard drive as easily as it can the SD card. My current favorite is called Etcher, and it can be downloaded for any platform at https://etcher.io/, as shown in the following steps:

  1. The free version is more than enough for our needs, so download and install it as you download Raspbian Lite.
  2. Once they are both downloaded, you'll want to place the micro SD card in your computer, either by placing it into a full-sized SD adapter and then into a slot on your computer, or by using a USB-to-micro-SD adapter. Whichever you use, make sure your computer can see the volume as a drive before continuing. Then, boot up Etcher. The following screenshot shows Etcher running on a Mac:

A screenshot of the Etcher program running on a Mac

  1. Once you see a window similar to the preceding screenshot, you'll need to select the Raspbian Lite image you just downloaded. You don't even need to unzip the .zip file—Etcher can handle that outright! Once you've selected the image, Etcher should select your micro SD card drive, so long as your machine can see it! Once you've ensured that the image and micro SD card drive are properly selected, hit Flash! to begin the process.

Note

Sometimes, with larger micro SD cards, you'll get a warning from Etcher about the drive being very large (this happens to me when I use 64 GB cards). This is to prevent you from overwriting your computer's hard drive. You can bypass the warning by going through a confirmation window—just be absolutely sure that your micro SD card drive is selected first!

A few minutes will pass as Etcher burns the image to your micro SD card, then verifies that it is present on the card. Once this is done, remove and reinsert the micro SD card so that your computer recognizes it as a drive again; the drive should be namedboot. We're not quite done editing the image files yet, so Etcher's polite attempt to eject the micro SD card drive needs to be ignored.

Editing files on the SD card

We need to edit and create some files on our Raspberry Pi's image in order to be able to access it with SSH when we turn on the Raspberry Pi. First, we'll set up the Wi-Fi using the following steps:

Note

If you're using an Ethernet cable and port to connect the Raspberry Pi to the internet, you can skip this step. If this doesn't get the Wi-Fi to work, you'll want to look at the information box under the section Booting up the Pi for troubleshooting steps and an alternative (if clunkier) way to set this up.

  1. In order to set up the Wi-Fi, you'll want to create a file in the root of the micro SD card drive called wpa_supplicant.conf that contains the following text:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="yourNetworkSSID"
    psk="yourNetworkPasswd"
}

 

 

  1. Replace yourNetworkSSID with the SSID of the Wi-Fi network you wish to connect to, and then replace yourNetworkPasswd with that Wi-Fi's network password.

Note

The Raspberry Pi's Wi-Fi chip can only connect to 2.4 GHz networks at the time of writing, so you need to make sure that you input a network that operates on that bandwidth or your Raspberry Pi will not be able to connect!

  1. After you've set up the Wi-Fi network, you'll want to tell the Raspberry Pi to allow you to SSH into it. To do this, you'll want to create a file called ssh in the same rootfolder as the wpa_supplicant.conf file. Make sure that the ssh file is empty and has no extension. When you're all done, the rootdirectory of the micro SD card drive will look similar to the following screenshot:

A list of files on the micro SD drive, once I've made my edits

Once this is all done, fully eject the microSD drive, take the microSD card and insert it into the Raspberry Pi. We're now ready to boot up the Raspberry Pi and start installing software.

Booting up the Pi

Once the micro SD card is inserted, plug your power source into your Raspberry Pi. A red and green LED should light up. The red LED should be solid—this is the power indicator. The green light will flicker on and off—this is the LED that indicates activity. The Raspberry Pi should take a maximum of a minute or so to boot up once you've plugged it in. Next, we'll SSH in. 

 

SSHing from a Linux or Mac

If you're on a Mac or Linux machine, you'll open up a Terminal and type the following:

ssh [email protected]

If you're successful, you'll see a question appear, asking about the authenticity of the host. Respond by typing yes and hitting Enter. You'll then be asked for a password, which is raspberry, as shown in the following screenshot: 

Successful SSH into a Raspberry Pi from a Mac Terminal

Once you've entered the password, you should see the following:

Successful login to a Raspberry Pi via SSH from a Mac

SSHing from Windows

In order to SSH from a Windows machine, you'll need to use a program called PuTTY. You can get it on https://putty.org/. But first, you'll want your Raspberry Pi's IP address. You'll need a monitor, an HDMI cable, and a USB keyboard. Once you have these, go through the following steps:

  1. Plug the monitor, HDMI cable, and USB keyboard into the Raspberry Pi before booting it up. Then plug in the power supply.
  2. When it prompts you for a username, type pi. When it asks for a password, enter raspberry. Once you are logged in, type ifconfig. You should see a lot of information appear.
  3. Look for the wlan0 section and the inet address under that heading. For the following output, the IP is 192.168.1.106, as shown in the following screenshot. Write this IP down. Then, you can unplug the display and the keyboard—you won't need them again:

Getting the IP address from the Terminal

  1. Once you have the IP address for your Raspberry Pi, you can boot up PuTTY. The window that opens is the configuration window, as shown in the following screenshot: 

The PuTTY configuration window

  1. Type the IP address that you obtained into the field labeled Host Name (or IP address) and click the Open button. You'll be asked about the authenticity of the host (only the first time you connect). Select Yes. Then enter Pi as the username and raspberry as the password when prompted. Once that's done, you should see the following:

Successful login to the Raspberry Pi with PuTTY

Now that everyone's logged in, let's set up our Raspberry Pi for our projects!