25c73f0d c36e 4c9b a6bf b147adeeba02


"LittleBits, Blink LED When Somebody Tweets About [...]"

by girlie_mac

Published on March 25, 2016

Creating littleBits Twitter LED notification with Node.js using Johnny-Five and PubNub.

You can also read the tutorial at https://www.pubnub.com/blog/2015-04-09-triggering-littlebits-leds-in-realtime-with-node-js-using-joh...

Duration: 2 hours

How To Make It


Blink LEDs When Somebody Tweets About Something As an easy initial project, I connected Twitter to littleBits using Twitter data stream. When somebody tweets the queries that you enter from the web form, it notifies by blinking LED. Take a look at the GIF animation! In this article, I will walk through the process to create a simpler version of the demo, sans the web client. However, the entire source code with front-end code is on GitHub repo at https://github.com/pubnub/twitter-littlebits-blinky, if you would like to check out!
Ffc4e399 08cc 4a9f 81c0 f4d2f1b5da6a

in action!


Setting up the Arduino at Heart Board Let’s get started with littleBits with Arduino at Heart. This procedure is for the very first time use only. Skip this if you already have used littleBits before. You’ll need: - Arduino at Heart module - Power module - 9V battery - USB cable to connect to your computer Then, connect them as shown below:
D4956491 423c 428e b88a ddf6dfd26fbd

setting up


Loading StandardFirmata on Arduino Meanwhile, download Arduino IDE and install to your computer. You will need the IDE only for the initial setup. You won’t need it anymore unless you want to keep using. On Arduino IDE, go to Tools > Port and make sure the right board (“Arduino Leonard””) is connected to the right port (“tty.usbmodem…” for Mac, “cu.usbmodem…” for Windows). Johnny-Five communicates with Arduino using the Firmata protocol, so you need to install StandardFirmata: 1. On IDE, Open File > Examples > Firmata > StandardFirmata 2. Click the upload button (arrow button) 3. Wait until the IDE message window says “Done uploading” 4. Close the IDE and you don’t need the IDE anymore Now you are ready to snap littleBit pieces and code!
E4998a78 c617 4836 ba86 dec818f4f594

Tools > Port and make sure the right board (“Arduino Leonard””) is connected to the right port (“tty.usbmodem…” for Mac, “cu.usbmodem…” for Windows)

D593b8b3 d969 4358 8ba3 4bc4dd316a3b

File > Examples > Firmata > StandardFirmata


Using Johnny-Five to Communicate Arduino with JavaScript Johnny-Five (http://johnny-five.io) is an Open Source, Firmata Protocol based, IoT and Robotics programming framework for Node.js, developed by Rick Waldron of Bocoup. You already have your Arduino programmed with Firmata in the last step, so now you just need to install Johnny-Five in your project directory! (Of course, you have to install Node.js on your machine if you have not!)
$ npm install johnny-five


Blinking littleBits’ LED Module Now, it is the fun part… let’s blink an LED using node.js! Thanks to Anna Gerber, who gathered all information and sample code to start programming littleBits with Johnny-Five on her GitHub repo, we can just take her code to get started! First, assemble the circuit by connecting power to d0 on Arduino, and bargraph (or an other LED module) to d5. Then, try this node.js code below and run. (Make sure the littleBits’ power is on!) You will see the LED lights blink for 1000 milliseconds repeatedly. Now we will tweak this basic code that interacts with LED.
77c7bb8c c6d2 423c b86b 73737c816286


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 }); });


Getting Realtime Tweets from the PubNub Twitter Firehose OK, now let’s make it more fun. Instead of you manually blink LED, let’s hook it up with PubNub’s Twitter Firehose, so when somebody in the world tweet certain terms, it notifies me with blinking lights! I have written a tutorial on how to use Twitter data stream on Twitter’s Developer Blog (https://blog.twitter.com/2014/tweet-emotion-real-time-tweet-analysis-with-pubnub-data-stream) before, but let me recap:


Installing and Initializing PubNub Node.js Module First, install pubnub node.js module in your project directory. Include this code below in your node.js file, let’s call it index.js. We are using a public stream called pubnub-twitter, so initialize the API with the channel name and a public key.
$ npm install pubnub
var channel = 'pubnub-twitter'; var pubnub = require('pubnub').init({ subscribe_key: 'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe' });


Subscribing to the Live Data To retrieve the live Tweets, you simply use PubNub subscribe() API. In my demo, the queries are actually from a user input from a web client via POST, however, to make this tutorial easy to follow, I will skip the process of creating the web application using Express. So, instead of taking a user input, we are using static strings in an array for now. Use any strings you want. var queries = ['my_twitter_handle', 'myname', 'taco']; Let’s filter the firehose of tweets coming from PubNub data stream with the queries. At the callback in the code snippet above (where To-do comment is), include this code:
pubnub.subscribe({ channel: channel, callback: function(m) { // To-do // Check if the tweet contains the strings you are seeking for. if (queries.some(function(v) { return m.text.toLowerCase().indexOf(v) >= 0; })) { blink(led); } } });


Communicating with littleBits Modify the Johnny-Five code sample above, and create the blink function. Instead of strobe, I am using pulse here. It pulses in 400ms interval, and stops after 4000ms. This function is called every time somebody in the world tweets the terms you are seeking for! OK, let’s run the node app! Make sure your littleBits modules are connected properly, and the power is on. LED should pulse when somebody tweets the queries you have specified! The source code with front-end code is on GitHub repo, fork it or reference it to create your own! https://github.com/pubnub/twitter-littlebits-blinky

function blink() { led.pulse(400);

board.wait(4000, function(){ led.stop(); });

board.repl.inject({ led: led }); }

Related Projects

Toilet Paper Subscription Helper

Toilet paper subscription helper monitors your toilet paper usage and calculates the best date for your next delivery. It notifies...

light or dark o-meter

This invention measures how bright or dim a room is


The children of the Casa Blanca Library designed and built this LittleBits powered helicopter with StrawBees rotor blades.