Category: Install

Debug Node js remotely on Intel Edison with Jetbrains Idea

This blog post will demonstrate how to configure Jetbrains Idea (or similar product) to remotely debug Node js applications on an Intel Edison. Though I will use Edison for this post these instructions should work for remotely debugging most Linux targets running Node.

I’m a big fan of using Node js to power my IoT applications on the Intel Edison. It has a lot of advantages — ease of use (once you get used to the node way of doing things), support for mraa and upm,  and it is pre-installed. Intel even provides a decent ide to code it with – the XDK for IoT.

Jetbrains makes several ide products for various languages. Most will support HTML5 and Node js via a plugin. I find Jetbrains tools are fairly reasonably priced and more than pay for themselves in productivity gains vs other tools. (No, I don’t work for Jetbrains, I’m just a fan). For the remainder of this post when I refer to one of Jetbrains compatible products I’ll refer to it as JB.

The XDK for IoT is a fine tool, but Jetbrains’ tools are an order of magnitude better. With Intellij I get better HTML5 support, Javascript debugging and Node debugging right in the ide. I prefer the Jetbrains ide for the bulk of the coding I do.

To use JB we need to do a little configuring. First – make sure the Node js plugin  is installed.  Jetbrains has detailed instructions on installing plugins on their website.  You should go ahead and install the “Remote Host Access” plugin too — we will need that to transfer files later.

We will be using a ssh tunnel to forward our host Node debugger port (default 5858) to our Edison device. We have a choice of ports to use so we have the option of working with more than one Edison. We will use port 5858 on one Edison in this example.

So — to configure:

On the Edison:

Allow port forwarding: 

SSH by default does not allow remote hosts to forward ports so we need to enable it. Edit /etc/ssh/sshd_config and find the following line somewhere in that config file.

#GatewayPorts no

Change it to this:

GatewayPorts yes

Shutdown the app running in .node_app_slot. 

I generally start my IoT apps from one of the templates included with the XDK (I’m lazy that way). I will also go ahead and use the node_app_slot directory to upload it to. This keeps the apps XDK compatible. I then open the local project directory in JB and setup the configuration for the project.

The problem here is that the Edison will autostart the project on a reboot. This makes us have to take the extra step of starting up the XDK, connecting to the Edison and stopping the running project.

Reboot the Edison to ensure the changes to our files are picked up. If you have code in node_app_slot go ahead and stop it.

On our host machine:

Setup ssh tunnel for port 5858:

The magic that makes our remote debugging work is ssh port forwarding. For Windows users this can be set up with Putty as described here. Just skip to the Putty config part and ignore the rest.  For Linux and Mac users this should work:

ssh -f root@edison.local -L 5858:localhost:5858 -N

Where edison.local is your device mdns name or ip address. This starts the port forwarding session. We have to manage this manually. If we reboot our Edison we need to kill the current ssh tunnel connection and start a new one. To do this first run:

ps aux | grep ssh 

This will list all the currently running ssh process. We need to find the one that matches the signature of the tunnel. It is easy to spot — the output will be look like this:

This is the output of ps aux | grep ssh showing our ssh processes.

From the output we can see the process that matches our ssh tunnel. The process id is the first number from the right (on Mac any way) and is 8084 in this case. To end the connection we need to kill the process with this command:

kill 8084

Windows users will just close Putty. This connection will also be cleared after a reboot.

Create a remote Node debug configuration in JB:

This assumes you already have a project started. What I normally do is start the project using a template in XDK and then open it with JB. This keeps the code compatible with both applications.

In JB select Run/Edit Configurations. We will click the + button and select ‘Node.js Remote Debug’. Name it what ever you like and set it up like this:

Node-js-deployment

When we debug this app we will use this configuration. There is more info on setting up Node js configurations here.

Setup Deployment in JB:

JB will automatically upload our source files to the Edison via a deployment configuration. To access this Select ‘Tools/Deployment/Configuration’ from the JB menu. Click the + button in the upper left and create a configuration that looks something like this:

deployment-1-node-jb

To keep from uploading unwanted files setup some exclude paths (these are local paths):

deploy-exclude-node-jb

To enable autoupload select ‘Tools/Deployment/Automatic Upload (always).  Whenever we save a file in our project it will be automatically sent to the proper location on the Edison. More info on JB deployment settings can be found here.

Once we have the deployment setup, we upload everything to the Edison by selecting “Tools/Deployment/Upload to xxxxx”.  Select the option that works for you.

Now that we have everything configured we are ready to debug. To start the debugging session we first ssh to the Edison and start our app in debug mode like this:

node --debug-brk app.js

Where app.js is the file we want to debug. This will cause node to start up and pause. We then select “Run/Debug” from the JB menu and give the debugger a little while to connect to our target machine.

