Archives For Node.js

raspberry pi and java 8

March 27, 2014

What you’re looking at is the SwingSet2 demo, something rather old now, running via the latest Java release, Java 8 on my Raspberry Pi, on the twm desktop and on top of the latest Arch Linux for ARM. What a mouthful…

It’s been almost a month now since my last Raspberry Pi post; I was on a business trip in Kansas for three weeks. During that period of time Oracle released Java 8 on 18 March. One of the Java implementations was for ARM V6, compiled for hard float (chip-supported floating point math). As a test I also dropped the demo package on my RPi and fired up one of the demos. To be honest it was slow starting, but not horribly slow. It was slow like my computers from a good decade ago, the ones running early Pentium chips (back when Intel still called their chips Pentiums). I don’t intend to run graphical Java applications as much as service type applications.

I’m looking at Java 8 to run JavaScript via Java 8’s built-in JavaScript engine, Nashorn. The key reason is not to abandon node.js, but to shift the JavaScript portion (if possible) to run on Nashorn as apposed to Google’s V8. There are a number of reasons for doing this, one of which is security; I believe Java 8 has far more than V8. Before you bust a gasket telling me about Java’s security sins, know that the vector for Java security breaches was with older versions of Java using applets. That’s not what we’re about here. A second key reason is that I can call down into the Java libraries via JavaScript. And I can investigate manipulating the RPi’s various I/O devices via Java 8.

I don’t know when I’ll release a new image. Compressed, my 8GB images are now at 1GB, which makes it a chore to upload them, and I’m sure a chore for you to download. Although I’ve seen downloads from Sourceforge, they’ve pretty much dried up. I may make an image with Java 8 available on April 1, like I had originally intended. It’ll have Arch Linux fully up to date as well as Java 8.

For now tax season looms and other work is making my life very busy. I’ve got another two week business trip coming up, and more business travel after that. It’s a long busy stretch ahead.


I installed Apache ant and built one of the sample applications, Scriptpad. In this simple example JavaScript is calling Java’s JFrame class and instantiating a running instance with a simple title. It took all of five lines of JavaScript to invoke a Java class instance. Not too shabby. This is, of course, just a trivial proof of concept. But it does illustrate the power of the JVM.

cylon single

Yesterday marked the second birthday of the Raspberry Pi. It was two years ago on 1 March 2012 (well, actually, 29 February, as 2012 was a leap year) that the Raspberry Pi was officially released (after months of speculation and pre-release ordering). It was an instant hit, selling 100,000 on its first day. Since that inaugural date it’s passed the 2.5 million mark, and continues on at a healthy pace. Mine were purchased a year ago March, but due to my circumstances I didn’t really start to dig in with the R-Pi until around Christmas.

Most of my time has been spent on “loftier” goals, such as building a development environment on the R-Pi using Arch Linux Arm. Arch Linux has turned out to be a good choice for me, better than the others originally presented for the R-Pi. I’ve also installed and gotten used to writing applications in Javascript on the R-Pi using Node.js. Using all that I’ve learned so far I wrote the following little script to flash four LEDs back and forth using the GPIO pins. I would have used two, but it looked a bit silly with just two LEDs going back and forth, so I added two more.

// NOTE: Code written for Raspberry Pi Model B.
// NOTE: Pin 13 is GPIO 27
var Gpio = require('../onoff').Gpio,
led1 = new Gpio(17, 'out'), // Header pin 11
led2 = new Gpio(18, 'out'), // Header pin 12
led3 = new Gpio(27, 'out'), // Header pin 13
led4 = new Gpio(22, 'out'), // Header pin 15
shifter = 1,
multiplier = 2;

// LEDs have 50 ms on period. Loop period is 200 ms.
iv1 = setInterval(function() {
    led1.writeSync(shifter & 1 ? 1 : 0); // 1 = on, 0 = off
    led2.writeSync(shifter & 2 ? 1 : 0);
    led3.writeSync(shifter & 4 ? 1 : 0);
    led4.writeSync(shifter & 8 ? 1 : 0);
    shifter *= multiplier;
    if (shifter > 4) multiplier = .5;
    if (shifter < 2) multiplier = 2;
}, 50);

