Installing and Running NodeJS on the Arduino Yun

NodeJS + Yun

Below are the steps to get NodeJS up and running on the Arduino Yun. This tutorial assumes you have already connected the Yun to your network, and have a basic understanding of NodeJS.

Below are the steps to get NodeJS up and running on the Arduino Yun. This tutorial assumes you have already connected the Yun to your network, and have a basic understanding of NodeJS.

The final steps (red section) of this tutorial are optional and should be taken with caution. They show how to disable Bridge on the Yun, in order to allow NodeJS to speak directly with the Arduino side of the board using node-serialport.

This, however, means you can only access the Linux side of the board over the network. If you do not have control over the network between your computer and Yun, you will not be able to connect.

Update the Yun’s image

To do all the following, make sure you have updated your Yun to the latest image. This to to allow your Yun to have is disk space expanded in the next step.

Instructions on updating the Yun’s image

Expand disk space with SD card

We will be moving the Yun’s operating system and memory onto a micro-sd card, so that we have enough room to install and run NodeJS.

Instructions on expanding the Yun’s disk space

Install Node and node-modules with opkg

With an updated Yun image and more memory, we can install Node using opkg (the Yun’s package management system). Be sure the Yun is connected to the internet.

Enter the following line into the Yun to prepare opkg.

opkg update

After each call to opkg, you should see some lines about downloading and installing some files.

Next install NodeJS with the following line.

opkg install node

This may take a few moments. After it’s finished, install the pre-compiled node-modules using opkg.

opkg install node-ws 
opkg install node-serialport

There are a few other pre-compiled node-modules you can install through opkg. All modules are installed globally, so there is no need to ever run npm (especially because npm doesn’t work on the Yun…)

Add SFTP to transfer files

Uploading files to the Yun is much faster if you don’t have to turn it off every time. To do this, we have enable our Yun to be accessed over SFTP. Now, applications like CyberDuck can connect to and upload files to your Yun

Enter the following line to install the SFTP package, and now you can connect to it using an FTP client like CyberDuck.

opkg update 
opkg install openssh-sftp-server

In CyberDuck, connect with SFTP, port 22, username ‘root’, and the server-name being your Yun’s IP address.

cyberDuck_yun

I store all the node and web files on the micro-sd card, at /mnt/sda1/arduino/node. “sda1” is the external memory, “arduino” is a folder that should already exist, and “node” is a folder I created to store my Node scripts.

cyberDuck_yun_2

Now you can upload and run NodeJS scripts on your Yun! See the below link for you can get NodeJS and the Arduino side of the Yun talking with Bridge:

Node.js on the Arduino Yun - Tom Igoe’s example for getting NodeJS and the ATmega32U4 to communicate with Bridge.

Give the ethernet port a static IP

To my mind, the biggest disadvantage of removing Bridge from the Yun, is that you are taking away the ability to access the Linino through the ATmega32U4. This is especially helpul during initial setup, and while configuring the Yun’s network.

To give some added security, I have chosen to give my Yun’s ethernet port a static, never changing IP address. With this, I can ssh into the Yun over an ethernet cable, using the following steps.

We will edit a file to change the Yun’s network settings. The nano command opens it in terminal:

nano /etc/config/network

If you get the error Error opening terminal: xterm-256color., the following worked for me.

ln -s x /usr/share/terminfo/78 
export TERM=xterm-color 
nano /etc/config/network

Find the part of the document that looks like this:

config interface 'wan'
   option iframe 'eth1'
   option proto 'dhcp'
   option metric '10'

And change it to look like this:

config interface 'wan'
   option iframe 'eth1'
   option 'proto' 'static'
   option 'ipaddr' '192.168.0.200'
   option 'netmask' '255.255.255.0'
   option 'gateway' '192.168.0.1'
   option 'dns' '192.168.0.1'
   option metric '10'

Instructions on the bottom show how to Write Out (save the file), and Exit.

To Write Out (save your changes), hit CONTROL-O. It will ask if you want to save changes to this file, hit ENTER to save. To Exit hit CONTROL-X.

Restart your Yun, and you will find it’s ethernet port has the permanent address 192.168.0.200.

In order to ssh into the Yun over an ethernet cable, your laptop’s ethernet IP address must share the same two first digits as the Yun’s IP. In this example, the laptop’s IP should beging width 192 and 168 in order for the two to communicate.

staticEthernet_mac

On a Mac, I can go to System Preferences > Network, select the ethernet port, and tell it to be a Manual IP Address (not DHCP). For example, I could set my laptop to be 192.168.0.42.

Disable Bridge on the Yun

Warning: Proceed with Caution! To disable Bridge and free the serial port between the Linux processor and the ATmega32U4, you simply have to comment out one line, and then restart the Yun. To bring Bridge back, simply uncomment the line, and restart the Yun.

Open the file we must in edit in Terminal with the following command.

nano /etc/inittab

If you get the error Error opening terminal: xterm-256color., the following worked for me.

ln -s x /usr/share/terminfo/78 
export TERM=xterm-color 
nano /etc/inittab

The nano command will open up a text editor in Terminal, where you can move around with the arrow keys.

Find the line that says this:

ttyATH0::askfirst:/bin/ash --login

And comment it out with pound sign at the beginning, like this:

#ttyATH0::askfirst:/bin/ash --login

Instructions on the bottom show how to Write Out (save the file), and Exit.

To Write Out (save your changes), hit CONTROL-O. It will ask if you want to save changes to this file, hit ENTER to save. To Exit hit CONTROL-X.

Restart your Yun, and Bridge will not run. Once again, simply uncomment the line to bring Bridge back.

Modify code and run

The Arduino on your Yun can now open a plain Serial connection to the Linux processor. However, it uses Serial1 instead of Serial. Serial (without the 1) is connected to the micro-USB, and is used to upload code and communicate with a laptop.

So to begin in your setup(), your Arduino code should read:

Serial1.begin(115200); 
while(!Serial1){}

And to read and write, continue using Serial1:

while(Serial1.available()){
   Serial1.print(Serial1.read());
}
Serial1.println();

Any process running on Linux side of the board can access the Arduino’s serial port with the name /dev/ttyATH0.

Now any NodeJS script using the node-serialport module can access the Atmel32u4 using port /dev/ttyATH0.

Resources

NodeJS_Yun_Example - Some example code, showing a NodeJS server mediating the connection between a browser and an Arduino.

Reference for node-serialport - Connect a Node script to the ATmega32U4

Reference for node-ws - Run a WebSocket server on the Yun to have fast, real-time communication with a browser or other devices.

Node-modules available through opkg - the repo containing all pre-compiled node-modules for the Yun. Others include Noble, Bleno, and socket.io.

Node.js on the Arduino Yun - Tom Igoe’s example for getting NodeJS and the ATmega32U4 to communicate with Bridge. Its a bit of a hack that uses the stdin and stdout of a process.

TEACHING
blog content post post format

Blog Archive