Your Arduino Yún is on Facebook


We'll show you how you can post a Facebook status update from your Arduino Yún. We can't guarantee how many 'likes' you'll get, but we can guarantee you'll like how easy it is to get your Yún on Facebook.

This sketch uses our Facebook > Publishing > SetStatus Choreo.

Get Set Up

1Make sure you have a Temboo account. If you don't already have one, you can register for free.

2You'll need to register an application using the Facebook dev console.

3With your Facebook application in place, visit the Facebook > OAuth bundle. Starting with the InitializeOAuth Choreo, use these Choreos to grant your Facebook application access to post to Facebook on your behalf. This short video demonstrates how to use our OAuth Choreos, and it's a good idea to take a break and watch it now before you dive into this step. You'll need the access token returned by this process when you create the Arduino sketch later in this example.

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.

Write the Sketch

Copy the sketch code below into a new tab in your Arduino IDE. This code calls the SetStatus Choreo, and you will need to replace the placeholder value in the code with your own Facebook access token value obtained in step 3 above.

/*
  UpdateFacebookStatus

  Demonstrates sending a Facebook status update 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 a #define statement to specify this value in a .h file.

// the Facebook Access Token, which can be obtained using the Temboo OAuth Choreos
const String FACEBOOK_ACCESS_TOKEN = "xxxxxxxxxx";

int numRuns = 1;   // execution count, so this sketch doesn't run forever
int maxRuns = 1;  // the max number of times the Facebook SetStatus 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() {
  // while we haven't reached the max number of runs...
  if (numRuns <= maxRuns) {

    // print status
    Serial.println("Running UpdateFacebookStatus - Run #" + String(numRuns++) + "...");
    
    // Define the status message we want to post on Facebook; since Facebook
    // doesn't allow duplicate status messages, we'll include a changing value.
    String statusMsg = "My Arduino Yun has been running for " + String(millis()) + " milliseconds!";

    // define the Process that will be used to call the "temboo" client                
    TembooChoreo SetStatusChoreo;

    // invoke the Temboo client
    // NOTE that the client must be reinvoked and repopulated with
    // appropriate arguments each time its run() method is called.
    SetStatusChoreo.begin();
    
    // set Temboo account credentials
    SetStatusChoreo.setAccountName(TEMBOO_ACCOUNT);
    SetStatusChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    SetStatusChoreo.setAppKey(TEMBOO_APP_KEY);

    // tell the Temboo client which Choreo to run (Facebook > Publishing > SetStatus)
    SetStatusChoreo.setChoreo("/Library/Facebook/Publishing/SetStatus");

    // set the required choreo inputs
    // see  https://www.temboo.com/library/Library/Facebook/Publishing/SetStatus/
    // for complete details about the inputs for this Choreo
    
    SetStatusChoreo.addInput("AccessToken", FACEBOOK_ACCESS_TOKEN);    
    SetStatusChoreo.addInput("Message", statusMsg);


    // 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 = SetStatusChoreo.run();
    
    // print the response code and API response.
    Serial.println("Response code: " + String(returnCode));

    // note that in this case, we're just printing the raw response from Facebook.
    // see the examples on using Temboo SDK output filters at http://www.temboo.com/arduino
    // for information on how to filter this data    
    while(SetStatusChoreo.available()) {
      char c = SetStatusChoreo.read();
      Serial.print(c);
    }

    SetStatusChoreo.close();
  }

  Serial.println("Waiting...");
  Serial.println("");

  delay(30000); // wait 30 seconds between SetStatus calls  
}

Create Your Header File

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 post to Facebook with your Yún. Now you've got another way to impress your friends.

Note: Facebook will prevent you from posting similar status messages multiple times, so be careful not to run this sketch repeatedly without changing the status message.

Convert the sketch to work with the Yún Shield

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  SetStatusChoreo;

Now your code is ready to run on your Yún Shield!

What's Next?

Now that you've figured out how to post Facebook, why not check out the other 2000+ Choreos in our Library and start thinking about all the possibilities for your next Yún project.

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back