Simple Node.js wrapper + samples



  • Thanks to @Theodore-Borromeo's sick work getting Node cross-compiled for Omega, we can now run JS! Hopefully we'll get an official opkg package in the coming days, but until then...

    1. To install node, download/extract node and libv8.so to a USB stick https://www.dropbox.com/s/f7bvgjfgazhy59i/node-0.10.5-omega.zip?dl=0
    2. Plug USB into you Omega
    3. Run mkdir /mnt/sda1
    4. Run mount /dev/sda1 /mnt/sda1/ (You'll have to repeat this step whenever you reboot the Omega)
    5. Run ln -s /mnt/sda1/libv8.so /usr/lib/libv8.so
    6. Run ln -s /mnt/sda1/node /usr/bin/node
    7. Run node --version — you should see v0.10.5

    To actually do stuff, I've written a bit of a wrapper which can be found here:

    https://github.com/manspaniel/node-omega-gpio

    It has/does these things:

    • onPinChange(pin, callback) for listing for input changes; good for triggering action on button presses.
    • Get/set pin values. Directions are set automatically.
    • Both async and sync functions.
    • Automatically exports pins using echo [pin] > /sys/class/gpio/gpiochip0/subsystem/export — Not always reliable!

    If you get one/more "Error exporting pin" errors, you wont be able to use those pins. Either try running again, or try rebooting your omega (remembering to re-mount the USB after reboot).

    I also have trouble getting pin 8 to do anything (it crashes the program because it can't set the direction), although using fast-gpio works fine. This seems to be a bug with the GPIO exporter, not the wrapper. I also don't seem to be able to do anything with pin 13 — no error, but nothing happens, even with fast-gpio. This one might be a hardware issue or manufacturing defect...

    And finally a sample. This assumes you've wired up an LED to pin 14 (eg. pin 14 > 330ohm resister > long leg of LED > short leg of LED > GND). Just download gpiohelper.js from the GitHub URL above and put it in the same folder as your JS.

    var GPIOHelper = require('./gpiohelper');
    
    var helper = new GPIOHelper();
    var ledPin = 14;
    
    // The state of our light. False for off, true for on.
    var lightState = false;
    
    // Every 1000 miliseconds...
    setInterval(function() {
    	
    	// Toggle the lightState variable. If it was true it will now be false, and vice versa.
    	lightState = !lightState;
    	
    	// FInally, set the light as either on or off depending on lightState
    	helper.setPinSync(ledPin, lightState);
    	
    }, 1000);
    

    There are samples in GitHub. Happy coding! :beer:



  • To blink the on board LED from a web browser, save the code below as htmlblink.js and from the command line run

    # node htmlblink.js
    

    Then point your browser to your device on port 8081
    e.g. http://Omega-ABCD:8081

    var http = require('http');
    var fs = require('fs');
    
    var ledFle = '/sys/class/leds/onion\:amber\:system/brightness';
    
    function led(onOff) {
     fs.writeFile( ledFle, onOff, function(err){
      if (err) {
       return console.log(err);
      }
     });
    }
    
    function handleRequest(req,res) {
     if (req.url=='/ledon') {
      led(1);
     }
     if (req.url=='/ledoff') {
      led(0);
     }
     res.writeHead(200, { "Content-Type": "text/html" });
     res.write("<h1>LED SWITCH</h1>");
     res.write('<a href="/ledon">[ON]</a> | <a href="/ledoff">[OFF]</a> ');
     res.end();
    }
    
    var server = http.createServer(handleRequest);
    
    server.listen(8081, function(){
     console.log("listening for http on port 8081");
    });
    


  • Thank you nought nought. I just tried this and it worked like a charm.



  • Thanks. Has anyone had any success getting npm and/or the node-gyp toolchain working?


  • administrators

    @Christopher-Hiller Node-gyp is a tough thing to get working on the Omega :( I don't think the Omega has enough resources to do the compilation. One idea we've had was to pre-cross-compile some of the popular Node.js modules that require compilation and put the binaries up in a separate npm repository.



  • I like the idea of have a pre compiled library of modules instead of compiling them, but... It puts the onus on someone to make the ones that someone wants and maintain them though. Wouldn't version differences get messy?


  • administrators

    @Chris-Ward Yeah, there are definitely a lot of intricacies involved. But instead of managing the versions entirely through directories and file naming conventions, we might be able to use a git server to do it. npm can install modules directly from a git repository.



  • @Dan-L. You sir, deserve a reward! Thank you so much! I can finally actually run Node JS on the Onion Omega!



  • @Austin-Kurpuis said:

    @Dan-L. You sir, deserve a reward! Thank you so much! I can finally actually run Node JS on the Onion Omega!

    buahahahaha. Good work :)



  • @Boken-Lin Just wanted to point out that the gentleman who actually created the cross-compilation toolchain I wrapped in a docker container has a script (that I not so coincidentally left in there) that should also cross-compile npm.

    From there, you could define the manifests for nom packages to definitely pull pre-cross-compiled binaries. . .


  • administrators

    @Theodore-Borromeo Yup, you are right! We're on it!



  • @Dan-L. Node uses NPM for installing packages. I'm fairly new to node, and I'd like to get the twit package installed. Any ideas on how to achieve this on the Omega? Thanks!


  • administrators

    @Steve-Fister Since twit is a module that's entirely native (does not have any binary components), you can use npm to install it on your computer, and copy the file to the Omega. Here's a tarball for twit: https://s3-us-west-2.amazonaws.com/onion-cdn/community/openwrt/twit.tar.gz.



  • @Boken-Lin Thank you!


Log in to reply