Then we simply debug and run the app with all the Jetbrains goodness.

References:

https://www.jetbrains.com/idea/help/running-and-debugging-node-js.html

http://stackoverflow.com/questions/8445534/how-to-remote-debug-node-js-with-phpstorm

http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html

Updating MRAA and/or UPM on Intel Edison.

Update November 4, 2015

With the release of mraa 8.1 there is now support for Node. 4.X. Upm  4.0 now allows the installation of upm modules via npm. I have updated this post to account for the new capabilities of these libraries.

There is also the option of updating the libraries by using the XDK. Using version 2571 of the XDK for IoT select the device config drop down and click ‘Update libraries on board’. updata-lib-xdk

This has to be the easiest way.

End Upate.

 

This comes up for me quite often so I decided to add a post about it.

Here are some quick and easy to follow step for installing/updating MRAA and UPM on an Intel Edison. The Edison generally  has the prerequisites installed already so you should just have to compile. If you need more comprehensive instructions you can take a look at MRAA/UPM section of my Getting Started with Minnow Board Max post.

These instructions assume you have git installed. If not install git first following the instructions found here.

Install MRAA:

If we just want to update MRAA on an Edison we can use:

echo "src mraa-upm http://iotdk.intel.com/repos/2.0/intelgalactic" > /etc/opkg/mraa-upm.conf

opkg update
opkg install libmraa0

This will install the latest release version of mraa (8.1 as of 3 November 15) and would be the preferred way to go.  If you need something that is upstream from this release you will have to down load and compile the source.

The mraa library can also be installed by npm if you only want the Node js bindings. It’s as simple as this:

npm install mraa

How to install from source is described here:

First we need to clone the MRAA repo from git hub:

git clone https://github.com/intel-iot-devkit/mraa.git

This will download whatever the default branch and tag is currently set at (usually the latest). If you wanted to switch to another tag (like version 7.3 for example) now is the time to do so

Now we are ready to build. We will used the same out of tree build as described in the MRAA documentation.

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr -DBUILDSWIGJAVA=ON
make
make install

Details can be found on the mraa github page.

 

Install UPM:

Upm will be installed with mraa. If you have just installed the latest mraa you should have the latest upm also (4.0 as of Nov. 4, 2015).

As of version 4.0 upm can also be installed via npm. Of course you will only get the node bindings.  It’s as simple as:

npm install jsupm_<module-name>

If there are sensors in the upstream branch you want to use you will have to compile all or part of upm by hand as described here:

UPM can be cloned with the following:

git clone https://github.com/intel-iot-devkit/upm.git

Switch to the desired version (or just use the default). Ensure the version you select is supported by the version of MRAA that is installed.

Now is also the time to add any libraries that you need that are not currently in the release. Just add the directory of the driver to the /src directory with the required support files.

We will again be using the out of tree build as described in the documentation. Enter these commands to build and install:

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr

If you want to only install a specific driver change to that drivers directory in build/src and then run:

make 
make install

If you want to install all of upm just run make and make install in the root of the build directory.

Building UPM on an Edison can take a while, so be patient.

That should do it. If there are no error messages you should be ready to utilize the new libraries.

Details can be found on the upm github page.

Getting started with Minnowboard MAX

Update October 12, 2015

MRAA 8.0 and UPM 4.0 have been released recently. These releases offer improvements in both stability and, in the case of UPM, an increase in number the sensors supported. I have updated this post to install the latest versions of this software.

End Update.

After recently receiving my Minnow Board Max  (MBM)  I found that there are few good resources for getting started with it. Unlike the BeagleBone Black I have used before, the MBM does not have a 4GB eMMC that we can program by flashing a prebuilt OS image. Instead we need to use one of the external connectors to provide storage for the OS. We have a choice of using a mini SD card, USB storage, or a SATA2 device. I chose to use an 230 GB OCZ Vector on the SATA port.

The beauty of the MBM is that it is essentially a low powered X64 compatible computer. This opens up many options for OS that we can use. Since I have such a large SSD on the system I chose to dual boot Windows and Linux. I put Windows 8.1 on just because I could, not because I have any plans to develop with it. Surprisingly, 64 bit Windows runs pretty well on the MBM — not nearly as laggy as I expected. But Windows heavily loads the MBM making it run hot compared to running it with Linux. Complete instructions for installing Windows 8.1 on the MBM can be found here.

For my needs Linux is a better choice. I want to be able to take advantage of the MRAA and UPM libraries as well as have a desktop OS on the board. Since we only have 2 gigs of memory on the MBM I want a distro with a very lightweight window manager. I also want to use Ubuntu as I have a long history with it (plus I am not that crazy about Yocto anyway). This lead me to Xubuntu — an Ubuntu distro that utilizes the XFCE window manager. I chose to use version 15.04.

