Last active
August 29, 2015 14:17
-
-
Save girliemac/f9b05e8e03a3e5327945 to your computer and use it in GitHub Desktop.
Snapping LittleBits and Coding in Node.js
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 five = require('johnny-five'); | |
var board = new five.Board(); | |
board.on('ready', function() { | |
led = new five.Led(5); // create a Led on pin 5 | |
led.strobe(1000); // strobe for 1000ms | |
this.repl.inject({ | |
led: led | |
}); | |
}); |
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 channel = 'pubnub-twitter'; | |
var pubnub = require('pubnub').init({ | |
subscribe_key: 'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe' | |
}); |
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
pubnub.subscribe({ | |
channel: channel, | |
callback: function(m) { | |
// To-do | |
// Check if the tweet contains the strings you are seeking for. | |
// If true, blink LED. | |
} | |
}); |
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 queries = ['my_twitter_handle', 'myname', 'taco']; |
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
if (queries.some(function(v) { return m.text.toLowerCase().indexOf(v) >= 0; })) { | |
blink(led); | |
} |
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
function blink() { | |
led.pulse(400); | |
board.wait(4000, function(){ | |
led.stop(); | |
}); | |
board.repl.inject({ | |
led: led | |
}); | |
} |
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
/* Lite version for tutorial sans web client | |
* Fot the entire source code incl. front-end, see at https://github.com/pubnub/twitter-littlebits-blinky | |
*/ | |
(function() { | |
// Blinking LittleBit LED with Johnny-five | |
var five = require('johnny-five'); | |
var board = new five.Board(); | |
var led; | |
function pulse() { | |
led.pulse(400); | |
board.wait(4000, function(){ | |
led.stop(); | |
}); | |
board.repl.inject({ | |
led: led | |
}); | |
} | |
board.on('ready', function() { | |
console.log('Board is ready!'); | |
led = new five.Led(5); | |
pulse(); | |
}); | |
// Getting data from PubNub Twitter stream | |
var channel = 'pubnub-twitter'; | |
var pubnub = require('pubnub').init({ | |
subscribe_key: 'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe' | |
}); | |
var queries = ['lunch', 'sandwich', 'sushi', 'burrito']; // using static quesries for lite version | |
pubnub.subscribe({ | |
channel: channel, | |
callback: function(m) { | |
if(!m || !m.text) return; | |
if (queries.some(function(v) { return m.text.toLowerCase().indexOf(v) >= 0; })) { | |
if(!led) { | |
console.log('LittleBits board is not ready yet.'); | |
return; | |
} | |
console.log(m.user.screen_name + ': ' + m.text); | |
pulse(); | |
} | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment