[136] | 1 | /*
|
---|
| 2 | ReadATweet
|
---|
| 3 |
|
---|
| 4 | Demonstrates retrieving the most recent Tweet from a user's home timeline
|
---|
| 5 | using Temboo from an Arduino Y炭n.
|
---|
| 6 |
|
---|
| 7 | Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
---|
| 8 |
|
---|
| 9 | A Temboo account and application key are necessary to run all Temboo examples.
|
---|
| 10 | If you don't already have one, you can register for a free Temboo account at
|
---|
| 11 | http://www.temboo.com
|
---|
| 12 |
|
---|
| 13 | In order to run this sketch, you'll need to register an application using
|
---|
| 14 | the Twitter dev console at https://dev.twitter.com. After creating the
|
---|
| 15 | app, you'll find OAuth credentials for that application under the "OAuth Tool" tab.
|
---|
| 16 | Substitute these values for the placeholders below.
|
---|
| 17 |
|
---|
| 18 | This example assumes basic familiarity with Arduino sketches, and that your Y炭n
|
---|
| 19 | is connected to the Internet.
|
---|
| 20 |
|
---|
| 21 | Want to use another social API with your Arduino Y炭n? We've got Facebook,
|
---|
| 22 | Google+, Instagram, Tumblr and more in our Library!
|
---|
| 23 |
|
---|
| 24 | This example code is in the public domain.
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 | #include <Bridge.h>
|
---|
| 28 | #include <Temboo.h>
|
---|
| 29 | #include "TembooAccount.h" // contains Temboo account information
|
---|
| 30 | // as described in the footer comment below
|
---|
| 31 |
|
---|
| 32 | /*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
---|
| 33 |
|
---|
| 34 | // Note that for additional security and reusability, you could
|
---|
| 35 | // use #define statements to specify these values in a .h file.
|
---|
| 36 | const String TWITTER_ACCESS_TOKEN = "your-twitter-access-token";
|
---|
| 37 | const String TWITTER_ACCESS_TOKEN_SECRET = "your-twitter-access-token-secret";
|
---|
| 38 | const String TWITTER_CONSUMER_KEY = "your-twitter-consumer-key";
|
---|
| 39 | const String TWITTER_CONSUMER_SECRET = "your-twitter-consumer-secret";
|
---|
| 40 |
|
---|
| 41 | int numRuns = 1; // execution count, so this doesn't run forever
|
---|
| 42 | int maxRuns = 10; // the max number of times the Twitter HomeTimeline Choreo should run
|
---|
| 43 |
|
---|
| 44 | void setup() {
|
---|
| 45 | Serial.begin(9600);
|
---|
| 46 |
|
---|
| 47 | // For debugging, wait until a serial console is connected.
|
---|
| 48 | delay(4000);
|
---|
| 49 | while(!Serial);
|
---|
| 50 | Bridge.begin();
|
---|
| 51 | }
|
---|
| 52 | void loop()
|
---|
| 53 | {
|
---|
| 54 | // while we haven't reached the max number of runs...
|
---|
| 55 | if (numRuns <= maxRuns) {
|
---|
| 56 | Serial.println("Running ReadATweet - Run #" + String(numRuns++));
|
---|
| 57 |
|
---|
| 58 | TembooChoreo HomeTimelineChoreo;
|
---|
| 59 |
|
---|
| 60 | // invoke the Temboo client.
|
---|
| 61 | // NOTE that the client must be reinvoked, and repopulated with
|
---|
| 62 | // appropriate arguments, each time its run() method is called.
|
---|
| 63 | HomeTimelineChoreo.begin();
|
---|
| 64 |
|
---|
| 65 | // set Temboo account credentials
|
---|
| 66 | HomeTimelineChoreo.setAccountName(TEMBOO_ACCOUNT);
|
---|
| 67 | HomeTimelineChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
---|
| 68 | HomeTimelineChoreo.setAppKey(TEMBOO_APP_KEY);
|
---|
| 69 |
|
---|
| 70 | // tell the Temboo client which Choreo to run (Twitter > Timelines > HomeTimeline)
|
---|
| 71 | HomeTimelineChoreo.setChoreo("/Library/Twitter/Timelines/HomeTimeline");
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | // set the required choreo inputs
|
---|
| 75 | // see https://www.temboo.com/library/Library/Twitter/Timelines/HomeTimeline/
|
---|
| 76 | // for complete details about the inputs for this Choreo
|
---|
| 77 |
|
---|
| 78 | HomeTimelineChoreo.addInput("Count", "1"); // the max number of Tweets to return from each request
|
---|
| 79 | HomeTimelineChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN);
|
---|
| 80 | HomeTimelineChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET);
|
---|
| 81 | HomeTimelineChoreo.addInput("ConsumerKey", TWITTER_CONSUMER_KEY);
|
---|
| 82 | HomeTimelineChoreo.addInput("ConsumerSecret", TWITTER_CONSUMER_SECRET);
|
---|
| 83 |
|
---|
| 84 | // next, we'll define two output filters that let us specify the
|
---|
| 85 | // elements of the response from Twitter that we want to receive.
|
---|
| 86 | // see the examples at http://www.temboo.com/arduino
|
---|
| 87 | // for more on using output filters
|
---|
| 88 |
|
---|
| 89 | // we want the text of the tweet
|
---|
| 90 | HomeTimelineChoreo.addOutputFilter("tweet", "/[1]/text", "Response");
|
---|
| 91 |
|
---|
| 92 | // and the name of the author
|
---|
| 93 | HomeTimelineChoreo.addOutputFilter("author", "/[1]/user/screen_name", "Response");
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | // tell the Process to run and wait for the results. The
|
---|
| 97 | // return code will tell us whether the Temboo client
|
---|
| 98 | // was able to send our request to the Temboo servers
|
---|
| 99 | unsigned int returnCode = HomeTimelineChoreo.run();
|
---|
| 100 |
|
---|
| 101 | // a response code of 0 means success; print the API response
|
---|
| 102 | if(returnCode == 0) {
|
---|
| 103 |
|
---|
| 104 | String author; // a String to hold the tweet author's name
|
---|
| 105 | String tweet; // a String to hold the text of the tweet
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | // choreo outputs are returned as key/value pairs, delimited with
|
---|
| 109 | // newlines and record/field terminator characters, for example:
|
---|
| 110 | // Name1\n\x1F
|
---|
| 111 | // Value1\n\x1E
|
---|
| 112 | // Name2\n\x1F
|
---|
| 113 | // Value2\n\x1E
|
---|
| 114 |
|
---|
| 115 | // see the examples at http://www.temboo.com/arduino for more details
|
---|
| 116 | // we can read this format into separate variables, as follows:
|
---|
| 117 |
|
---|
| 118 | while(HomeTimelineChoreo.available()) {
|
---|
| 119 | // read the name of the output item
|
---|
| 120 | String name = HomeTimelineChoreo.readStringUntil('\x1F');
|
---|
| 121 | name.trim();
|
---|
| 122 |
|
---|
| 123 | // read the value of the output item
|
---|
| 124 | String data = HomeTimelineChoreo.readStringUntil('\x1E');
|
---|
| 125 | data.trim();
|
---|
| 126 |
|
---|
| 127 | // assign the value to the appropriate String
|
---|
| 128 | if (name == "tweet") {
|
---|
| 129 | tweet = data;
|
---|
| 130 | } else if (name == "author") {
|
---|
| 131 | author = data;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | Serial.println("@" + author + " - " + tweet);
|
---|
| 136 |
|
---|
| 137 | } else {
|
---|
| 138 | // there was an error
|
---|
| 139 | // print the raw output from the choreo
|
---|
| 140 | while(HomeTimelineChoreo.available()) {
|
---|
| 141 | char c = HomeTimelineChoreo.read();
|
---|
| 142 | Serial.print(c);
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | HomeTimelineChoreo.close();
|
---|
| 147 |
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | Serial.println("Waiting...");
|
---|
| 151 | delay(90000); // wait 90 seconds between HomeTimeline calls
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /*
|
---|
| 155 | IMPORTANT NOTE: TembooAccount.h:
|
---|
| 156 |
|
---|
| 157 | TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
---|
| 158 | You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
---|
| 159 | by inserting your own Temboo account name and app key information. The contents of the file should
|
---|
| 160 | look like:
|
---|
| 161 |
|
---|
| 162 | #define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
---|
| 163 | #define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
---|
| 164 | #define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
---|
| 165 |
|
---|
| 166 | You can find your Temboo App Key information on the Temboo website,
|
---|
| 167 | under My Account > Application Keys
|
---|
| 168 |
|
---|
| 169 | The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
---|
| 170 |
|
---|
| 171 | Keeping your account information in a separate file means you can share the main .ino file without worrying
|
---|
| 172 | that you forgot to delete your credentials.
|
---|
| 173 | */
|
---|