The AdafruitTwitter class requires WICED Feather Lib 0.5.5 or higher to run.

The AdafruitTwitter class makes sending tweets easy via a custom Application that you can setup using this learning guide.

1. Creating a WICED Twitter Application

In order to enable WICED to interact with Twitter, you first need to log in to twitter's app admin console at http://apps.twitter.com and create a new app:

Enter the Application Details

Next you need to enter your application details, based on the following data:

The NAME field must be globally unique, so you should make it something personal like adding your twitter username.

Then accept the license terms and click the Create your Twitter application button at the bottom of the page.

This will redirect you to the main app config page, as shown below:

Set the Application Permissions

Click on the Permissions tab and set the appropriate permissions:

Click the Update Settings button to save the permissions changes.

Manage the Access Keys

Go back to the Details tab and scroll down to the Application Settings section:

Click the manage keys and access tokens link.

Copy the Appropriate Key Data

Make a note of the consumer key values blurred out below since you will need them in your sketch:

Create your Access Token

On the same page shown above, click the Create my access token button to give your account access to your new application:

Make a note of the access token data shown blurred out below, which you will also need in your sketch:

2. Using the AdafruitTwitter Class

Next, open the Applications/SendTweet example for WICED or create a new sketch with the following code, updating it with your access point details, as well as the Consumer and Access Tokens generated above:

/*********************************************************************
 This is an example for our Feather WIFI modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

#include "adafruit_feather.h"
#include "adafruit_http.h"
#include "adafruit_twitter.h"

/* This example demonstrates how to use AdafrtuiTwitter class
 * to send out a tweet
 *
 * To run this demo:
 * 1. Goto https://apps.twitter.com/ and login
 * 2. Create an application to use with this WICED Feather
 * 3. (Optional) You could change the access level to give the applicaion
 * permission to send DM. It is advised to do so, do that you could use WICED
 * to send DM in other example
 * 4. In the app management click "manage keys and access tokens"
 * and then click "Create my access token"
 * 5. Change CONSUMER_KEY, CONSUMER_SECRET, TOKEN_ACCESS, TOKEN_SECRET accordingly
 * 6. Change your TWEET status
 * 7. Compile and run, if you run this sketch too often, Twitter server may reject
 * your connection request, just wait a few minutes and try again.
 */

// Network
#define WLAN_SSID               "yourSSID"
#define WLAN_PASS               "yourPassword"

// Twitter Account
#define CONSUMER_KEY            "YOUR_CONSUMER_KEY"
#define CONSUMER_SECRET         "YOUR_CONSUMER_SECRET"

#define TOKEN_ACCESS            "YOUR_TOKEN_ACCESS"
#define TOKEN_SECRET            "YOUR_TOKEN_SECRET"

#define TWEET                   "Hello from Adafruit WICED Feather"

AdafruitTwitter Twitter;

/**************************************************************************/
/*!
    @brief  The setup function runs once when reset the board
*/
/**************************************************************************/
void setup()
{
  Serial.begin(115200);

  // wait for serial port to connect. Needed for native USB port only
  while (!Serial) delay(1);

  Serial.println("Twitter Send Tweet Example\r\n");

  // Print all software versions
  Feather.printVersions();

  while ( !connectAP() )
  {
    delay(500); // delay between each attempt
  }

  // Connected: Print network info
  Feather.printNetwork();

  Twitter.begin(CONSUMER_KEY, CONSUMER_SECRET, TOKEN_ACCESS, TOKEN_SECRET);
  Twitter.err_actions(true, true);

  Serial.print("Sending tweet: " TWEET " ... ");
  Twitter.tweet(TWEET);
  Serial.println("OK");

  Twitter.stop();
}

/**************************************************************************/
/*!
    @brief  The loop function runs over and over again forever
*/
/**************************************************************************/
void loop()
{
  togglePin(PA15);
  delay(1000);
}

/**************************************************************************/
/*!
    @brief  Connect to defined Access Point
*/
/**************************************************************************/
bool connectAP(void)
{
  // Attempt to connect to an AP
  Serial.print("Please wait while connecting to: '" WLAN_SSID "' ... ");

  if ( Feather.connect(WLAN_SSID, WLAN_PASS) )
  {
    Serial.println("Connected!");
  }
  else
  {
    Serial.printf("Failed! %s (%d)", Feather.errstr(), Feather.errno());
    Serial.println();
  }
  Serial.println();

  return Feather.connected();
}

This guide was first published on Mar 23, 2016. It was last updated on Sep 14, 2016.

This page (AdafruitTwitter) was last updated on Jul 05, 2016.

Text editor powered by tinymce.