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 Node.js, Johnny-Five, and Raspi-IO


So, now that our Raspbian OS is installed and set up, it's time to install Node.js (which comes bundled with npm), Johnny-Five, and Raspi-IO!

Installing Node.js and npm

In days past, you would have to compile the Node.js source on your Pi, to varying degrees of success because of the nonexistence of binaries for the ARM processor that Raspberry Pi uses. Luckily now, because of a rousing amount of third-party support in the past few years, you can easily download the binary from the https://nodejs.org/en/ website! But how are we going to do this from the command line of our Raspberry Pi? 

 

Detecting your version of ARM processor

If you're using the Raspberry Pi 3 Model B recommended by this book, you're most likely on ARM v8 (the Raspberry Pi 3 original is ARMv7, which is fine too!). But you should always double-check (doubly so if you're using a different Raspberry Pi, such as the Pi Zero or Pi 2/1 series). To check the ARM version on your Raspberry Pi, run the following in your SSH Terminal:

uname -m

You'll see a return message that looks like armv#, where # is a number (possibly followed by a letter). That number is what is important, because that number tells us which Node.js binary we will need. Once you have your ARM version, go through the following steps:

  1. Head to the Node.js download page at https://nodejs.org/en/download/, as shown in the following screenshot:

A snapshot of the Node.js binary download page

  1. Right-click on the ARM version link you need and copy the URL. Then, run the following in your Raspberry Pi's SSH Terminal:
wget <binary-download-url>

 

 

 

 

Replace <binary-download-url> (carats, too!) with the URL you copied from the Node.js download website. Once it's downloaded, we need to extract the archive using the following code:

tar -xvf node-v****-linux-armv**.tar.xz
  1. The asterisks will differ depending on the current LTS version of Node.js and your ARM version. The Raspberry Pi will spit out a lot of filenames to the console, then give you back your shell prompt. This means that the binaries have been extracted into your home folder. We need to place them into the /usr/local folder. To do that, run the following:
cd node-v****-linux-armv** 
sudo mv ./lib/* /usr/local/lib
sudo mv ./share/* /usr/local/share
  1. This will move all of the precompiled binaries to their new homes on your Raspberry Pi. Once this is done, run the following:
node -v
npm -v

You should see something like the following:

Successful Node.js installation results

  1. If that's all well and good, you now have Node.js and npm installed! Let's wrap this up with Johnny-Five and Raspi-IO! Note that you can absolutely clean up the binary downloads by running the following:
cd ~
rm -rf node-v**-linux-armv**
rm -rf node-v****-linux-armv**.tar.xz

 

 

 

 

 

 

Note

Some of you with more Debian experience may be asking, well, why can't we just useapt-get? The short answer is that the package with the name node was taken a very long time ago, and because that is the case, and because sudo apt-get install nodejs is outdated (at the time of writing, using this command will install v4 when we needv8+, if it installs Node.js at all), we need to download the binaries and move them ourselves.

Installing Johnny-Five and Raspi-IO

To install Johnny-Five, once you've made sure Node.js and npm are installed, run the following command:

npm i -g johnny-five raspi-io

This installs the libraries globally; you won't have to reinstall it every new project. And that's it! You're ready to start developing Node.js robotics projects on the Raspberry Pi with Johnny-Five!