Javascript robotics: The Johnny Five REPL

Published on June 5th 2020
A screenshot of a social media post
In the previous article, we discussed how to get up and running with Johnny Five and the ESP8266 micro-controller. If you are not sure how to get set up I recommend you read that article first.
Previously we created a simple script that allowed us to control the onboard LED with our keyboard. This required us to install the keypress package and listen in process.stdin. This is not a bad approach but there is a better one.
Johnny Five comes packaged with a REPL. A REPL (read-eval-print-loop) is an interactive language shell giving a simple interactive interface to the Johnny Five API. We can control our robots with the REPL but there is some set up first.

Setting up the REPL

We need to make the REPL aware of our hardware by injecting the instance of the hardware into our script. We are using the script from before but with the keyboard code stripped out:
1const { EtherPortClient } = require("etherport-client")
2const { Board, Led, Pin } = require("johnny-five")
4const board = new Board({
5 port: new EtherPortClient({
6 host: "",
7 port: 3030,
8 }),
9 repl: false,
12const LED_PIN = 2
14board.on("ready", () => {
15 console.log("Board ready")
16 var led = new Led(LED_PIN)
Now, let's add the REPL code. Update the board ready callback to look like this:
2board.on("ready", function() {
3 /*
4 Initialize pin 2, which
5 controls the built-in LED
6 */
7 var led = new Led(LED_PIN);
9 /*
10 Injecting object into the REPL
11 allow access while the program
12 is running.
14 Try these in the REPL:
16 led.on();
18 led.blink();
20 */
21 board.repl.inject({
22 led: led
23 });
With this simple addition, we now have access to all the functions available on the LED object.

Limiting access

But what if we don't want to give our users unfettered access? What if we only want to give access to specific functions or write functions that do more than just control the hardware. Maybe we want to add logging or provide more appropriate function names. Well, we can write our own functions that are available in the REPL and inject those instead.
2board.on("ready", function() {
3 /*
4 Initialise pin 2, which
5 controls the built-in LED
6 */
7 var led = new Led(LED_PIN);
9 board.repl.inject({
10 // Allow limited on/off control access to the
11 // Led instance from the REPL.
12 on: function() {
13 led.on();
14 },
15 off: function() {
17 },
18 flash: function () {
19 led.blink();
20 },
22 });
This script will make on(), off() and flash() functions available in the REPL.
And that is it. A nice, short intro to the Johnny Five REPL. Until you start hooking your scripts up to WebSockets or a REST API, I think this is one of the better ways to control your robots.
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