Johnny-five: TypeError: Cannot read property 'type' of nul

Created on 18 Jul 2013  Â·  8Comments  Â·  Source: rwaldron/johnny-five

This could be my fault. I'm relatively new to Javascript.

root@beaglebone:~/server# node app.js
   info  - socket.io started
1374178483464 Board Connecting... 
1374178483489 Board -> Serialport connected /dev/ttyACM0

/usr/local/lib/node_modules/johnny-five/lib/board.js:615
  var type = board.pins.type;
                       ^
TypeError: Cannot read property 'type' of null
    at Function.Board.Pins.normalize (/usr/local/lib/node_modules/johnny-five/lib/board.js:615:24)
    at Led.Board.Device (/usr/local/lib/node_modules/johnny-five/lib/board.js:548:21)
    at new Led (/usr/local/lib/node_modules/johnny-five/lib/led.js:28:16)
    at Object.<anonymous> (/home/root/server/app.js:7:11)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

Here is my app.js

var app = require('http').createServer(handler),
    io = require('/usr/local/lib/node_modules/socket.io').listen(app),
    fs = require('fs'),
    five = require ('/usr/local/lib/node_modules/johnny-five');

var board = new five.Board({port: "/dev/ttyACM0"});
var led = new five.Led(13);
var camServo = new five.Servo(9);

app.listen(1337);
console.log("Listening on http://beaglebone:1337...");

// directs page requests to static files

function handler (req, res) {
  fs.readFile(__dirname + req.url, function (err,data) {
    if (err) {
      res.writeHead(404);
      res.end(JSON.stringify(err));
      return;
    }
    res.writeHead(200);
    res.end(data);
  });
}

// this handles socket.io comm from html files

io.sockets.on('connection', function(socket) {
    socket.send('connected...');

    socket.on('message', function(data) {
        if (data == 'turn on') {
            console.log('+');
            led.on();
            socket.broadcast.send("ledPin ON");
        }
        if (data == 'turn off') {
            console.log('-');
            led.off();
            socket.broadcast.send("ledPin OFF");
        }
        return;
    });

    socket.on('disconnect', function() {
        socket.send('disconnected...');
    });
});

Most helpful comment

It works because it allows the board to reach ready state before initializing the devices attached to the board and then checks that they are defined before using them :)

All 8 comments

What kind of board are you using?

An Arduino Mega.

On Thursday, July 18, 2013, Rick Waldron wrote:

What kind of board are you using?

—
Reply to this email directly or view it on GitHubhttps://github.com/rwldrn/johnny-five/issues/168#issuecomment-21226983
.

Actually, I just realized the problem. You _cannot_ do this:

var board = new five.Board({port: "/dev/ttyACM0"});
var led = new five.Led(13);
var camServo = new five.Servo(9);

Because the board isn't ready when new five.Led(13); and new five.Servo(9);, that's why every example that has ever been written in the history of the project has a "ready" handler:

var board = new five.Board({port: "/dev/ttyACM0"});
board.on("ready", function() {
  // ... the board is connected, and capabilities reported
  var led = new five.Led(13);
  var camServo = new five.Servo(9);

});

This _might_ work, but I'm not positive, because I've never tried it:

var board = new five.Board({port: "/dev/ttyACM0"});
var led = new five.Led({ pin: 13, board: board });
var camServo = new five.Servo({ pin: 9, board: board });

Edit: It did fix something upon further investigation.

   info  - socket.io started
1374199895540 Board Connecting... 
1374199895564 Board -> Serialport connected /dev/ttyACM0
Listening on http://beaglebone:1337...
1374199895631 Board  
1374199895641 Repl Initialized 
>> /usr/local/lib/node_modules/johnny-five/node_modules/firmata/lib/firmata.js:14971: Uncaught TypeError: Cannot set property 'mode' of undefined

On Thu, Jul 18, 2013 at 8:52 PM, Rick Waldron [email protected]:

Actually, I just realized the problem. You _cannot_ do this:

var board = new five.Board({port: "/dev/ttyACM0"});var led = new five.Led(13);var camServo = new five.Servo(9);

Because the board isn't ready when new five.Led(13); and new
five.Servo(9);, that's why every example that has ever been written in
the history of the project has a "ready" handler:

var board = new five.Board({port: "/dev/ttyACM0"});board.on("ready", function() {
// ... the board is connected, and capabilities reported
var led = new five.Led(13);
var camServo = new five.Servo(9);
});

This _might_ work, but I'm not positive, because I've never tried it:

var board = new five.Board({port: "/dev/ttyACM0"});var led = new five.Led({ pin: 13, board: board });var camServo = new five.Servo({ pin: 9, board: board });

—
Reply to this email directly or view it on GitHubhttps://github.com/rwldrn/johnny-five/issues/168#issuecomment-21227098
.

Which one worked? Don't bother with the last one, I've since tried it and it doesn't work. The board object absolutely _must_ be in its ready state before any devices can be initialized, because they rely on the board's pin capability mapping, which the board object doesn't know about until the connection handshaking is completed.

Try something like this...

var board = new five.Board({port: "/dev/ttyACM0"});
var devices = {};

board.on("ready", function() {
  devices.led = new five.Led(13);
  devices.camServo = new five.Servo(9);  
});

... 

io.sockets.on('connection', function(socket) {
  socket.send('connected...');

  socket.on('message', function(data) {
    if (data == 'turn on') {
      console.log('+');

      if (devices.led) {
        devices.led.on();
      }

      socket.broadcast.send("ledPin ON");
    }
    if (data == 'turn off') {
      console.log('-');
      if (devices.led) {
        devices.led.off();
      }

      socket.broadcast.send("ledPin OFF");
    }
    return;
  });

  socket.on('disconnect', function() {
    socket.send('disconnected...');
  });
});

Thanks it works! I am curious as to why it works?

It works because it allows the board to reach ready state before initializing the devices attached to the board and then checks that they are defined before using them :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ntatko picture ntatko  Â·  11Comments

teuteuguy picture teuteuguy  Â·  4Comments

Spy474 picture Spy474  Â·  10Comments

lezabour picture lezabour  Â·  12Comments

danielhep picture danielhep  Â·  4Comments