Click here to Skip to main content
15,881,559 members
Articles / Internet of Things
Article

Updating the Orbotix Sphero in response to tweets using the Intel® Edison board

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Apr 2016CPOL3 min read 7.8K  
This guide contains steps to have your Orbotix Sphero check Twitter and change its colored lights in response to new tweets posted using the hashtag #intelmaker.

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Get access to the new Intel® IoT Developer Kit, a complete hardware and software solution that allows developers to create exciting new solutions with the Intel® Galileo and Intel® Edison boards. Visit the Intel® Developer Zone for IoT.

This guide contains steps to have your Orbotix Sphero* check Twitter and change its colored lights in response to new tweets posted using the hashtag #intelmaker.

Requirements

You must have:

This section contains steps to create a new project in the Intel XDK IoT Edition, with the appropriate code to update your Sphero.

  1. In the Intel XDK IoT Edition, click the Projects tab. Click Start a New Project.

    Image 1
  2. Under Internet of Things Embedded Application, click Templates.
  3. Click Blank Template, then click Continue.
  4. In the Project Name field, type a name for your project and click Create.

    Image 2
  5. Your new project opens, with the main.js file displayed. Delete the contents of main.js and paste the following code into the file:
    var twitter = require('mtwitter');
    var colors = require('colors');
    var moment = require('moment');
    var spheron = require('spheron');
    var sphero = spheron.sphero();
    var spheroPort = '/dev/rfcomm0';
    var COLORS = spheron.toolbelt.COLORS;
    var newMessage = false;
    var lastTweet = '21:35:5';
    var twit = new twitter({
        consumer_key : 'xxxxxxxxxxxxxxxxxxxxx',
        consumer_secret : 'xxxxxxxxxxxxxxxxxxxxx',
        access_token_key : 'xxxxxxxxxxxxxxxxxxxxx',
        access_token_secret : 'xxxxxxxxxxxxxxxxxxxxx'
    });
    console.log('Starting'.cyan);
    setInterval(function() {
        twit.get('search/tweets', {q: '#intelmaker'}, function(err, item) {
            console.log(item.statuses[0].created_at.substring(11, 18).cyan)
            console.log(lastTweet);
            console.log("From isNew(): ", newMessage);
            if(item.statuses[0].created_at.substring(11, 18) === lastTweet) {
                console.log("we here");
                newMessage = false;
            }
            else{
                newMessage = true
                updateSphero(true);
                lastTweet = item.statuses[0].created_at.substring(11, 18);
            }
        });
     }, 1000);
     function updateSphero(flag)
     {
        console.log('updating sphero');
        sphero.on('open', function() {
            sphero.setRGB(COLORS.RED, false);
            setTimeout(function(){sphero.setRGB(COLORS.BLUE, false);}, 2000);
        });
        sphero.open(spheroPort);
     }
  6. From the left, select package.json. In the dependencies section, be sure to add an entry for each of the modules required by the code:
    "dependencies": {
    
            "mtwitter":"latest",
            "colors":"latest",
            "moment":"latest",
            "spheron":"latest"
    }
    
    Image 3

This section contains steps to create a new Twitter app and retrieve the appropriate keys to allow your code to interact with Twitter.

  1. Navigate to the Twitter Apps site at https://apps.twitter.com/ and sign in using your Twitter account.
  2. Click Create New App. The Create an application page opens.

    Image 4
  3. On the Create an application page, do the following:
    1. Type your name in the Name field.
    2. Type a description of your app in the Description field.
    3. Type a placeholder URL in the Website field.
    Image 5
  4. Read the terms and conditions of use, then select the Yes, I agree check box. Click Create your Twitter application.

    Image 6
  5. Click the Keys and Access Tokens tab.

    Image 7
  6. In the Application Settings section, copy and paste the consumer key from the Consumer Key (API Key) field and the consumer secret from the Consumer Secret (API Secret) field into a separate text editing program, such as Notepad.

    Image 8
  7. In the Your Access Token section, click Create my access token.


     
  8. Copy and paste the access token key from the Access Token fieldand the access token secret from the Access Token Secret field into your text editing program.

    Image 9
  9. In the Intel XDK IoT Edition, navigate to the line that begins with consumer_key. Replace the placeholder text in the code with the consumer key, consumer secret, access token, and access token secret.

    Image 10

This section contains steps to connect your board to Sphero using Bluetooth*.

  1. Connect to your board in a serial communication window.
  2. To activate Bluetooth and scan for Bluetooth-enabled devices, enter the following commands:
    rfkill unblock bluetooth<br />
    	bluetoothctl

     
  3. The MAC address of your Sphero should be displayed at the bottom of the Sphero's box, in the format XX-XX-XX-XX-XX-XX. If you cannot locate the Sphero's MAC address, do the following:
    1. Enter the command:
      scan on
    2. Locate your Sphero in the list and copy the MAC address.
    3. Enter the command:
      scan off
  4. Pair your board with the Sphero by entering the following commands:
    pair SpheroMacAddress<br />
    	exit<br />
    	rfcomm bind 0 SpheroMacAddress 1

    The MAC address should be in the format XX:XX:XX:XX:XX:XX. This will create a Sphero connection.

This section contains steps to build, upload, and run your code. After completing the steps, the color of your Sphero will change in response to tweets.

  1. In the bottom toolbar, click the Upload icon Image 11 to upload the project to your board.
  2. Click the Run icon Image 12to run your project. Your Sphero checks Twitter once a second and turns blue when someone tweets using the hashtag #intelmaker.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
You may know us for our processors. But we do so much more. Intel invents at the boundaries of technology to make amazing experiences possible for business and society, and for every person on Earth.

Harnessing the capability of the cloud, the ubiquity of the Internet of Things, the latest advances in memory and programmable solutions, and the promise of always-on 5G connectivity, Intel is disrupting industries and solving global challenges. Leading on policy, diversity, inclusion, education and sustainability, we create value for our stockholders, customers and society.
This is a Organisation

42 members

Comments and Discussions

 
-- There are no messages in this forum --