cylon/examples/arduinos_and_skynet/multiple_arduinos_one_skyne...

72 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-11-26 01:45:20 +08:00
var Cylon = require('../..');
2014-11-26 01:45:20 +08:00
var arduinos = [
{
name: "arduino0",
port: "/dev/ttyACM0",
devices: {
led00: { driver: 'led', pin: 13 }
}
},
2014-11-26 01:45:20 +08:00
{
name: "arduin01",
port: "/dev/ttyACM1",
devices: {
led10: { driver: 'led', pin: 11 }
led11: { driver: 'led', pin: 12 }
led12: { driver: 'led', pin: 13 }
}
}
2014-11-26 01:45:20 +08:00
];
2014-11-26 01:45:20 +08:00
Cylon.robot({
name: "SkynetBot",
connection: {
name: 'skynet',
adaptor: 'skynet',
uuid: "96630051-a3dc-11e3-8442-5bf31d98c912",
token: "2s67o7ek98pycik98f43reqr90t6s9k9"
},
2014-11-26 01:45:20 +08:00
handler: function(data) {
if (data.payload == null) {
return;
}
console.log("Data: ", data);
2014-11-26 01:45:20 +08:00
for (var i in data.payload.robots) {
var robot = data.payload.robots[i],
bot = Cylon.robots[robot.name];
2014-11-26 01:45:20 +08:00
if (robot.cmd === 'on') {
bot.devices[robot.device].turnOn();
} else {
bot.devices[robot.device].turnOff();
}
2014-11-26 01:45:20 +08:00
}
},
work: function(my) {
my.skynet.on('message', my.handler)
console.log("Skynet is listening");
}
2014-11-26 01:45:20 +08:00
});
2014-11-26 01:45:20 +08:00
arduinos.forEach(function(bot) {
Cylon.robot({
name: bot.name,
2014-11-26 01:45:20 +08:00
connection: { name: 'arduino', adaptor: 'firmata', port: bot.port },
devices: bot.devices,
work: function(my) {
console.log(my.name + " is online");
}
});
});
Cylon.start();