Created
February 11, 2014 00:30
-
-
Save sandeepmistry/8927095 to your computer and use it in GitHub Desktop.
Arduino Yun: bleno + johnny-five
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('util'); | |
var bleno = require('bleno'); | |
var five = require("johnny-five"); | |
var BlenoPrimaryService = bleno.PrimaryService; | |
var BlenoCharacteristic = bleno.Characteristic; | |
var BlenoDescriptor = bleno.Descriptor; | |
var board = new five.Board({ | |
port: '/dev/ttyATH0' | |
}); | |
var led = null; | |
console.log('yun - bleno + johnny-five'); | |
board.on('ready', function() { | |
console.log('board on -> ready'); | |
led = new five.Led(13); | |
}); | |
var LedCharacteristic = function() { | |
LedCharacteristic.super_.call(this, { | |
uuid: '6C656420202020202020202020202020', | |
properties: ['write', 'writeWithoutResponse'] | |
}); | |
}; | |
util.inherits(LedCharacteristic, BlenoCharacteristic); | |
LedCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) { | |
console.log('LedCharacteristic write request: ' + data.toString('hex') + ' ' + offset + ' ' + withoutResponse); | |
if (data[0]) { | |
led.on(); | |
} else { | |
led.off(); | |
} | |
callback(this.RESULT_SUCCESS); | |
}; | |
function ArduinoYunService() { | |
ArduinoYunService.super_.call(this, { | |
uuid: '61726475696E6F20202020202079756E', | |
characteristics: [ | |
new LedCharacteristic() | |
] | |
}); | |
} | |
util.inherits(ArduinoYunService, BlenoPrimaryService); | |
bleno.on('stateChange', function(state) { | |
console.log('bleno on -> stateChange: ' + state); | |
if (state === 'poweredOn') { | |
bleno.startAdvertising('yun', ['61726475696E6F20202020202079756E']); | |
} else { | |
bleno.stopAdvertising(); | |
} | |
}); | |
bleno.on('advertisingStart', function(error) { | |
console.log('bleno on -> advertisingStart: ' + (error ? 'error ' + error : 'success')); | |
if (!error) { | |
bleno.setServices([ | |
new ArduinoYunService() | |
]); | |
} | |
}); | |
bleno.on('advertisingStop', function() { | |
console.log('bleno on -> advertisingStop'); | |
}); | |
bleno.on('servicesSet', function() { | |
console.log('bleno on -> servicesSet'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment