Talking to Robots using just JavaScript!

Roy Derks (@gethackteam)
Hackteam
Published in
4 min readNov 28, 2018

Recently I gave a lightning talk at dotJS 2018 — which is a great conference about the latest developments in the world of JavaScript — another person attending was the interesting Devon Lindsey talking about robots. Robots?! Yes, robots — which where running on JavaScript! This motivated me to do some online research that led me to the Johnny-Five project, a JavaScript Robotics and IoT-platform maintained by a community of passionate software developers and hardware engineers.

Over 75 developers have made contributions towards building a robust, extensible and composable ecosystem — Johnny-Five

As it happened there is a lot of information to be found only about this framework, also in the form of tutorials and (e)books. One of them that especially caught my eye was Hands-On Robotics with JavaScript by Kassandra Perch. This book gives you all the basics to get started with Johnny-Five and create software for robots with just plain-old JavaScript!

Johnny-Five platform

As said before the Johnny-Five framework can be used to write JavaScript for Robotics and IoT hardware. The framework is backed by a strong community of over 75 developers that make contributions to the open-source software of Johnny-Five. It was specifically created for Arduino-based boards, but can literally run on any type of hardware — like Raspberry Pi — using something called IO-plugins that are maintained by the community.

The initial setup for a “Hello World” application running on Johnny-Five and Node.js

If you look at the example above, you can see you only need to run Node.js on your hardware to be able to install the Johnny-Five framework using npm. After which it only takes a few lines of code to have it saying “Hello World” with a blinking LED on an Arduino-based board.

Raspberry Pi-3 example

In the book by Kassandra Perch the examples all use a Raspberry Pi-3 as the hardware, so let’s try to recreate that example by setting up the “Hello World”-application a Raspberry Pi-3. First, you will of course need this piece of hardware and a LED-light — which are included in most of the starter kits that are available online. On this device you need to have a distribution system installed, for which I would advise to use the Debian-based Raspbian Lite or Raspbian Full.

As we aren’t using a Arduino-based board for this tutorial, you are required to also install the Johnny-Five IO-plugin for your Raspberry Pi. For this example I will skip the steps on how to install Raspbian and assume you already have a distribution system and Node.js installed — but this is all very clearly explained in the book or the many tutorials that can be found online. Having that said, you will need to access the Raspberry Pi and run the following commands from your terminal to install Johnny-Five and the IO-plugin:

npm i -g johnny-five raspi-io

That was easy right? To proceed you will create a new directory called led-blink in where the project files will be added. You can simply do this using:

cd ~
mkdir led-blink & cd led-blink

Once inside this new directory, we can initiate a new npm project to create a package.json file for our application by executing:

npm init -y

Now we have our development environment setup, it is time to add the code needed to run our example. Remember the example published before? You just need to make some minor adjustments to have this example up and running on the Raspberry Pi-3. Let’s create a new file and call it blink.js and place the following code inside this file:

const five = require('johnny-five');
const Raspi = require('raspi-io');
const board = new five.Board({
io: new Raspi()
});
board.on('ready', () => {
// Create an Led on pin 7 (GPIO4) on P1 and strobe it on/off
// Optionally set the speed; defaults to 100ms
(new five.Led('P1-7')).strobe();
});

As you can see in the code snippet above, you start by importing both johnny-five and raspi-io after you create a new Johnny-Five instance and tell the LED — which I assume you placed on pin number 7 counted from the top of your Raspberry board — it should strobe/blink every 100ms.

As it is a JavaScript script running on Node.js, you can simply run a node blink.js to start the blinking of LED or add this to your npm start command in the package.json file. Also, check the source code on Github if anything is unclear.

So where are the robots?

Writing a blog about ACTUALLY talking to robots using JavaScript would be really, really hard as this subject needs a lot of explaining. Also, covering all the hardware settings might be a bit to advanced for developers just getting started with Raspberry Pi or Johnny-Five. But these topics are widely covered in books — like the one mentioned before — and others tutorials online.

Do you like this post or have any suggestions? Please let me know! Below this post, on Twitter or by email.

Make sure to follow me on Twitter to keep notified of all things related to #javacript and #javascriptEverywhere

You can find more information about the book Hands-On Robotics with JavaScript by Kassandra Perch here and find all the code examples on this Github repository. If you want to learn more about the Johnny-Five platform, please have a look at their website http://johnny-five.io. Thank you for reading this post!

--

--

Roy Derks (@gethackteam)
Hackteam

Roy is an entrepreneur, speaker and author from The Netherlands. Most recently he wrote the books Fullstack GraphQL and React Projects.