Search our Blogs
Showing results for 
Search instead for 
Do you mean 
 

Sphero as Your Twitter Watchdog Powered by IDOL OnDemand

Please note that HP IDOL OnDemand is now HPE Haven OnDemand. The API endpoints have changed to Haven OnDemand. Please see the API documentation for more details.

 

---

 

The Sphero ball is an interesting toy - it can be controlled by various applications on mobile devices, but it also offers an API to enable integration for code projects.

 

Dogs may guard your home - why not have a pet machine watch your social media?

 

In this blog post you'll learn how to program your Sphero to react to your Twitter feed!

 

Setup

 

What you'll need:

  • A Sphero - connect it to your laptop using bluetooth
  • A laptop/server

The Stack:

  • Node.js
  • Cylonjs - a nifty Javascript framework to interact with robots and devices. It doesn't offer all of the Sphero's functionality but it suits our needs
  • iod-node - an IOD client for node
  • Twitter - a node Twitter client
npm install git+https://github.com/HP-IDOL-OnDemand/iod-node
npm install cylon
npm install twitter

The Code

 

Code is available in full as a Gist.

 

var Cylon = require('cylon');
var Twitter = require('twitter');
var iod = require('iod-node')

var iodclient= new iod.IODClient('http://api.idolondemand.com','<iodapikey>')

var twitterclient = new Twitter({
  consumer_key: '<twitterconsumerkey',
  consumer_secret: '<twitterconsumerkey>',
  access_token_key: '<twitteraccesstokenkey>',
  access_token_secret: '<twitteraccesstokensecret>'
});

 

First, you need to configure your clients with the apikeys from Twitter and IDOL OnDemand.

Once you've done that, it's time to get started with the Sphero part.

 

spheroconn=Cylon.robot({
  connections: {
    sphero: { adaptor: 'sphero', port: '/dev/cu.Sphero-YBW-AMP-SPP' }
  },

  devices: {
    sphero: { driver: 'sphero' }
  },

  work: watchdog

});

spheroconn.start()

Cylon makes it easy to create a Sphero adapter, you'll want to find the correct port for your Sphero. It will appear in /dev/ once you've connected to it once using bluetooth.

 

The work function will get called once the connection happens, here you'll want to call your watchdog function.

 

Analyzing Tweets

 

function watchdog(my){
      twitterclient.stream('statuses/filter', {track: 'sphero'}, function(stream) {
        stream.on('data', function(tweet) {
        //handle each tweet
        });

        stream.on('error', function(error) {
          throw error;
        });
      });
}

 

The next step is to code the work function, called "watchdog", as above. We can use the Twitter streaming API to specify what happens for each tweet about a certain topic.

 

        stream.on('data', function(tweet) {
          var data= {'text':tweet.text};
          iodclient.call('analyzesentiment',function(err,resp,body){
            var sentiment = body["aggregate"]["sentiment"];
            
            my.sphero.setHeading(0)
            my.sphero.roll(0,180) // Everytime the sphero will spin half a rotation
            if (sentiment=="positive"){  my.sphero.setRGB(0x01DF74) } //Green
            else if (sentiment == "negative"){  my.sphero.setRGB(0xFF3300) } //Red
            else{ my.sphero.setRGB(0x0066FF) } //Blue
            
            after((1).seconds(), function() { // Return to white
              my.sphero.setRGB(0xFFFFFF) 
            }); 
            
          },data)
        });

 

When we receive a tweet we can use the IDOL OnDemand client we created before to send the tweet to the Sentiment Analysis api.

 

          iodclient.call('analyzesentiment',function(err,resp,body){
            var sentiment = body["aggregate"]["sentiment"];
                ...
           })

 

We can then make the Sphero spin and change color according to sentiment.

 

            my.sphero.setHeading(0)
            my.sphero.roll(0,180) // Everytime the sphero will spin half a rotation
            if (sentiment=="positive"){  my.sphero.setRGB(0x01DF74) } //Green
              ...

 

That's pretty much it - you now have a Sphero that will spin red or green whenever a new tweet shows up, based on the sentiment of that tweet!

 

We'd love to hear about your experience - leave us a comment in the box below!

 

Get the Source Code:

 

Gist

Social Media
About the Author
Topics
† The opinions expressed above are the personal opinions of the authors, not of HPE. By using this site, you accept the Terms of Use and Rules of Participation