It took me about 2-3 days to get johnny five properly working on my Arduino Yun. so I thought Id write down the steps I took so others (and myself in future) can use it when facing similar task.
First you need to make sure your Yun's firmware is up to date. This should be done first because updating firmware deletes all files on your Yun. There is a good instruction on how to do this at: http://arduino.cc/en/Tutorial/YunSysupgradeNext you'll want to increase your Yun's storage space because the flash memory that comes with Yun is not very large and it also wears out overtime. So using an SD card both gives you more space to work with and also somewhat extends the life of your Yun. Good instructions for that can be found at: http://arduino.cc/en/Tutorial/ExpandingYunDiskSpace. There is also a newer version of the disk space expander sketch that they mentioned in the instruction, the new one should laos create a swap partition. I havent tried it myself but there's a link to where to get it at the links section if you want to try.
Now its time to SSH into your Yun and install NodeJS. If you are on a windows machine (like I was) then Putty is a good SSH client to use. Installing NodeJS is easy, just type the following commands:opkg update
opkg install nodeInstalling NodeJS (the second command) took a looooong time (~30-45 min maybe) for me so be patient.
Next up, you'll need to install node-serialport. This is needed to facilitate the connection between the OpenWRT linux side of Yun and the Arduino ATmega 32U4 side. To do that, type in the following commands:
opkg update
opkg install node-serialport
Same here, installing node-serialport took a long time
Next you'll need to disable the bridge script so NodeJS can use that channel instead. To do that edit the /etc/inittab
file and comment out the ttyATH0
line. You can edit the file by typing
nano /etc/inittab
The file should look something like this after editing:
::sysinit:/etc/init.d/rcS S boot ::shutdown:/etc/init.d/rcS K shutdown #ttyATH0::askfirst:/bin/ash --loginNext upload the StandardFirmataYun sketch to your Arduino, you can do this over wifi or with an USB cable, doesnt matter. The sketch can be found here: https://github.com/firmata/arduino/tree/master/examples/StandardFirmataYun and I also have it available in this repo here. Arduino IDE comes with a version of StandardFirmata but it has problems that make it not suitable for Yun (namely not using ATH0 and not handling the u-boot problem, see the links section). Now the last thing left to do is to install the johnny-five module and all other modules you need for your project (I also used socket.io and express). You'll probablly need to do this on your computer since Yun doesnt have enough RAM to install npm modules (you get an out of memory error). To install those modules simply navigate to your projects's folder in your PC and type in:
npm install module-nameSo to install johnny-five type
npm install johnny-fiveBefore copying the modules over to your Yun make sure to delete the
serialport
module from johnny-five/node-modules
folder. This is needed because serialport on your PC is compiled for your PC and may not work on Yun. If we delete it then johnny-five will use the node-serialport we installed on Yun earlier.
I used PSCP to upload my project to my Yun.
When defining the board in johnny-five make sure to pass the port argument. It didnt work otherwise for me.
var board = new five.Board({port: "/dev/ttyATH0"});Thats it. Everything should work fine now. Below is the server script I used on Yun to turn an LED on and off from a website. It uses express to host the webserver, socker.io to send information from the website to NodeJS and johnny-five to control the led. Same setup should work with any other johnny-five capabilites (motors, servos, sensors, etc.)
// Basic web server setup var express = require('express'); var srv = express(); var http = require('http').Server(srv); var io = require('socket.io')(http); var port = 8080; srv.use(express.static(__dirname)); http.listen(port, function(){ console.log('listening on localhost:' + port ); }); // Johnny-five setup var five = require('johnny-five'); var board = new five.Board({port: "/dev/ttyATH0"}); var boardReady = false var led board.on("ready", function() { //Set Hardware variables led = new five.Led(13); // Write down that board is ready boardReady = true; console.log("Board ready!"); }); // Start listening for socket events io.on('connection', function(socket){ // Led state changed socket.on('led', function(data){ if(boardReady){ if(data == true) led.on(); else led.off(); } }); });
- A guide on the same subject that helped me: http://cylonjs.com/documentation/platforms/yun/
- NodeJS: http://nodejs.org/
- Johnny-Five: https://github.com/rwaldron/johnny-five
- Socket.io: http://socket.io/
- Express: http://expressjs.com/
- Arduino Yun: http://arduino.cc/en/Main/ArduinoBoardYun?from=Products.ArduinoYUN
- Another guide on expanding disk space and installing NodeJS: http://blog.arduino.cc/2014/05/06/time-to-expand-your-yun-disk-space-and-install-node-js
- Another article on this: https://github.com/born2net/mediaArduino
- A forum post about how firmata blocks yun's boot: http://forum.arduino.cc/index.php?topic=215338.msg1577312#msg1577312
- New version of disk space expander that also allows creating swap: https://github.com/Fede85/YunSketches/tree/master/YunDiskSpaceExpander
- Official firmata for Yun: https://github.com/firmata/arduino/tree/master/examples/StandardFirmataYun