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.

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.

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.

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:

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.

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.

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.

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.