1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-12 23:08:52 +01:00
Arduino/libraries/Bridge/examples/Temboo/ReadATweet/ReadATweet.ino

174 lines
6.4 KiB
Arduino
Raw Normal View History

2013-06-26 17:29:57 +02:00
/*
ReadATweet
2014-04-03 19:56:46 +02:00
Demonstrates retrieving the most recent Tweet from a user's home timeline
2013-07-11 11:24:53 +02:00
using Temboo from an Arduino Yun.
2013-06-26 17:29:57 +02:00
2013-06-28 11:08:56 +02:00
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
2014-04-03 19:56:46 +02:00
A Temboo account and application key are necessary to run all Temboo examples.
If you don't already have one, you can register for a free Temboo account at
2013-06-26 17:29:57 +02:00
http://www.temboo.com
In order to run this sketch, you'll need to register an application using
2014-04-03 19:56:46 +02:00
the Twitter dev console at https://dev.twitter.com. After creating the
app, you'll find OAuth credentials for that application under the "OAuth Tool" tab.
Substitute these values for the placeholders below.
2013-06-26 17:29:57 +02:00
2014-04-03 19:56:46 +02:00
This example assumes basic familiarity with Arduino sketches, and that your Yun
2013-06-28 11:08:56 +02:00
is connected to the Internet.
2014-04-03 19:56:46 +02:00
Want to use another social API with your Arduino Yun? We've got Facebook,
2013-07-11 11:24:53 +02:00
Google+, Instagram, Tumblr and more in our Library!
2014-04-03 19:56:46 +02:00
2013-06-26 17:29:57 +02:00
This example code is in the public domain.
*/
#include <Bridge.h>
2013-07-08 15:09:09 +02:00
#include <Temboo.h>
2013-06-28 11:08:56 +02:00
#include "TembooAccount.h" // contains Temboo account information
2014-04-03 19:56:46 +02:00
// as described in the footer comment below
2013-06-28 11:08:56 +02:00
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
2013-07-03 09:02:59 +02:00
// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.
2013-06-28 11:08:56 +02:00
const String TWITTER_ACCESS_TOKEN = "your-twitter-access-token";
const String TWITTER_ACCESS_TOKEN_SECRET = "your-twitter-access-token-secret";
const String TWITTER_CONSUMER_KEY = "your-twitter-consumer-key";
const String TWITTER_CONSUMER_SECRET = "your-twitter-consumer-secret";
2013-06-26 17:29:57 +02:00
2013-07-08 15:09:09 +02:00
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
2013-06-26 17:29:57 +02:00
void setup() {
Serial.begin(9600);
2014-04-03 19:56:46 +02:00
2013-07-08 15:09:09 +02:00
// For debugging, wait until a serial console is connected.
2013-06-26 17:29:57 +02:00
delay(4000);
2014-04-03 19:56:46 +02:00
while(!Serial);
2013-06-26 17:29:57 +02:00
Bridge.begin();
}
void loop()
{
// while we haven't reached the max number of runs...
2013-07-03 09:02:59 +02:00
if (numRuns <= maxRuns) {
2013-07-08 15:09:09 +02:00
Serial.println("Running ReadATweet - Run #" + String(numRuns++));
2014-04-03 19:56:46 +02:00
2013-07-08 15:09:09 +02:00
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();
2014-04-03 19:56:46 +02:00
2013-06-26 17:29:57 +02:00
// set Temboo account credentials
2013-07-08 15:09:09 +02:00
HomeTimelineChoreo.setAccountName(TEMBOO_ACCOUNT);
HomeTimelineChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
HomeTimelineChoreo.setAppKey(TEMBOO_APP_KEY);
2013-06-26 17:29:57 +02:00
2013-07-08 15:09:09 +02:00
// tell the Temboo client which Choreo to run (Twitter > Timelines > HomeTimeline)
HomeTimelineChoreo.setChoreo("/Library/Twitter/Timelines/HomeTimeline");
2014-04-03 19:56:46 +02:00
2013-06-26 17:29:57 +02:00
// set the required choreo inputs
// see https://www.temboo.com/library/Library/Twitter/Timelines/HomeTimeline/
// for complete details about the inputs for this Choreo
2013-07-08 15:09:09 +02:00
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);
2014-04-03 19:56:46 +02:00
HomeTimelineChoreo.addInput("ConsumerKey", TWITTER_CONSUMER_KEY);
2013-07-08 15:09:09 +02:00
HomeTimelineChoreo.addInput("ConsumerSecret", TWITTER_CONSUMER_SECRET);
2014-04-03 19:56:46 +02:00
// next, we'll define two output filters that let us specify the
2013-06-26 17:29:57 +02:00
// 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
2014-04-03 19:56:46 +02:00
2013-06-26 17:29:57 +02:00
// we want the text of the tweet
2013-07-08 15:09:09 +02:00
HomeTimelineChoreo.addOutputFilter("tweet", "/[1]/text", "Response");
2014-04-03 19:56:46 +02:00
2013-06-26 17:29:57 +02:00
// and the name of the author
2013-07-08 15:09:09 +02:00
HomeTimelineChoreo.addOutputFilter("author", "/[1]/user/screen_name", "Response");
2013-06-26 17:29:57 +02:00
2014-04-03 19:56:46 +02:00
// tell the Process to run and wait for the results. The
// return code will tell us whether the Temboo client
2013-06-26 17:29:57 +02:00
// was able to send our request to the Temboo servers
2013-07-03 09:02:59 +02:00
unsigned int returnCode = HomeTimelineChoreo.run();
2014-04-03 19:56:46 +02:00
// a response code of 0 means success; print the API response
if(returnCode == 0) {
2013-06-26 17:29:57 +02:00
String author; // a String to hold the tweet author's name
String tweet; // a String to hold the text of the tweet
2014-04-03 19:56:46 +02:00
// choreo outputs are returned as key/value pairs, delimited with
2013-06-26 17:29:57 +02:00
// newlines and record/field terminator characters, for example:
// Name1\n\x1F
// Value1\n\x1E
// Name2\n\x1F
2014-04-03 19:56:46 +02:00
// Value2\n\x1E
2013-06-26 17:29:57 +02:00
// see the examples at http://www.temboo.com/arduino for more details
// we can read this format into separate variables, as follows:
2014-04-03 19:56:46 +02:00
while(HomeTimelineChoreo.available()) {
2013-06-26 17:29:57 +02:00
// 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;
}
}
2014-04-03 19:56:46 +02:00
2013-06-26 17:29:57 +02:00
Serial.println("@" + author + " - " + tweet);
2014-04-03 19:56:46 +02:00
2013-06-26 17:29:57 +02:00
} else {
// there was an error
// print the raw output from the choreo
2014-04-03 19:56:46 +02:00
while(HomeTimelineChoreo.available()) {
2013-07-03 09:02:59 +02:00
char c = HomeTimelineChoreo.read();
Serial.print(c);
2013-06-26 17:29:57 +02:00
}
}
HomeTimelineChoreo.close();
2013-07-08 15:09:09 +02:00
2013-06-26 17:29:57 +02:00
}
2013-07-03 09:02:59 +02:00
Serial.println("Waiting...");
delay(90000); // wait 90 seconds between HomeTimeline calls
2013-06-26 17:29:57 +02:00
}
2013-07-09 20:07:54 +02:00
/*
IMPORTANT NOTE: TembooAccount.h:
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
by inserting your own Temboo account name and app key information. The contents of the file should
look like:
2014-04-03 19:56:46 +02:00
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
2013-07-09 20:07:54 +02:00
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
2014-04-03 19:56:46 +02:00
You can find your Temboo App Key information on the Temboo website,
2013-07-09 20:07:54 +02:00
under My Account > Application Keys
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
2014-04-03 19:56:46 +02:00
Keeping your account information in a separate file means you can share the main .ino file without worrying
2013-07-09 20:07:54 +02:00
that you forgot to delete your credentials.
*/