1 | /*
|
---|
2 | SendATweet
|
---|
3 |
|
---|
4 | Demonstrates sending a tweet via a Twitter account using Temboo from an Arduino Y炭n.
|
---|
5 |
|
---|
6 | Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
---|
7 |
|
---|
8 | A Temboo account and application key are necessary to run all Temboo examples.
|
---|
9 | If you don't already have one, you can register for a free Temboo account at
|
---|
10 | http://www.temboo.com
|
---|
11 |
|
---|
12 | In order to run this sketch, you'll need to register an application using
|
---|
13 | the Twitter dev console at https://dev.twitter.com. Note that since this
|
---|
14 | sketch creates a new tweet, your application will need to be configured with
|
---|
15 | read+write permissions. After creating the app, you'll find OAuth credentials
|
---|
16 | for that application under the "OAuth Tool" tab. Substitute these values for
|
---|
17 | the placeholders below.
|
---|
18 |
|
---|
19 | This example assumes basic familiarity with Arduino sketches, and that your Y炭n is connected
|
---|
20 | to the Internet.
|
---|
21 |
|
---|
22 | Want to use another social API with your Arduino Y炭n? We've got Facebook,
|
---|
23 | Google+, Instagram, Tumblr and more in our Library!
|
---|
24 |
|
---|
25 | This example code is in the public domain.
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include <Bridge.h>
|
---|
29 | #include <Temboo.h>
|
---|
30 | #include "TembooAccount.h" // contains Temboo account information
|
---|
31 | // as described in the footer comment below
|
---|
32 |
|
---|
33 |
|
---|
34 | /*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
---|
35 |
|
---|
36 | // Note that for additional security and reusability, you could
|
---|
37 | // use #define statements to specify these values in a .h file.
|
---|
38 | const String TWITTER_ACCESS_TOKEN = "your-twitter-access-token";
|
---|
39 | const String TWITTER_ACCESS_TOKEN_SECRET = "your-twitter-access-token-secret";
|
---|
40 | const String TWITTER_CONSUMER_KEY = "your-twitter-consumer-key";
|
---|
41 | const String TWITTER_CONSUMER_SECRET = "your-twitter-consumer-secret";
|
---|
42 |
|
---|
43 | int numRuns = 1; // execution count, so this sketch doesn't run forever
|
---|
44 | int maxRuns = 3; // the max number of times the Twitter Update Choreo should run
|
---|
45 |
|
---|
46 | void setup() {
|
---|
47 | Serial.begin(9600);
|
---|
48 |
|
---|
49 | // for debugging, wait until a serial console is connected
|
---|
50 | delay(4000);
|
---|
51 | while(!Serial);
|
---|
52 |
|
---|
53 | Bridge.begin();
|
---|
54 | }
|
---|
55 |
|
---|
56 | void loop()
|
---|
57 | {
|
---|
58 | // only try to send the tweet if we haven't already sent it successfully
|
---|
59 | if (numRuns <= maxRuns) {
|
---|
60 |
|
---|
61 | Serial.println("Running SendATweet - Run #" + String(numRuns++) + "...");
|
---|
62 |
|
---|
63 | // define the text of the tweet we want to send
|
---|
64 | String tweetText("My Arduino Yun has been running for " + String(millis()) + " milliseconds.");
|
---|
65 |
|
---|
66 |
|
---|
67 | TembooChoreo StatusesUpdateChoreo;
|
---|
68 |
|
---|
69 | // invoke the Temboo client
|
---|
70 | // NOTE that the client must be reinvoked, and repopulated with
|
---|
71 | // appropriate arguments, each time its run() method is called.
|
---|
72 | StatusesUpdateChoreo.begin();
|
---|
73 |
|
---|
74 | // set Temboo account credentials
|
---|
75 | StatusesUpdateChoreo.setAccountName(TEMBOO_ACCOUNT);
|
---|
76 | StatusesUpdateChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
---|
77 | StatusesUpdateChoreo.setAppKey(TEMBOO_APP_KEY);
|
---|
78 |
|
---|
79 | // identify the Temboo Library choreo to run (Twitter > Tweets > StatusesUpdate)
|
---|
80 | StatusesUpdateChoreo.setChoreo("/Library/Twitter/Tweets/StatusesUpdate");
|
---|
81 |
|
---|
82 | // set the required choreo inputs
|
---|
83 | // see https://www.temboo.com/library/Library/Twitter/Tweets/StatusesUpdate/
|
---|
84 | // for complete details about the inputs for this Choreo
|
---|
85 |
|
---|
86 | // add the Twitter account information
|
---|
87 | StatusesUpdateChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN);
|
---|
88 | StatusesUpdateChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET);
|
---|
89 | StatusesUpdateChoreo.addInput("ConsumerKey", TWITTER_CONSUMER_KEY);
|
---|
90 | StatusesUpdateChoreo.addInput("ConsumerSecret", TWITTER_CONSUMER_SECRET);
|
---|
91 |
|
---|
92 | // and the tweet we want to send
|
---|
93 | StatusesUpdateChoreo.addInput("StatusUpdate", tweetText);
|
---|
94 |
|
---|
95 | // tell the Process to run and wait for the results. The
|
---|
96 | // return code (returnCode) will tell us whether the Temboo client
|
---|
97 | // was able to send our request to the Temboo servers
|
---|
98 | unsigned int returnCode = StatusesUpdateChoreo.run();
|
---|
99 |
|
---|
100 | // a return code of zero (0) means everything worked
|
---|
101 | if (returnCode == 0) {
|
---|
102 | Serial.println("Success! Tweet sent!");
|
---|
103 | } else {
|
---|
104 | // a non-zero return code means there was an error
|
---|
105 | // read and print the error message
|
---|
106 | while (StatusesUpdateChoreo.available()) {
|
---|
107 | char c = StatusesUpdateChoreo.read();
|
---|
108 | Serial.print(c);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | StatusesUpdateChoreo.close();
|
---|
112 |
|
---|
113 | // do nothing for the next 90 seconds
|
---|
114 | Serial.println("Waiting...");
|
---|
115 | delay(90000);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /*
|
---|
120 | IMPORTANT NOTE: TembooAccount.h:
|
---|
121 |
|
---|
122 | TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
---|
123 | You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
---|
124 | by inserting your own Temboo account name and app key information. The contents of the file should
|
---|
125 | look like:
|
---|
126 |
|
---|
127 | #define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
---|
128 | #define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
---|
129 | #define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
---|
130 |
|
---|
131 | You can find your Temboo App Key information on the Temboo website,
|
---|
132 | under My Account > Application Keys
|
---|
133 |
|
---|
134 | The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
---|
135 |
|
---|
136 | Keeping your account information in a separate file means you can share the main .ino file without worrying
|
---|
137 | that you forgot to delete your credentials.
|
---|
138 | */
|
---|