We'll show you how to update Twitter from your Arduino Yún. With the right sensors you could set up the Arduino to post a message under certain conditions, such as radiation or loud noise being detected.
This sketch uses our Twitter > Tweets > StatusesUpdate Choreo.
1Make sure you have a Temboo account. If you don't already have one, you can register for free.
2You'll also need to register an application using the Twitter dev console. After creating the app, you'll find API keys for that application under the API Keys tab. You'll need to substitute these into the sketch below.
3Since this sketch creates a new tweet, your application will need to be configured with read+write permissions. You can configure this setting under the in the API Keys section of your Twitter dev console that we mentioned above.
4Make 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
5Make sure that your Yún is connected to the Internet. Arduino has a helpful guide if you need assistance.
Copy the code below into a new tab in your Arduino IDE. This code calls the Twitter StatusesUpdate Choreo, and you will need to replace the placeholder values in the code with your own Twitter app details.
/* SendATweet Demonstrates sending a tweet via a Twitter account 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 sketch doesn't run forever int maxRuns = 1; // the max number of times the Twitter Update 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() { // only try to send the tweet if we haven't already sent it successfully if (numRuns <= maxRuns) { Serial.println("Running SendATweet - Run #" + String(numRuns++) + "..."); // define the text of the tweet we want to send String tweetText("My Arduino Yun has been running for " + String(millis()) + " milliseconds."); TembooChoreo StatusesUpdateChoreo; // invoke the Temboo client // NOTE that the client must be reinvoked, and repopulated with // appropriate arguments, each time its run() method is called. StatusesUpdateChoreo.begin(); // set Temboo account credentials StatusesUpdateChoreo.setAccountName(TEMBOO_ACCOUNT); StatusesUpdateChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); StatusesUpdateChoreo.setAppKey(TEMBOO_APP_KEY); // identify the Temboo Library choreo to run (Twitter > Tweets > StatusesUpdate) StatusesUpdateChoreo.setChoreo("/Library/Twitter/Tweets/StatusesUpdate"); // set the required choreo inputs // see https://www.temboo.com/library/Library/Twitter/Tweets/StatusesUpdate/ // for complete details about the inputs for this Choreo // add the Twitter account information StatusesUpdateChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN); StatusesUpdateChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET); StatusesUpdateChoreo.addInput("ConsumerKey", TWITTER_API_KEY); StatusesUpdateChoreo.addInput("ConsumerSecret", TWITTER_API_SECRET); // and the tweet we want to send StatusesUpdateChoreo.addInput("StatusUpdate", tweetText); // tell the Process to run and wait for the results. The // return code (returnCode) will tell us whether the Temboo client // was able to send our request to the Temboo servers unsigned int returnCode = StatusesUpdateChoreo.run(); // a return code of zero (0) means everything worked if (returnCode == 0) { Serial.println("Success! Tweet sent!"); } else { // a non-zero return code means there was an error // read and print the error message while (StatusesUpdateChoreo.available()) { char c = StatusesUpdateChoreo.read(); Serial.print(c); } } StatusesUpdateChoreo.close(); // do nothing for the next 90 seconds Serial.println("Waiting..."); delay(90000); } }
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 send a tweet from your Yún. 140 characters, here we come!
Note: Twitter will prevent you from posting the same status message multiple times. We've set each status message here to post how long your Yún has been running for (in milliseconds) so that they're all unique.
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 StatusesUpdateChoreo;
3Now your code is ready to run on your Yún Shield!
Tweeting from the Yún is awesome, but you probably don't want all your followers to see the tweets you send while building and testing your app. One good approach for dealing with this is to make sure that only you can see the tweets, by sending Direct Messages to yourself. This way, your Yún can send tweets in private until your project is ready for prime time. The sketch below shows how you can send direct messages from your Yún to your own Twitter account.
/* SendDirectMessage Demonstrates sending a Twitter Direct Message 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"; // your twitter screen name will be something like @temboo or @cormacdriver const String TWITTER_NAME ="your-twitter-account-name"; int numRuns = 1; // execution count, so this sketch doesn't run forever int maxRuns = 1; // the max number of times the Twitter Direct Message 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() { // only try to send the direct message if we haven't already sent it successfully if (numRuns <= maxRuns) { Serial.println("Running SendDirectMessage - Run #" + String(numRuns++) + "..."); // define the text of the direct message we want to send String tweetText("My Arduino Yun has been running for " + String(millis()) + " milliseconds."); TembooChoreo SendDirectMessageChoreo; // invoke the Temboo client // NOTE that the client must be reinvoked, and repopulated with // appropriate arguments, each time its run() method is called. SendDirectMessageChoreo.begin(); // set Temboo account credentials SendDirectMessageChoreo.setAccountName(TEMBOO_ACCOUNT); SendDirectMessageChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); SendDirectMessageChoreo.setAppKey(TEMBOO_APP_KEY); // identify the Temboo Library choreo to run (Twitter > DirectMessages > SendDirectMessage) SendDirectMessageChoreo.setChoreo("/Library/Twitter/DirectMessages/SendDirectMessage"); // set the required choreo inputs // see https://www.temboo.com/library/Library/Twitter/DirectMessages/SendDirectMessage // for complete details about the inputs for this Choreo // add the Twitter account information SendDirectMessageChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN); SendDirectMessageChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET); SendDirectMessageChoreo.addInput("ConsumerKey", TWITTER_API_KEY); SendDirectMessageChoreo.addInput("ConsumerSecret", TWITTER_API_SECRET); // and the direct messae we want to send SendDirectMessageChoreo.addInput("Text", tweetText); // and the user who we want to send the DM to SendDirectMessageChoreo.addInput("ScreenName", TWITTER_NAME); // tell the process to run and wait for the results. The // return code (returnCode) will tell us whether the Temboo client // was able to send our request to Temboo unsigned int returnCode = SendDirectMessageChoreo.run(); // a return code of zero (0) means everything worked if (returnCode == 0) { Serial.println("Success! Direct message sent!"); } else { // a non-zero return code means there was an error // read and print the error message while (SendDirectMessageChoreo.available()) { char c = SendDirectMessageChoreo.read(); Serial.print(c); } } SendDirectMessageChoreo.close(); // do nothing for the next 90 seconds Serial.println("Done."); delay(90000); } }
Now that you've mastered 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.
We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.