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...
- To install node, download/extract
node
andlibv8.so
to a USB stick https://www.dropbox.com/s/f7bvgjfgazhy59i/node-0.10.5-omega.zip?dl=0 - Plug USB into you Omega
- Run
mkdir /mnt/sda1
- Run
mount /dev/sda1 /mnt/sda1/
(You'll have to repeat this step whenever you reboot the Omega) - Run
ln -s /mnt/sda1/libv8.so /usr/lib/libv8.so
- Run
ln -s /mnt/sda1/node /usr/bin/node
- Run
node --version
ā you should seev0.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!