1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-11 22:24:13 +01:00
Arduino/libraries/Bridge/examples/Temboo/SendATweet/SendATweet.ino

139 lines
5.2 KiB
Arduino
Raw Normal View History

2013-06-28 11:08:56 +02:00
/*
SendATweet
2014-05-30 12:17:09 +02:00
Demonstrates sending a tweet via a Twitter account using Temboo from an Arduino Yún.
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-28 11:08:56 +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. Note that since this
2013-07-03 09:02:59 +02:00
sketch creates a new tweet, your application will need to be configured with
2014-04-03 19:56:46 +02:00
read+write permissions. 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-28 11:08:56 +02:00
2014-05-30 12:17:09 +02:00
This example assumes basic familiarity with Arduino sketches, and that your Yún is connected
2013-06-28 11:08:56 +02:00
to the Internet.
2014-05-30 12:17:09 +02:00
Want to use another social API with your Arduino Yún? 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-28 11:08:56 +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";
int numRuns = 1; // execution count, so this sketch doesn't run forever
2013-07-08 15:09:09 +02:00
int maxRuns = 3; // the max number of times the Twitter Update Choreo should run
2013-06-28 11:08:56 +02:00
void setup() {
Serial.begin(9600);
// for debugging, wait until a serial console is connected
delay(4000);
2014-04-03 19:56:46 +02:00
while(!Serial);
2013-06-28 11:08:56 +02:00
Bridge.begin();
}
void loop()
{
// only try to send the tweet if we haven't already sent it successfully
if (numRuns <= maxRuns) {
Serial.println("Running SendATweet - Run #" + String(numRuns++) + "...");
2014-04-03 19:56:46 +02:00
2013-07-08 15:09:09 +02:00
// define the text of the tweet we want to send
String tweetText("My Arduino Yun has been running for " + String(millis()) + " milliseconds.");
2014-04-03 19:56:46 +02:00
2013-07-08 15:09:09 +02:00
TembooChoreo StatusesUpdateChoreo;
2013-06-28 11:08:56 +02:00
// invoke the Temboo client
2013-07-08 15:09:09 +02:00
// NOTE that the client must be reinvoked, and repopulated with
// appropriate arguments, each time its run() method is called.
StatusesUpdateChoreo.begin();
2014-04-03 19:56:46 +02:00
2013-06-28 11:08:56 +02:00
// set Temboo account credentials
2013-07-08 15:09:09 +02:00
StatusesUpdateChoreo.setAccountName(TEMBOO_ACCOUNT);
StatusesUpdateChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
StatusesUpdateChoreo.setAppKey(TEMBOO_APP_KEY);
2013-06-28 11:08:56 +02:00
2013-07-03 09:02:59 +02:00
// identify the Temboo Library choreo to run (Twitter > Tweets > StatusesUpdate)
2013-07-08 15:09:09 +02:00
StatusesUpdateChoreo.setChoreo("/Library/Twitter/Tweets/StatusesUpdate");
2013-06-28 11:08:56 +02:00
// set the required choreo inputs
2014-04-03 19:56:46 +02:00
// see https://www.temboo.com/library/Library/Twitter/Tweets/StatusesUpdate/
2013-06-28 11:08:56 +02:00
// for complete details about the inputs for this Choreo
2014-04-03 19:56:46 +02:00
2013-06-28 11:08:56 +02:00
// add the Twitter account information
2013-07-08 15:09:09 +02:00
StatusesUpdateChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN);
StatusesUpdateChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET);
2014-04-03 19:56:46 +02:00
StatusesUpdateChoreo.addInput("ConsumerKey", TWITTER_CONSUMER_KEY);
2013-07-08 15:09:09 +02:00
StatusesUpdateChoreo.addInput("ConsumerSecret", TWITTER_CONSUMER_SECRET);
// and the tweet we want to send
StatusesUpdateChoreo.addInput("StatusUpdate", tweetText);
2013-06-28 11:08:56 +02:00
2014-04-03 19:56:46 +02:00
// tell the Process to run and wait for the results. The
// return code (returnCode) will tell us whether the Temboo client
2013-06-28 11:08:56 +02:00
// was able to send our request to the Temboo servers
2013-07-03 09:02:59 +02:00
unsigned int returnCode = StatusesUpdateChoreo.run();
2013-06-28 11:08:56 +02:00
// a return code of zero (0) means everything worked
2013-07-03 09:02:59 +02:00
if (returnCode == 0) {
2014-04-03 19:56:46 +02:00
Serial.println("Success! Tweet sent!");
2013-06-28 11:08:56 +02:00
} else {
// a non-zero return code means there was an error
// read and print the error message
while (StatusesUpdateChoreo.available()) {
2013-07-03 09:02:59 +02:00
char c = StatusesUpdateChoreo.read();
Serial.print(c);
2013-06-28 11:08:56 +02:00
}
2014-04-03 19:56:46 +02:00
}
2013-06-28 11:08:56 +02:00
StatusesUpdateChoreo.close();
// do nothing for the next 90 seconds
2013-07-03 09:02:59 +02:00
Serial.println("Waiting...");
2013-06-28 11:08:56 +02:00
delay(90000);
}
}
/*
IMPORTANT NOTE: TembooAccount.h:
2013-07-03 09:02:59 +02:00
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:
2013-06-28 11:08:56 +02:00
2014-04-03 19:56:46 +02:00
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
2013-06-28 11:08:56 +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-06-28 11:08:56 +02:00
under My Account > Application Keys
2013-07-03 09:02:59 +02:00
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.
2013-06-28 11:08:56 +02:00
*/