// Stop blinking the LEDs and turn them all off after 10 seconds.
setTimeout(function() {
    clearInterval(iv1); // Stop blinking
    led1.writeSync(0);  // Turn LED 1 off.
    led1.unexport();    // Unexport GPIO and free resources
    led2.writeSync(0);  // Turn LED 2 off.
    led2.unexport();    // Unexport GPIO and free resources
    led3.writeSync(0);  // Turn LED 3 off.
    led3.unexport();    // Unexport GPIO and free resources
    led4.writeSync(0);  // Turn LED 4 off.
    led4.unexport();    // Unexport GPIO and free resources
}, 10000);

Following is a sample video clip showing the LEDs in action. I apologize in advance for requiring Flash to view this. I uploaded this to Flickr, and I guess Flickr hasn’t gotten the memo yet on HTML5 video.

The key to flashing back and forth is lines 17, 18, and 19. Line 17 shifts a single bit left or right depending on the multiplier, which can either be 2 or 1/2 (.5). Lines 18 and 19 decide when to set the multiplier to either shift right (2) or left (.5). I tried to use the bit operators in Javascript, but found that the code would have been a bit clunkier and longer if I had. I could have also put an ‘else’ clause in front of the second if statement, but decided not to. We’re not dealing with time critical testing here, and I preferred short, simple statements in this example.

If the code looks a bit familiar, it should. I started with the flash_led.js example in the onoff examples directory, and hacked it from there. It took me about 30 minutes of tinkering and hacking to get it to work. I would have done it in half the time except I was switching my attention between this and “Gravity” on the Blu-ray player.

I also decided to use a simpler breadboard as apposed to the larger, more sophisticated Parallax Professional board. I’m using the board for which it was intended, as a Propeller development board, and using I2C to tie the R-Pi to the Propeller, with the Propeller and the R-Pi working together, doing some interesting things for me.

Here’s hoping I don’t get bored with the R-Pi or allow some other major event to stop me from working with it.

Camera Used

I used my Olympus E-M5 with the Panasonic 1.4/25mm sitting on a Gorilla Hybrid. It was a nice compact rig that allowed me to set the camera fairly close to the R-Pi and the LEDs. I was generally pleased with the output (we’re not talking Oscar material here), but I was somewhat annoyed with the very minor shaking I see going on in the video. I thought I had the vaunted five axis stabilization enabled when I hit the video record button. I need to go back and see just how all that is set up.

I’ve been working with Node.js on the Raspberry Pi, particularly with regards to the web application framework Express and the page template engine Jade. My past experiences with web tools like these goes back to the late 1990s with Microsoft’s early IIS and ASP pages, IE 4, msxml 0.8 and Microsoft’s initial Dynamic HTML implemention, through plain old Apache 1.0 with Perl, to Tomcat and JSP pages and Ruby on Rails to Node.js and Express and Jade with a side trip to EJS.

I’ve been following a tutorial titled “Creating a basic site with Node.js and Express” to create an initial application. Interestingly enough, even though the tutorial was written back in April 2011, it was still reasonably up to date. I had one deprecated call in app.js (replacing “app.use(express.bodyParser())” with “app.use(express.json())” and “app.use(express.urlencoded())”) and one error in a Jade template file (replace “!!! 5” with “doctype html” in views/layout.jade). Once those minor fixes were in place I was able to start up my very simple Node web server and have it deliver the sample page (see below) to my various home devices on my internal home network. You see a screen capture on the Nexus 7 2012 running Opera. If I’m going to use Node on my R-Pi, then I’m going to make full use of it. The screen capture above shows the system resources via htop in the upper left window, and the simple Node application with logging running in the upper right window.

As usual everything is behind a firewall.

My reason for digging into this part of Node is to build a web interface into the i2c and onoff Node modules, and thus the Raspberry Pi’s I/O capabilities. My next release of my Arch Linux image will contain all of this (though not necessarily the final web interface into the Raspberry Pi’s I/O), hopefully this weekend. I will only update the graphic image, not the base image.

Today was a pretty good day playing around with the Raspberry Pi. First thing I was able to do was fix my problem with the SDHC flash file system. That was fixed with fsck. See the updates from 16 February.

The second was to test the node.js module onoff. I ran the straightforward tests using my Parallax Propeller Professional Development Board. I flashed a few LEDs and pressed a button on the board simply wired into the Raspberry Pi’s GPIO header. You can see the run above.

One interesting issue seems to be the switch inputs (GPIO 18/pin 12). The switch inputs need debouncing, either in hardware or software.

But those are issues for another time, another experiment. Right now I’ve regained confidence in the overall board and its software. And for me that’s a very good thing.