Your Arduino Yún can read tweets


We'll show you how to get your Arduino Yún to retrieve the most recent tweet from your Twitter home timeline. You can use this as a first step to getting your Yún to react and respond to what's happening on Twitter.

This sketch uses our Twitter > Timelines > HomeTimelines Choreo.

Get Set Up

1Make sure you have a Temboo account. If you don't already have one, you can register for free.

2Make sure that you have the latest version of the Arduino IDE. You should also be sure that you have the newest version of the Temboo Library by checking the Arduino Library Manager

3Make sure that your Yún is connected to the Internet. Arduino has a helpful guide if you need assistance.

4You'll also need to register an application using the Twitter dev console. After creating the app, you'll find API keys for that application in the API Keys tab. You'll need to substitute these into the sketch code below.

Write the Sketch

Copy the sketch code below into a new tab in your Arduino IDE. This code calls the HomeTimeline Choreo for Twitter, and you will need to replace the placeholder values in the code with your own Twitter app details.

/*
  ReadATweet

  Demonstrates retrieving the most recent Tweet from a user's home timeline 
  using the Temboo Arduino Yun SDK.
  
  This example code is in the public domain.
*/

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information

/*** SUBSTITUTE YOUR VALUES BELOW: ***/

// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.
const String TWITTER_ACCESS_TOKEN = "your-twitter-access-token";
const String TWITTER_ACCESS_TOKEN_SECRET = "your-twitter-access-token-secret";
const String TWITTER_API_KEY = "your-twitter-api-key";
const String TWITTER_API_SECRET = "your-twitter-api-secret";

int numRuns = 1;   // execution count, so this doesn't run forever
int maxRuns = 10;   // the max number of times the Twitter HomeTimeline Choreo should run

void setup() {
  Serial.begin(9600);
  
  // For debugging, wait until a serial console is connected.
  delay(4000);
  while(!Serial);
  Bridge.begin();
}
void loop()
{
  // while we haven't reached the max number of runs...
  if (numRuns <= maxRuns) {
    Serial.println("Running ReadATweet - Run #" + String(numRuns++));
    
    TembooChoreo HomeTimelineChoreo;

    // invoke the Temboo client.
    // NOTE that the client must be reinvoked, and repopulated with
    // appropriate arguments, each time its run() method is called.
    HomeTimelineChoreo.begin();
    
    // set Temboo account credentials
    HomeTimelineChoreo.setAccountName(TEMBOO_ACCOUNT);
    HomeTimelineChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    HomeTimelineChoreo.setAppKey(TEMBOO_APP_KEY);

    // tell the Temboo client which Choreo to run (Twitter > Timelines > HomeTimeline)
    HomeTimelineChoreo.setChoreo("/Library/Twitter/Timelines/HomeTimeline");
    
    
    // set the required choreo inputs
    // see https://www.temboo.com/library/Library/Twitter/Timelines/HomeTimeline/
    // for complete details about the inputs for this Choreo

    HomeTimelineChoreo.addInput("Count", "1"); // the max number of Tweets to return from each request
    HomeTimelineChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN);
    HomeTimelineChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET);
    HomeTimelineChoreo.addInput("ConsumerKey", TWITTER_API_KEY);    
    HomeTimelineChoreo.addInput("ConsumerSecret", TWITTER_API_SECRET);

    // next, we'll define two output filters that let us specify the 
    // elements of the response from Twitter that we want to receive.
    // see the examples at http://www.temboo.com/arduino
    // for more on using output filters
   
    // we want the text of the tweet
    HomeTimelineChoreo.addOutputFilter("tweet", "/[1]/text", "Response");
    
    // and the name of the author
    HomeTimelineChoreo.addOutputFilter("author", "/[1]/user/screen_name", "Response");


    // tell the Process to run and wait for the results. The 
    // return code will tell us whether the Temboo client 
    // was able to send our request to the Temboo servers
    unsigned int returnCode = HomeTimelineChoreo.run();
    
   // a response code of 0 means success; print the API response
    if(returnCode == 0) {
      
      String author; // a String to hold the tweet author's name
      String tweet; // a String to hold the text of the tweet


      // choreo outputs are returned as key/value pairs, delimited with 
      // newlines and record/field terminator characters, for example:
      // Name1\n\x1F
      // Value1\n\x1E
      // Name2\n\x1F
      // Value2\n\x1E      
      
      // see the examples at http://www.temboo.com/arduino for more details
      // we can read this format into separate variables, as follows:
      
      while(HomeTimelineChoreo.available()) {
        // read the name of the output item
        String name = HomeTimelineChoreo.readStringUntil('\x1F');
        name.trim();

        // read the value of the output item
        String data = HomeTimelineChoreo.readStringUntil('\x1E');
        data.trim();

        // assign the value to the appropriate String
        if (name == "tweet") {
          tweet = data;
        } else if (name == "author") {
          author = data;
        }
      }
     
      Serial.println("@" + author + " - " + tweet);
    
    } else {
      // there was an error
      // print the raw output from the choreo
      while(HomeTimelineChoreo.available()) {
        char c = HomeTimelineChoreo.read();
        Serial.print(c);
      }
    }

    HomeTimelineChoreo.close();

  }

  Serial.println("Waiting...");
  delay(90000); // wait 90 seconds between HomeTimeline calls
}

Create Your Header File

The sketch above references the TembooAccount.h header file, which contains your Temboo account information.

If you are currently logged in, you'll see your account details in the code snippet below (otherwise you'll see placeholder values). Copy the code snippet into a new tab in Arduino and call it TembooAccount.h.

#define TEMBOO_ACCOUNT "ACCOUNT_NAME"  // your Temboo account name 
#define TEMBOO_APP_KEY_NAME "APP_NAME"  // your Temboo app key name
#define TEMBOO_APP_KEY  "APP_KEY"  // your Temboo app key

With both files in place you are ready to upload the sketch and read a tweet with your Yún. #Enjoy!

Convert the sketch to work with the Yún Shield

If you're working with the Yún Shield paired with another Arduino board, you'll need to make some small changes to the Yún sketch above it so that it's compatible with the Arduino Yún Shield.

1First, change the include statement #include <Temboo.h> to #include <TembooYunShield.h>. Your include statements should look like this:

#include <Bridge.h>
#include <TembooYunShield.h>
#include "TembooAccount.h" 

2Next, change the Temboo object name from TembooChoreo to TembooYunShieldChoreo. Your code should look like this:

TembooYunShieldChoreo  HomeTimelineChoreo;

3Now your code is ready to run on your Yún Shield!

What's Next?

Now that you've figured out how to read Twitter, why not check out the other 2000+ Choreos in our Library and start thinking about all the possibilities for your next Yún project.

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back