From 4a3fdf7fec47ee0d3aa7165a6f99ac51989d7636 Mon Sep 17 00:00:00 2001 From: edgarsilva Date: Wed, 5 Mar 2014 14:50:39 -0600 Subject: [PATCH 1/5] WIP on multipoe arduino controlled by skynet. --- ...rduinos_with_one_skynet_connection_each.js | 62 ++++++++++ ...onnection_controlling_multiple_arduinos.js | 64 ++++++++++ examples/multiple_arduinos.js | 64 ++++++++++ examples/pure-skynet.js | 113 ++++++++++++++++++ examples/skynet/skynet-blink.js | 13 +- lib/basestar.js | 3 + 6 files changed, 314 insertions(+), 5 deletions(-) create mode 100644 examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js create mode 100644 examples/multi_platform/one_skynet_connection_controlling_multiple_arduinos.js create mode 100644 examples/multiple_arduinos.js create mode 100644 examples/pure-skynet.js diff --git a/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js b/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js new file mode 100644 index 0000000..509ce5e --- /dev/null +++ b/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js @@ -0,0 +1,62 @@ +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; +})(); + +skynetBot1 = new SkynetBot(); +skynetBot1.name = 'skynet1'; +skynetBot1.port('/dev/ttyACM1'); +skynetBot1.creds("e8f942f1-a49c-11e3-9270-795e22e700d8","0lpxpyafz7z7u8frgvp44g8mbr7o80k9"); +console.log(skynetBot1.connections); +Cylon.robot(skynetBot1); + +skynetBot0 = new SkynetBot(); +skynetBot0.name = 'skynet0'; +//console.log(skynetBot0.connections); +skynetBot0.creds("96630051-a3dc-11e3-8442-5bf31d98c912", "2s67o7ek98pycik98f43reqr90t6s9k9"); +console.log(skynetBot0.connections); +console.log("==========================================="); +Cylon.robot(skynetBot0); + +Cylon.start(); diff --git a/examples/multi_platform/one_skynet_connection_controlling_multiple_arduinos.js b/examples/multi_platform/one_skynet_connection_controlling_multiple_arduinos.js new file mode 100644 index 0000000..df6d12a --- /dev/null +++ b/examples/multi_platform/one_skynet_connection_controlling_multiple_arduinos.js @@ -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(); diff --git a/examples/multiple_arduinos.js b/examples/multiple_arduinos.js new file mode 100644 index 0000000..48c4d2f --- /dev/null +++ b/examples/multiple_arduinos.js @@ -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: "e8f942f1-a49c-11e3-9270-795e22e700d8", token: "0lpxpyafz7z7u8frgvp44g8mbr7o80k9" } + ], + + 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(); diff --git a/examples/pure-skynet.js b/examples/pure-skynet.js new file mode 100644 index 0000000..1d0acc0 --- /dev/null +++ b/examples/pure-skynet.js @@ -0,0 +1,113 @@ +var skynet = require('skynet'); + +var conn = skynet.createConnection({ + "uuid": "96630051-a3dc-11e3-8442-5bf31d98c912", + "token": "2s67o7ek98pycik98f43reqr90t6s9k9" + "protocol": "websocket", // or "websocket" + "qos": 0, // MQTT Quality of Service (0=no confirmation, 1=confirmation, 2=N/A) + "host": "localhost", // optional - defaults to http://skynet.im + "port": 80 // optional - defaults to 80 +}); + +conn.on('notReady', function(data){ + console.log('UUID FAILED AUTHENTICATION!'); + console.log(data); +}); + +conn.on('ready', function(data){ + console.log('UUID AUTHENTICATED!'); + console.log(data); + + // Subscribe to device + conn.subscribe({ + "uuid": "96630051-a3dc-11e3-8442-5bf31d98c912", + "token": "2s67o7ek98pycik98f43reqr90t6s9k9" + }, function (data) { + console.log(data); + }); + + // Subscribe to device + //conn.unsubscribe({ + //"uuid": "f828ef20-29f7-11e3-9604-b360d462c699" + //}, function (data) { + //console.log(data); + //}); + + // Send and receive messages + conn.message({ + "devices": "*", + "payload": { + "skynet":"online" + }, + "qos": 0 + }); + conn.message({ + "devices": "96630051-a3dc-11e3-8442-5bf31d98c912", + "payload": { + "skynet":"online 2" + }, + "qos": 0 + }); + conn.message({ + "devices": "96630051-a3dc-11e3-8442-5bf31d98c912", + "payload": { + "skynet":"online 3" + }, + "qos": 0 + }); + + conn.on('message', function(channel, message){ + console.log('message received', channel, message); + }); + + + // Event triggered when device loses connection to skynet + conn.on('disconnect', function(data){ + console.log('disconnected from skynet'); + }); + + // Register a device (note: you can leave off the token to have skynet generate one for you) + //conn.register({ + //"token": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9", + //"type": "drone" + //}, function (data) { + //console.log(data); + //}); + + // UnRegister a device + //conn.unregister({ + //"uuid": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9", + //"token": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9" + //}, function (data) { + //console.log(data); + //}); + + + // Update device + //conn.update({ + //"uuid":"ad698900-2546-11e3-87fb-c560cb0ca47b", + //"token": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9", + //"armed":true + //}, function (data) { + //console.log(data); + //}); + + // WhoAmI? + conn.whoami({"uuid":"96630051-a3dc-11e3-8442-5bf31d98c912"}, function (data) { + console.log(data); + }); + + // Receive an array of device UUIDs based on user defined search criteria + conn.devices({ + "type":"drone" + }, function (data) { + console.log(data); + }); + + // Skynet status + conn.status(function (data) { + console.log(data); + }); + +}); + diff --git a/examples/skynet/skynet-blink.js b/examples/skynet/skynet-blink.js index 6603e28..ca82d81 100644 --- a/examples/skynet/skynet-blink.js +++ b/examples/skynet/skynet-blink.js @@ -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(); diff --git a/lib/basestar.js b/lib/basestar.js index 6a9f5fd..74aa116 100644 --- a/lib/basestar.js +++ b/lib/basestar.js @@ -55,9 +55,12 @@ namespace("Cylon", function() { opts.sendUpdate = opts.sendUpdate || false; opts.targetEventName = opts.targetEventName || opts.eventName; + console.log(opts.eventName); + opts.source.on(opts.eventName, function() { var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : []; args.unshift(opts.targetEventName); + console.log(opts.eventName); opts.target.emit.apply(opts.target, args); if (opts.sendUpdate) { From 86ba2698eaf822107411af7e95b4e6d4b986d67f Mon Sep 17 00:00:00 2001 From: edgarsilva Date: Thu, 6 Mar 2014 00:19:26 -0600 Subject: [PATCH 2/5] WIP on multi skynet connections. --- ...rduinos_with_one_skynet_connection_each.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js b/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js index 509ce5e..baba8c9 100644 --- a/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js +++ b/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js @@ -44,19 +44,18 @@ SkynetBot = (function(){ return SkynetBot; })(); -skynetBot1 = new SkynetBot(); -skynetBot1.name = 'skynet1'; -skynetBot1.port('/dev/ttyACM1'); -skynetBot1.creds("e8f942f1-a49c-11e3-9270-795e22e700d8","0lpxpyafz7z7u8frgvp44g8mbr7o80k9"); -console.log(skynetBot1.connections); -Cylon.robot(skynetBot1); - skynetBot0 = new SkynetBot(); skynetBot0.name = 'skynet0'; -//console.log(skynetBot0.connections); +skynetBot0.port('/dev/ttyACM1'); skynetBot0.creds("96630051-a3dc-11e3-8442-5bf31d98c912", "2s67o7ek98pycik98f43reqr90t6s9k9"); -console.log(skynetBot0.connections); -console.log("==========================================="); + +skynetBot1 = new SkynetBot(); +skynetBot1.name = 'skynet1'; +skynetBot1.creds("e8f942f1-a49c-11e3-9270-795e22e700d8","0lpxpyafz7z7u8frgvp44g8mbr7o80k9"); + Cylon.robot(skynetBot0); +Cylon.robot(skynetBot1); Cylon.start(); + + From 490a74d00b2de75ff129ab4300c746901f9fbdcd Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Thu, 6 Mar 2014 15:59:28 -0600 Subject: [PATCH 3/5] Add examples for multiple arduinos and multiple skynet connections. --- ...rduinos_with_one_skynet_connection_each.js | 2 - examples/multiple_arduinos.js | 64 ------------------- 2 files changed, 66 deletions(-) delete mode 100644 examples/multiple_arduinos.js diff --git a/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js b/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js index baba8c9..0298348 100644 --- a/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js +++ b/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js @@ -57,5 +57,3 @@ Cylon.robot(skynetBot0); Cylon.robot(skynetBot1); Cylon.start(); - - diff --git a/examples/multiple_arduinos.js b/examples/multiple_arduinos.js deleted file mode 100644 index 48c4d2f..0000000 --- a/examples/multiple_arduinos.js +++ /dev/null @@ -1,64 +0,0 @@ -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: "e8f942f1-a49c-11e3-9270-795e22e700d8", token: "0lpxpyafz7z7u8frgvp44g8mbr7o80k9" } - ], - - 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(); From 57066668ab2a2e0aed467723716fd9295dc3bbe6 Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Thu, 6 Mar 2014 16:13:20 -0600 Subject: [PATCH 4/5] Remove unnecessary example for pure-skynet and remove log messages from basestar. --- examples/pure-skynet.js | 113 ---------------------------------------- lib/basestar.js | 3 -- 2 files changed, 116 deletions(-) delete mode 100644 examples/pure-skynet.js diff --git a/examples/pure-skynet.js b/examples/pure-skynet.js deleted file mode 100644 index 1d0acc0..0000000 --- a/examples/pure-skynet.js +++ /dev/null @@ -1,113 +0,0 @@ -var skynet = require('skynet'); - -var conn = skynet.createConnection({ - "uuid": "96630051-a3dc-11e3-8442-5bf31d98c912", - "token": "2s67o7ek98pycik98f43reqr90t6s9k9" - "protocol": "websocket", // or "websocket" - "qos": 0, // MQTT Quality of Service (0=no confirmation, 1=confirmation, 2=N/A) - "host": "localhost", // optional - defaults to http://skynet.im - "port": 80 // optional - defaults to 80 -}); - -conn.on('notReady', function(data){ - console.log('UUID FAILED AUTHENTICATION!'); - console.log(data); -}); - -conn.on('ready', function(data){ - console.log('UUID AUTHENTICATED!'); - console.log(data); - - // Subscribe to device - conn.subscribe({ - "uuid": "96630051-a3dc-11e3-8442-5bf31d98c912", - "token": "2s67o7ek98pycik98f43reqr90t6s9k9" - }, function (data) { - console.log(data); - }); - - // Subscribe to device - //conn.unsubscribe({ - //"uuid": "f828ef20-29f7-11e3-9604-b360d462c699" - //}, function (data) { - //console.log(data); - //}); - - // Send and receive messages - conn.message({ - "devices": "*", - "payload": { - "skynet":"online" - }, - "qos": 0 - }); - conn.message({ - "devices": "96630051-a3dc-11e3-8442-5bf31d98c912", - "payload": { - "skynet":"online 2" - }, - "qos": 0 - }); - conn.message({ - "devices": "96630051-a3dc-11e3-8442-5bf31d98c912", - "payload": { - "skynet":"online 3" - }, - "qos": 0 - }); - - conn.on('message', function(channel, message){ - console.log('message received', channel, message); - }); - - - // Event triggered when device loses connection to skynet - conn.on('disconnect', function(data){ - console.log('disconnected from skynet'); - }); - - // Register a device (note: you can leave off the token to have skynet generate one for you) - //conn.register({ - //"token": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9", - //"type": "drone" - //}, function (data) { - //console.log(data); - //}); - - // UnRegister a device - //conn.unregister({ - //"uuid": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9", - //"token": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9" - //}, function (data) { - //console.log(data); - //}); - - - // Update device - //conn.update({ - //"uuid":"ad698900-2546-11e3-87fb-c560cb0ca47b", - //"token": "zh4p7as90pt1q0k98fzvwmc9rmjkyb9", - //"armed":true - //}, function (data) { - //console.log(data); - //}); - - // WhoAmI? - conn.whoami({"uuid":"96630051-a3dc-11e3-8442-5bf31d98c912"}, function (data) { - console.log(data); - }); - - // Receive an array of device UUIDs based on user defined search criteria - conn.devices({ - "type":"drone" - }, function (data) { - console.log(data); - }); - - // Skynet status - conn.status(function (data) { - console.log(data); - }); - -}); - diff --git a/lib/basestar.js b/lib/basestar.js index 74aa116..6a9f5fd 100644 --- a/lib/basestar.js +++ b/lib/basestar.js @@ -55,12 +55,9 @@ namespace("Cylon", function() { opts.sendUpdate = opts.sendUpdate || false; opts.targetEventName = opts.targetEventName || opts.eventName; - console.log(opts.eventName); - opts.source.on(opts.eventName, function() { var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : []; args.unshift(opts.targetEventName); - console.log(opts.eventName); opts.target.emit.apply(opts.target, args); if (opts.sendUpdate) { From f761c22849e9f41388f03834da924e3252d12eaf Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Thu, 6 Mar 2014 16:42:05 -0600 Subject: [PATCH 5/5] Rename arduinos and skynet connections examples. --- .../multiple_arduinos_multiple_skynet_connections.js} | 0 .../multiple_arduinos_one_skynet_connection.js} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename examples/{multi_platform/multi_arduinos_with_one_skynet_connection_each.js => arduinos_and_skynet/multiple_arduinos_multiple_skynet_connections.js} (100%) rename examples/{multi_platform/one_skynet_connection_controlling_multiple_arduinos.js => arduinos_and_skynet/multiple_arduinos_one_skynet_connection.js} (100%) diff --git a/examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js b/examples/arduinos_and_skynet/multiple_arduinos_multiple_skynet_connections.js similarity index 100% rename from examples/multi_platform/multi_arduinos_with_one_skynet_connection_each.js rename to examples/arduinos_and_skynet/multiple_arduinos_multiple_skynet_connections.js diff --git a/examples/multi_platform/one_skynet_connection_controlling_multiple_arduinos.js b/examples/arduinos_and_skynet/multiple_arduinos_one_skynet_connection.js similarity index 100% rename from examples/multi_platform/one_skynet_connection_controlling_multiple_arduinos.js rename to examples/arduinos_and_skynet/multiple_arduinos_one_skynet_connection.js