To get the system I am looking for I will be following these steps:

  1. Update BIOS
  2. Install OS
  3. Install Nodejs
  4. Compile and install SWIG 3.0.7.
  5. Compile and Install MRAA 8.0
  6. Compile and install UPM 4.0

Update BIOS: 

My board shipped with version 0.80 of 64 bit BIOS.  As of 9/10/15 the latest bios is 0.82, which can be found here. I used MinnowBoard.MAX.FirmwareUpdateX64 for the update. Instructions for updating using the MBM Flash Utility can be found here.

Install O/S

Xubuntu 15.04 can be downloaded here. Be sure to get the 64 bit version. The image can be burned to a DVD or put on miniSD or thumb drive. The Ubuntu site has some detailed instructions for setting up install media. I chose to copy the Xubuntu image to a thumb drive for the install. The instructions for installing are here – they describe 14.04 but work for 15.04 as well. When complete you should go ahead and do all the updates before proceeding.

Install Nodejs:

Nodejs is available in the Ubuntu repositories, but that version will not work if we try to compile MRAA nor will it allow us to install the version of MRAA that is in NPM. There are a couple of options to get around this. We can use the Nodesource PPA and use APT or we can just download the tarball from the Node site.

The instructions for using the Nodesource PPA are here. Even though they specify 14.04, the instructions are valid for 15.04 as well.

I will use the tarball from the Node site.

It’s a simple install if we use the compiled download so I downloaded the  0.12.x  version of node. The read me in the archive details the install, but it can be difficult to decipher so I will summarize here. All we need to do is copy the files from the archive to /usr/local in the file system. We can accomplish this as follows:

cd /usr/local
sudo tar --strip-components 1 -xzf /path/to/node-v0.12.7-linux-x64.tar.gz

Or use whatever tool you are comfortable with to copy the files to the correct directories. A favorite tool of mine is Midnight Commander (which can be installed via APT).

Since we are copying the files onto the system we will need to set the NODE_PATH environment variable ourselves. This can be done by editing the /etc/environment file. Add the following line to the file:

NODE_PATH="/usr/lib/node_modules"

We need to get the change picked up by the environment. To do so enter:

 sudo source /etc/environment

Alternatively you can reboot the device. If you see node related errors later, check to make sure the environment is set correctly.

Install SWIG

SWIG needs to be installed if we are going to build MRAA. SWIG has a dependency of Perl regular expressions. Install the the needed files as follows:

sudo apt-get install libpcre3 libpcre3-dev

Then download SWIG 3.0.7 from here. Detailed build instructions can be found here. Download and decompress the SWIG source files. Change to the root of the SWIG source and run the following commands:

./configure
make
sudo make install

Install MRAA:

We need build MRAA so that we can take advantage of the Python and Java bindings as well as Node. Detailed build instructions can be found here. I will summarize the steps I followed:

First – install the perquisites. This includes a Java SDK, python dev modules, cmake and git.

sudo apt-get install python-dev python3-dev cmake git

Version 8.0 of MRAA requires JNI 8, so we need to install oracle java. Instructions for doing so can be found here. (The page may look a little funny because I chose the https address). Just follow the install instructions and  test to ensure Java 8 JDK is installed.  The Java home environment variable should be set during the install. If not set it by editing the /etc/environment again by adding JAVA_HOME. Add this line to /etc/environment:

JAVA_HOME="/usr/lib/jvm/java-8-oracle"

To ensure the JAVA_HOME setting is picked up run ‘sudo source /etc/environment’ (or reboot).

Next we need to clone the MRAA repo from git hub:

git clone https://github.com/intel-iot-devkit/mraa.git

Now we are ready to build. We will used the same out of tree build as described in the MRAA documentation.

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr -DBUILDSWIGJAVA=ON
make
sudo make install

Install UPM:

UPM can be cloned with the following:

git clone https://github.com/intel-iot-devkit/upm.git

We will again be using the out of tree build as described in the documentation. Enter these commands to build and install:

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr
make 
sudo make install

Testing:

There are several examples installed in /usr/share/mraa/examples that can be used to test our installation with. These can be copied to a directory we have write access to and modified to test our set up. Be sure to use the MRAA numbers for the MBM pinouts when connecting devices to the system.

Other options:

I chose a Linux setup that worked for me, but there are other options that could be used. There are several options described on the web at Minnowborad.org using different distros of Linux, 32 bit versions of the firmware and alternate BIOS firmware.

So there it is. Hopefully this post will save you some time in getting your MinnowBoard Max configured for use. It took me a couple of days to gather all the info I needed to get my setup working. Feel free to hit me up if you have any questions or found something I did wrong.