0

I've been playing with Raspberry Pi and Node for fun. I thought for a simple experiment what if I grabbed some user input to turn an LED on and off.

const readline = require('readline');
const log = console.log;
const five = require('johnny-five');
const raspi = require('raspi-io');
const board = new five.Board({
  io: new raspi(),
});

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

board.on('ready', function() {
  const led = new five.Led('P1-7');

  const recursiveAsyncReadLine = function () {
    rl.question('Command: ', function (answer) {
      switch(answer) {
        case 'on':
          log('Got it! Your answer was: "', answer, '"');
          led.on();
          break;
        case 'off':
          log('Got it! Your answer was: "', answer, '"');
          led.stop().off();
          break;
        default:
      }
      recursiveAsyncReadLine();
    });
  };
  recursiveAsyncReadLine();
});

It works however I get 2 strange bugs. In the console output below you'll see it prompts for my input... I enter my input, then it repeats my input in a distorted string of text (see Example 1). Then after my verification message is output (Got it! Your answer was: " on ") I am met with a ReferenceError: on is not defined (Example 2) even though the LED lit up perfectly.

Command: on
oonn  //Example 1
Got it! Your answer was: " on "
Command:
ReferenceError: on is not defined //Example 2
>> off
ooffff
Got it! Your answer was: " off "
Command:
ReferenceError: off is not defined
>> on
oonn
Got it! Your answer was: " on "
Command:
ReferenceError: on is not defined
>> off
ooffff
Got it! Your answer was: " off "
Command:
ReferenceError: off is not defined
>> on
oonn
Got it! Your answer was: " on "
Command:
ReferenceError: on is not defined
>> off
ooffff
Got it! Your answer was: " off "
Command:
ReferenceError: off is not defined

I am thinking this is not so much a Raspberry Pi/Johnny-five thing and just a plain old JavaScript or Node issue.

Any ideas?

Striped
  • 2,544
  • 3
  • 25
  • 31
fromspace
  • 355
  • 3
  • 18
  • The `ReferenceError: on is not defined` does not have its origin in the shown code, because there is not variable `on` that is used there. And the error seems to be catched and logged at a completely different place. Where is the error `ReferenceError: on is not defined` logged? – t.niese Feb 12 '18 at 19:04

2 Answers2

0

Ok retracing my previous statement looks like your issue is probably

led.stop().off();

In examples for Johny-five they show

led.stop();
led.off();

http://johnny-five.io/examples/led/

Good luck, happy hacking :-)

Chris Wininger
  • 651
  • 8
  • 13
  • If `off` would not exists for the value returned by `stop()`, then the error shown would be either, `undefined has no properties` (if `undefined` is returned), or `off is not a function` (if an object is returned but the property `off` is not a function). A `ReferenceError` occurs only if a variable with this name is used that is not defined in the scope. Beside that there are examples using it that way [led-pulse.js](https://github.com/rwaldron/johnny-five/blob/002b5191b99a990bc6e6f34ada62b99d5a01ac09/eg/led-pulse.js). – t.niese Feb 12 '18 at 19:11
  • Hmm, is there any more of a stack trace, that is pretty weird – Chris Wininger Feb 12 '18 at 19:38
0

What happens if you reduce to this (works for me)?

const readline = require('readline');
const log = console.log;

const rl = readline.createInterface({
     input: process.stdin,
     output: process.stdout
   });

   const recursiveAsyncReadLine = function () {
          rl.question('Command: ', function (answer) {
             switch(answer) {
                case 'on':
                   log('Got it! Your answer was: "', answer, '"');
                break;
                   case 'off':
                   log('Got it! Your answer was: "', answer, '"');
                   break;
                default:
              }
              recursiveAsyncReadLine();
       });
 };

 recursiveAsyncReadLine();

If that works, that at least tells us it has something to do with the interaction with either raspi-io or johnny-five. Also where does this run, does this code execute on the pi, if so, do you enter the text directly on the pi or is this being entered remotely. If entered remotely what is the platform of the machine you are entering the text on?

Chris Wininger
  • 651
  • 8
  • 13