Created
April 12, 2016 15:22
-
-
Save LosantGists/48356b198aa89f77391d58ed06380e95 to your computer and use it in GitHub Desktop.
creating-a-motion-sensitive-light-using-an-edison-and-a-wemo
This file contains 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 Cylon = require('cylon'); | |
var Wemo = require('wemo-client'); | |
var findOutlet = function(cb){ | |
var wemo = new Wemo(); | |
var stop = false; | |
wemo.discover(function(deviceInfo) { | |
if(stop || deviceInfo.friendlyName !== 'LosantOutlet_1') { return; } | |
stop = true; | |
console.log('Found outlet!'); | |
cb(wemo.client(deviceInfo)); | |
}); | |
setTimeout(function(){ | |
if(stop){ return; } | |
stop = true; | |
console.log('Failed to find outlet, trying again...'); | |
findOutlet(cb); | |
}, 5000); | |
}; | |
var cylonWork = function(cylon){ | |
findOutlet(function(outlet){ | |
outlet.on('binaryState', function(value){ | |
value = Number(value); | |
if(value === 0){ | |
cylon.led.turnOff(); | |
console.log('Outlet has been turned off.'); | |
} else { | |
cylon.led.turnOn(); | |
console.log('Outlet has been turned on.'); | |
} | |
}); | |
cylon.motionDetector.digitalRead(function(err, value){ | |
if(value === 0){ | |
console.log('No motion detected, turning off outlet...'); | |
outlet.setBinaryState(0); | |
} else { | |
console.log('Motion detected, turning on outlet...'); | |
outlet.setBinaryState(1); | |
} | |
}); | |
}); | |
}; | |
var robot = Cylon.robot({ | |
connections: { | |
edison: { adaptor: 'intel-iot' } | |
}, | |
devices: { | |
motionDetector: { driver: 'direct-pin', pin: 2 }, // motion detector plugged into D2. | |
led: { driver: 'led', pin: 3 }, // led plugged into D3 | |
}, | |
work: cylonWork | |
}); | |
robot.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment