Merge pull request #129 from hybridgroup/multi-arduino-skynet

Examples with multiple arduinos and skynet connections.
This commit is contained in:
Ron Evans 2014-03-06 14:54:22 -08:00
commit ea83c58097
3 changed files with 131 additions and 5 deletions

View File

@ -0,0 +1,59 @@
var Cylon = require('../..');
SkynetBot = (function(){
function SkynetBot(){
this.connections = [
{ name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' },
{ name: 'skynet', adaptor: 'skynet'}
];
}
SkynetBot.prototype.port = function(serialPort){
for (var i in this.connections){
if (this.connections[i].adaptor == 'firmata')
this.connections[i].port = serialPort;
}
};
SkynetBot.prototype.creds = function(uuid, token, portNumber){
for (var i in this.connections){
if (this.connections[i].adaptor == 'skynet'){
this.connections[i].uuid = uuid;
this.connections[i].token = token;
this.connections[i].portNumber = portNumber;
}
}
};
SkynetBot.prototype.device = { name: 'led13', driver: 'led', pin: 13, connection: 'arduino' };
SkynetBot.prototype.work = function(my) {
my.skynet.on('message', function(data) {
if(data.led13 == 'on') {
my.led13.turnOn()
}
else if(data.led13 == 'off') {
my.led13.turnOff()
}
});
Logger.info("Skynet instance `" + my.name + "` is listening ...");
};
return SkynetBot;
})();
skynetBot0 = new SkynetBot();
skynetBot0.name = 'skynet0';
skynetBot0.port('/dev/ttyACM1');
skynetBot0.creds("96630051-a3dc-11e3-8442-5bf31d98c912", "2s67o7ek98pycik98f43reqr90t6s9k9");
skynetBot1 = new SkynetBot();
skynetBot1.name = 'skynet1';
skynetBot1.creds("e8f942f1-a49c-11e3-9270-795e22e700d8","0lpxpyafz7z7u8frgvp44g8mbr7o80k9");
Cylon.robot(skynetBot0);
Cylon.robot(skynetBot1);
Cylon.start();

View File

@ -0,0 +1,64 @@
var Cylon = require('..'),
arduino1, arduino2;
Arduino = (function(){
function Arduino(){}
Arduino.prototype.connection = { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' };
Arduino.prototype.work = function(my) {
console.log("Name =====>");
console.log(my.name);
}
return Arduino;
})();
skynet = {
connections: [
{ name: 'skynet',
adaptor: 'skynet',
uuid: "96630051-a3dc-11e3-8442-5bf31d98c912", token: "2s67o7ek98pycik98f43reqr90t6s9k9" }
],
work: function(my) {
Logger.info("Skynet is listening...");
my.skynet.on('message', function(data) {
console.log(data);
if (data.payload != null){
var robot,
robots = data.payload.robots;
for(var index in robots){
robot = robots[index];
console.log(robot);
my.master.findRobot(robot.name, function(err, bot){
if (robot.cmd == 'on')
bot.devices[robot.device].turnOn();
else
bot.devices[robot.device].turnOff();
});
}
}
});
}
}
Cylon.robot(skynet);
arduino0 = new Arduino();
console.log(arduino0);
arduino0.name = 'arduino0';
arduino0.device = {name: 'led00', driver: 'led', pin: 13};
Cylon.robot(arduino0);
arduino1 = new Arduino();
arduino1.name = 'arduino1'
arduino1.connection.port = '/dev/ttyACM1';
arduino1.devices = [
{name: 'led10', driver: 'led', pin: 11},
{name: 'led11', driver: 'led', pin: 12},
{name: 'led12', driver: 'led', pin: 13}
];
Cylon.robot(arduino1);
Cylon.start();

View File

@ -3,22 +3,25 @@ var Cylon = require('../..');
Cylon.robot({
connections: [
{ name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' },
{ name: 'skynet', adaptor: 'skynet', uuid: "742401f1-87a4-11e3-834d-670dadc0ddbf", token: "xjq9h3yzhemf5hfrme8y08fh0sm50zfr" }
{ name: 'skynet', adaptor: 'skynet',
uuid: "96630051-a3dc-11e3-8442-5bf31d98c912", token: "2s67o7ek98pycik98f43reqr90t6s9k9" }
],
device: { name: 'led', driver: 'led', pin: 13, connection: 'arduino' },
device: { name: 'led13', driver: 'led', pin: 13, connection: 'arduino' },
work: function(my) {
Logger.info("connected...");
Logger.info("Skynet is listening...");
my.skynet.on('message', function(data) {
Logger.info(data);
var data = JSON.parse(data);
if(data.message.red == 'on') {
my.led.turnOn()
my.led13.turnOn()
}
else if(data.message.red == 'off') {
my.led.turnOff()
my.led13.turnOff()
}
});
}
}).start();