Skip to content

Instantly share code, notes, and snippets.

@girliemac
Last active August 29, 2015 14:17
Show Gist options
  • Save girliemac/f9b05e8e03a3e5327945 to your computer and use it in GitHub Desktop.
Save girliemac/f9b05e8e03a3e5327945 to your computer and use it in GitHub Desktop.
Snapping LittleBits and Coding in Node.js
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
});
});
var channel = 'pubnub-twitter';
var pubnub = require('pubnub').init({
subscribe_key: 'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe'
});
pubnub.subscribe({
channel: channel,
callback: function(m) {
// To-do
// Check if the tweet contains the strings you are seeking for.
// If true, blink LED.
}
});
var queries = ['my_twitter_handle', 'myname', 'taco'];
if (queries.some(function(v) { return m.text.toLowerCase().indexOf(v) >= 0; })) {
blink(led);
}
function blink() {
led.pulse(400);
board.wait(4000, function(){
led.stop();
});
board.repl.inject({
led: led
});
}
/* 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