Javascript robotics: The Johnny Five REPL

jim-rohm
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 package and listen in . 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 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")
3
4const board = new Board({
5 port: new EtherPortClient({
6 host: "192.168.1.109",
7 port: 3030,
8 }),
9 repl: false,
10})
11
12const LED_PIN = 2
13
14board.on("ready", () => {
15 console.log("Board ready")
16 var led = new Led(LED_PIN)
17})
Now, let's add the REPL code. Update the board ready callback to look like this:
1[...]
2board.on("ready", function() {
3 /*
4 Initialize pin 2, which
5 controls the built-in LED
6 */
7 var led = new Led(LED_PIN);
8
9 /*
10 Injecting object into the REPL
11 allow access while the program
12 is running.
13
14 Try these in the REPL:
15
16 led.on();
17 led.off();
18 led.blink();
19
20 */
21 board.repl.inject({
22 led: led
23 });
24});
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.
1[...]
2board.on("ready", function() {
3 /*
4 Initialise pin 2, which
5 controls the built-in LED
6 */
7 var led = new Led(LED_PIN);
8
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() {
16 led.off();
17 },
18 flash: function () {
19 led.blink();
20 },
21
22 });
23});
This script will make , and 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