Node.js, Arduino & Raspberry Pi (II)

UPDATE: Johnny-Five is fully working now! read more about it here!

In our last post we talked about talking to Arduino with Javascript. We used the framework Duino as a workaround when your Arduino board, as mine, is not the best suited for running Jhonny-Five (or if you can’t manage to make it work, as it was my case).

Our next step: Repeat our current achievements from a Raspberry Pi connected to our Arduino board.

First of all, you obviously need to install Node.js in your Raspberry Pi. To do that, I followed the step “Installing Node.JS” from this post. I ignored the steps for changing the IP address to a static address and the start script, as we will cover (and go further) these aspects when turning our Raspberry Pi into an AP.

So now, with Node.js running in my Raspberry Pi, I promptly plugged my Arduino board, set the classic 13-pin-led configuration and the code we saw in the last post:

1
2
3
4
5
6
7
8
9
var arduino = require('duino'),
    board = new arduino.Board();

var led = new arduino.Led({
  board: board,
  pin: 13
});

led.blink();

And…. it didn’t work.

What happened?

After some debugging I found that the Board.js file from Duino, when searching for the serial port where the Arduino is supposed to be plugged, runs this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Board.prototype.detect = function (callback) {
  this.log('info', 'attempting to find Arduino board');
  var self = this;
  child.exec('ls /dev | grep usb', function(err, stdout, stderr){
    var usb = stdout.slice(0, -1).split('\n'),
        found = false,
        err = null,
        possible, temp;

    while ( usb.length ) {
      possible = usb.pop();

      if (possible.slice(0, 2) !== 'cu') {
        try {
          temp = new serial.SerialPort('/dev/' + possible, {
            baudrate: self.baudrate,
            parser: serial.parsers.readline('\n')
          });
        } catch (e) {
          err = e;
        }
        if (!err) {
          found = temp;
          self.log('info', 'found board at ' + temp.port);
          break;
        } else {
          err = new Error('Could not find Arduino');
        }
      }
    }

    callback(err, found);
  });
}

More precisely, this line:

1
ls /dev | grep usb

This command was returning… nothing, so the Board class wasn’t able to see where was the Arduino plugged. The problem is that our Arduino board, when plugged into our Raspberry Pi, will appear (at least in my Rpi), as ttyACM0 or ttyACM1, and the grep command as it is, will not return any suitable device.

I forked the project to make some experiments (you can find my fork here), and replaced the grep command for

1
ls /dev | grep -E 'usb|ttyACM*'

And bang! it worked!

Now our Raspberry Pi is running some Javascript code and talking to our Arduino board!

Next step?: Deploy a full Node.js server into our Raspberry Pi to offer a proper HTML5 interface to control our Arduino board and set the Raspberry Pi as an AP so that we can connect to it.

We’ll talk about this in our next post :D

Comments