debsahu / TwitterWebAPI

An Arduino library to talk to Twitter using Twitter Web API made for ESP8266
GNU General Public License v3.0
29 stars 8 forks source link
arduino-library esp8266 twitter twitter-api

TwitterWebAPI

An Arduino library to talk to Twitter using Twitter Web API made for ESP8266. This is based on a sketch posted here. There are other approaches like using a bearer token arduino-twitter-api, but there are limitations in terms of not being able to send tweet. This can both search/read and post tweets.

ESP8266 TwitterWebAPI

Consumer Key, Consumer Secret, Access Token & Access Token Secret

In order to talk to Twitter,

Fill the obtained Consumer Key, Consumer Secret, Access Token and Access Token Secret inside the sketch.

Using the Library

To use in your sketch include these lines.

#include <TwitterWebAPI.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

Define Twitter GET request timeout (optional, default is 1500ms)

#define TWI_TIMEOUT 2000  // in msec

Setup correct timezone to correct the time obtained from NTP server.

const char *ntp_server = "pool.ntp.org";  // time1.google.com, time.nist.gov, pool.ntp.org
int timezone = -5;                        // US Eastern timezone -05:00 HRS

You WILL need Consumer Key, Consumer Secret, Access Token and Access Token Secret that can be obtained from the above steps.

// Values below are just a placeholder
// Obtain these by creating an app @ https://apps.twitter.com/
  static char const consumer_key[]    = "gkyjeH3EF32NJfiuheuyf8623";
  static char const consumer_sec[]    = "HbY5h$N86hg5jjd987HGFsRjJcMkjLaJw44628sOh353gI3H23";
  static char const accesstoken[]     = "041657084136508135-F3BE63U4Y6b346kj6bnkdlvnjbGsd3V";
  static char const accesstoken_sec[] = "bsekjH8YT3dCWDdsgsdHUgdBiosesDgv43rknU4YY56Tj";

Declare clients before setup().

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntp_server, timezone*3600, 60000);  // NTP server pool, offset (in seconds), update interval (in milliseconds)
TwitterClient tcr(timeClient, consumer_key, consumer_sec, accesstoken, accesstoken_sec);

In setup make sure to start NTP connection. A correct time is required to be able to post/search on Twitter.

tcr.startNTP();

Search for a key word

std::string search_str;
String tmsg = tcr.searchTwitter(search_str);

Search Twitter User Info (eg follower count)

std::string search_str;
tcr.searchUser(search_str);

Post to Twitter

std::string twitter_post_msg;
tcr.tweet(twitter_post_msg);

Example: TwitterTweetSearchFSWiFiMgr

In addition to TwitterWebAPI, make sure that TimeLib library and NTPClient library is installed, and install the following libraries

For display you can either install