Wireless javascript robotics with Johnny five and the ESP8266

Published on May 29th 2020
A truck cake sitting on top of a table
In this article, I will talk about the first steps needed to start building robots with Javascript. I will be using the infamous ESP8266 microcontroller, this is because it is super cheap and allows it not to be tethered to your machine in the way that an Arduino would be.
To allow us to write our robotics scripts in Javascript with will be using the Johnny-Five library written by Rick Waldron. The library supports a huge selection of boards and hardware. Although not all boards support all of the hardware.
Several other boards could be used as alternatives (Proton, Tessel) but these are considerable more expensive and not as easily available. It is also possible to use an Arduino attached to a Raspberry Pi (RPi) and you then interface wirelessly with the RPi but that seems a little unnecessary now.
There are better languages to build robots with, but being an engineer working primarily in Javascript, I was keen to keep things close to home. In this setup Javascript is not run on the microcontroller but is run through some custom firmware called the Firmata protocol. This does mean it runs slower than something like C would but generally for things like robots this is not much of an issue. The first step in this process is to upload the StandardFirmataWifi sketch onto our board but to be able to do that we need the Arduino IDE and for it to recognise our ESP8266 based boards.

Getting set up

The following instructions are based on using a mac. They will be very similar to other platforms.
Copy the following URL http://arduino.esp8266.com/stable/package_esp8266com_index.json. Open the IDE and go to the files menu and click on preferences. Add the URL to the ‘Additional Boards Manager URL’.
A screenshot of a social media post
Image of arduino ide
Close the preferences panel and click tools. Under tools select boards and then board manager. Navigate to esp8266 by esp8266 community and install the software for Arduino.
A screenshot of a cell phone
Board Manager
Once this has been done you should be able to program your ESP8266 based board, I am using the NodeMCU board.
Click the examples panel and select the Wireless Firamta sketch. We will be updating so make a copy now.
We need to update the header file wifiConfig.h with our network credentials. Update the values of char ssid[] ="" and char wpa_passphrase[] ="".
With this done we can now upload the sketch to our device. Once uploaded you can close the Arduino IDE.
Don’t forget that the ESP* boards have a different pin layout to the Arduino boards. See the picture below for an example.
A black and silver text on a screen
NodeMCU Pinout
Now that we are all set up on the microcontroller we need to create a new node project and install the needed packages.

Our first robot script

Create a new folder for the project and initialise a new node project
1$ mkdir helloWorld && cd $_ && npm init -y
Now we need to install Johnny-Five and the ethernet client that allows us to connect wirelessly.
1$ npm install johnny-five ethernet-client keypress
With this done we are set to write our first script.
1const { EtherPortClient } = require("etherport-client")
2const { Board, Led, Pin } = require("johnny-five")
3const keypress = require("keypress")
5const board = new Board({
6 port: new EtherPortClient({
7 host: "",
8 port: 3030,
9 }),
10 repl: false,
14const LED_PIN = 2
16board.on("ready", () => {
17 console.log("Board ready")
18 var led = new Led(LED_PIN)
19 console.log("Use Up and Down arrows for On and Off. Space to stop.")
21 process.stdin.resume()
22 process.stdin.setEncoding("utf8")
23 process.stdin.setRawMode(true)
25 process.stdin.on("keypress", (ch, key) => {
26 if (!key) {
27 return
28 }
30 if (key.name === "q") {
31 console.log("Quitting")
32 process.exit()
33 } else if (key.name === "up") {
34 console.log("Blink")
35 led.blink()
36 } else if (key.name === "down") {
37 console.log("Stop blinking")
38 led.stop()
39 }
40 })
We need to replace with the IP assigned to our board. I use an app called Fing but this information can be found out from the Serial monitor in the Arduino IDE.
A screenshot of a cell phone
This simple script will make the allow you to turn the onboard led on and off. Nothing fancy but paves the way for much more exciting things. To execute the file node index.js. You should see something similar to this:
1$ node hello.js
21590554783332 SerialPort Connecting to host:port:
31590554783334 Connected Connecting to host:port:
41590554793338 Use Up and Down arrows for On and Off. Space to stop.
Now that the board is set up we are ready to create some more interesting projects. Johnny Five provides such a gentle introduction to robotics in Javascript but allows you to do so much as you have the wealth of packages in NPM and hundreds of public APIs that you can lean on to create great projects.
The next article will be a nice short one, introducing the Johnny Five REPL and why it is great for prototyping your next project.
Free download for your phone or tablet
Download on the App StoreGet it on Google Play

Lots to see

Follow and read AlanGardenMaster’s articles as he develops his new one-acre plot. PimlicoDan shows city gardening in a whole new light, or follow DaisyDays on her adventures in the allotment and as a professional gardener. Just a few of the many personalities you’ll meet in our app. Free download for your phone or tablet.
Download on the App StoreGet it on Google Play


Subscribe for the latest updates from Candide

What is Candide?

Candide has everything for plant lovers – buy plants from independent sellers and book tickets to visit inspiring gardens near you. Identify plants in seconds from a single photo and learn how to care for them with our in-depth guides.


Learn how to care for your plants and share your growing successes on Candide’s free app for your phone or tablet.

Download on the App StoreGet it on Google Play

Germinated in Bristol © 2021 Candide