Node.js on Arduino Yún

We wanted a system with which we could control the lights and heating of our office as well as reading out some sensors.

Here at AppSaloon (where I do my internship) they use Node.js for a lot of things, so the language of choice was already chosen. Some of us are really good at writing Node.js and others (me) are eager to learn it. So we decided to run Node js on the Yún. So how did we do this? I’ll give you the short version, because it took me a while to end up with this solution.

©2014 Arduino

©2014 Arduino

First off all you need to expand your flash, because there is not enough flash (16 MB) on the Arduino Yún. Sounds reasonable right? Not when you got the Yún for the first time in your hands. Luckily someone wrote this tutorial.

To start with you need to be on the same network to reach the Arduino :-).

Open your terminal

ssh root@arduino.local

It will ask for a password, which will by default be: ‘arduino’:

Now you can install Node.js on the Yún type :

opkg update

opkg install node

When the install is finished verify it by :

 node -v

It should give you something like this v0.10.28 (it may vary in the future)

Next you have to install the node-serialport package. This is necessary because the Atheros AR9331 needs to communicate with the ATmega32u4.

opkg update

opkg install node-serialport

After this you will need firmata for Node.js. This will control the inputs and outputs of the ATmega32u4.

Normally you would install firmata like this:

npm install firmata

But because the Arduino Yún does not have enough RAM this is ‘not possible’.

You can work around this problem by installing the firmata package on your own system and copy the node_modules directory from your system to the SD-card. Just pull your SD-card out of the Yun and plug it into your computer. You will find node_modules directory in the arduino folder.

'--arduino

----node_modules

------node-serialport

----www'

Now everthing is ok to write the Node.js server code. Below is my Node.js server code.

console.log('blink start ...');



var ledPin = 13;



var firmata = require('firmata');

var board = new firmata.Board("../../../../../dev/ttyATH0",function(err) {

if (err) 

{

    console.log(err);

    board.reset();

return;

} 

else 

{

    console.log('connected');

    var boardInfo = 'Firmware: ';

    boardInfo = boardInfo + board.firmware.name;

    boardInfo = '-';

    boardInfo = boardInfo + board.firmware.version.major;

    boardInfo = '.';

    boardInfo = boardInfo + board.firmware.version.minor;

    console.log(boardInfo);



    var ledOn = true;

    board.pinMode(ledPin, board.MODES.OUTPUT);

    var strings = require('querystring');

    var http = require('http');

    http.createServer(function(request, response)

    {

        console.log(strings.parse(request.url).value);

        if ((strings.parse(request.url).value) == 'HIGH')

        {

            board.digitalWrite(ledPin, board.HIGH);

        } 

        else 

        {

            board.digitalWrite(ledPin, board.LOW);

    }

    response.writeHead(200);

    response.write("Hello this is David");

    response.end();

}).listen(8080);

console.log('Listening on port 8080 ...');

}

});

Save this as led_server.js in the www directory of the Yun.

You will have to upload the standard firmata sketch inorder to make the communication work. This standard firmata sketch needs a bit off tweaking. In the Setup() change Serial to Serial1.

void setup()

{

   Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION,

      FIRMATA_MINOR_VERSION);



   Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);

   Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);

   Firmata.attach(REPORT_ANALOG, reportAnalogCallback);

   Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);

   Firmata.attach(SET_PIN_MODE, setPinModeCallback);

   Firmata.attach(START_SYSEX, sysexCallback);

   Firmata.attach(SYSTEM_RESET, systemResetCallback);



   Serial1.begin(57600); // Set the baud.

   Firmata.begin(Serial1);

   systemResetCallback();  // reset to default config

}

You wil need to upload the sketch with the wireless port not the USB!!!!

It could be that when you want to upload your sketch you get an error like this :

avrdude: verification error, first mismatch at byte 0x0000

         0xcb != 0xfb

avrdude: verification error; content mismatch

If this is the case click here.

You don’t need to wire anything to the Arduino, because we’re using pin 13 which has already a LED attached.

You need to comment the tty line in the inittab. Go to /etc/ and then type nano inittab. There you can comment the line.

tty

Now you are ready to start the Node.js server. You’re directory structure should look like this

'--arduino

----node_modules

------node-serialport

------firmata

----www

------led_server.js'

Go to the www directory of the Yún and type in your terminal:

node led_server.js

Now you’re done. Make sure you are on the same network and open your browser and turn the led ON with:

http://arduino.local:8080?value=HIGH

or OFF with

http://arduino.local:8080?value=LOW

You can follow my progress on this github repository. (I also installed git on the Yún so I can develop on my laptop with a nicer IDE and “git pull” on the Yún side.