├── AUTHORS ├── README.md ├── lib └── arduino.js ├── package.json ├── src └── node.pde └── test └── test.js /AUTHORS: -------------------------------------------------------------------------------- 1 | # Authors ordered by first contribution. 2 | 3 | Tobias Schneider 4 | Chris Williams 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Node Arduino 2 | ============ 3 | 4 | Version: 0.2.3 - Released March 20, 2011 5 | 6 | ***** 7 | 8 | This package is designed to allow you to control and program an arduino chipset with JavaScript. It leverages the node-serialport code base, so ensure that you have that installed. In order to see what this package can do, please watch [this presentation from JSConf EU 2010](http://jsconf.eu/2010/speaker/livingroombindmotion_function.html) by [Nikolai Onken](http://twitter.com/nonken) and [Jörn Zaefferer](http://bassistance.de/). 9 | 10 | ***** 11 | 12 | How To Use 13 | ========== 14 | 15 | Using node-arduino is pretty easy because it is pretty basic. It is essentially a wrapper with arduino specific message handling around the node-serialport library. 16 | 17 | To Install 18 | ---------- 19 | 20 |
21 |   npm install serialport arduino
22 | 
23 | 24 | To Use 25 | ------ 26 | 27 | Opening an arduino board: 28 | 29 |
30 |   var arduino = require("arduino");
31 |   var myBoard = arduino.connect("/dev/tty-usbserial1");
32 | 
33 | 34 | Alpha 35 | ----- 36 | 37 | This code is still very much alpha and early stage, but that just makes it more fun to work with. If you find an issue/problem, fork it and issue a pull request. No matter what, enjoy and do cool things with this code. -------------------------------------------------------------------------------- /lib/arduino.js: -------------------------------------------------------------------------------- 1 | /* 2 | * node-arduino: Control your Arduino with Node 3 | * 4 | * Copyright (c) 2010 Tobias Schneider 5 | * node-arduino is freely distributable under the terms of the MIT license. 6 | */ 7 | 8 | var sys = require('sys') 9 | , SerialPort = require('serialport').SerialPort 10 | ; 11 | 12 | const SERIAL_BAUDRATE = 9600; 13 | 14 | const OPC_PIN_MODE = 0x01; 15 | const OPC_DIGITAL_READ = 0x02; 16 | const OPC_DIGITAL_WRITE = 0x03; 17 | const OPC_ANALOG_REFERENCE = 0x04; 18 | const OPC_ANALOG_READ = 0x05; 19 | const OPC_ANALOG_WRITE = 0x06; 20 | 21 | exports.HIGH = 0x01; 22 | exports.LOW = 0x00; 23 | 24 | exports.INPUT = 0x00; 25 | exports.OUTPUT = 0x01; 26 | 27 | exports.true = 0x01; 28 | exports.false = 0x00; 29 | 30 | exports.EXTERNAL = 0x00; 31 | exports.DEFAULT = 0x01; 32 | exports.INTERNAL = 0x03; 33 | 34 | Board = function (path) { 35 | this.sp = new SerialPort(path, {baudrate: SERIAL_BAUDRATE}); 36 | } 37 | 38 | Board.prototype = { 39 | pinMode : function (pin, mode) { 40 | this.sp.write(new Buffer([OPC_PIN_MODE, pin, mode]), 3); 41 | } 42 | 43 | , digitalRead : function (pin) { 44 | // TODO 45 | } 46 | 47 | , digitalWrite : function (pin, val) { 48 | this.sp.write(new Buffer([OPC_DIGITAL_WRITE, pin, val]), 3); 49 | } 50 | 51 | , analogReference : function (type) { 52 | this.sp.write(new Buffer([OPC_ANALOG_REFERENCE, type]), 2); 53 | } 54 | 55 | , analogRead : function (pin) { 56 | // TODO 57 | } 58 | 59 | , analogWrite : function (pin, val) { 60 | this.sp.write(new Buffer([OPC_ANALOG_WRITE, pin, val]), 3); 61 | }, 62 | 63 | close: function () { 64 | this.sp.close() 65 | } 66 | 67 | }; 68 | 69 | exports.connect = function (path) { 70 | return new Board(path); 71 | }; 72 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { "name" : "arduino", 2 | "version" : "0.2.3", 3 | "description" : "Control your Arduino with Node", 4 | "author": "Tobias Schneider ", 5 | "main": "./lib/arduino", 6 | "contributors": [ 7 | { "name": "Tobias Schneider", "email": "schneider@uxebu.com" }, 8 | { "name": "Chris Williams", "email": "voodootikigod@gmail.com" } 9 | ], 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/voodootikigod/node-arduino.git" 13 | }, "dependencies": { 14 | "serialport": ">= 0.2.2" 15 | }, 16 | "keywords": ["arduino", "serialport", "robots"], 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/voodootikigod/node-arduino.git" 20 | }, 21 | "engines": { "node": ">= 0.4.1 < 0.5.0" } 22 | } 23 | -------------------------------------------------------------------------------- /src/node.pde: -------------------------------------------------------------------------------- 1 | /* 2 | * node-arduino: Control your Arduino with Node 3 | * 4 | * Copyright (c) 2010 Tobias Schneider 5 | * node-arduino is freely distributable under the terms of the MIT license. 6 | */ 7 | 8 | #define SERIAL_BAUDRATE 9600 9 | 10 | #define OPC_PIN_MODE 0x01 11 | #define OPC_DIGITAL_READ 0x02 12 | #define OPC_DIGITAL_WRITE 0x03 13 | #define OPC_ANALOG_REFERENCE 0x04 14 | #define OPC_ANALOG_READ 0x05 15 | #define OPC_ANALOG_WRITE 0x06 16 | 17 | void setup() { 18 | Serial.begin(SERIAL_BAUDRATE); 19 | } 20 | 21 | void loop() { 22 | while (Serial.available() > 0) { 23 | switch (Serial.read()) { 24 | case OPC_PIN_MODE: { 25 | Serial.println("pinMode"); 26 | pinMode(Serial.read(), Serial.read()); 27 | break; 28 | } 29 | case OPC_DIGITAL_READ: { 30 | digitalRead(Serial.read()); 31 | break; 32 | } 33 | case OPC_DIGITAL_WRITE: { 34 | Serial.println("digitalWrite"); 35 | digitalWrite(Serial.read(), Serial.read()); 36 | break; 37 | } 38 | case OPC_ANALOG_REFERENCE: { 39 | analogReference(Serial.read()); 40 | break; 41 | } 42 | case OPC_ANALOG_READ: { 43 | analogRead(Serial.read()); 44 | break; 45 | } 46 | case OPC_ANALOG_WRITE: { 47 | analogWrite(Serial.read(), Serial.read()); 48 | break; 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /* 2 | * node-arduino: Control your Arduino with Node 3 | * 4 | * Copyright (c) 2010 Tobias Schneider 5 | * node-arduino is freely distributable under the terms of the MIT license. 6 | */ 7 | 8 | var arduino = require('../lib/arduino') 9 | , board = arduino.connect('/dev/tty.usbserial-A700fkLn') 10 | , val = arduino.LOW 11 | ; 12 | 13 | board.pinMode(13, arduino.OUTPUT); 14 | 15 | setInterval(function () { 16 | val = val == arduino.LOW ? arduino.HIGH : arduino.LOW; 17 | 18 | board.digitalWrite(13, val); 19 | }, 500); 20 | --------------------------------------------------------------------------------