Twitter client that can be directly tweet. (Intermediate server is not required.)

Dependencies:   EthernetInterface HTTPClient-wolfSSL NTPClient OAuth4Tw mbed-rtos mbed wolfSSL

Fork of OAuth4Tw by Atsuya Okazaki

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <string.h>
00002 #include "mbed.h"
00003 #include "EthernetInterface.h"
00004 #include "NTPClient.h"
00005 #include "OAuth4Tw.h"
00006 
00007 Serial pc(USBTX, USBRX);
00008 DigitalOut myled(LED1);
00009 EthernetInterface eth;
00010 NTPClient ntp;
00011 
00012 OAuth4Tw oa4t("XXXXXXXXXXXXXXXXXXXXXXXXX",                          // Consumer key
00013               "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // Consumer secret
00014               "000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // Access token
00015               "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");     // Access token secret
00016 
00017 #if defined(TARGET_LPC1768)
00018 #define RESPONSE_BUFFER_SIZE 512
00019 #elif defined(TARGET_K64F) || defined(TARGET_LPC4088)
00020 #define RESPONSE_BUFFER_SIZE 4096
00021 #else
00022 #error not tested platform.
00023 #endif
00024 
00025 char response_buffer[RESPONSE_BUFFER_SIZE];
00026 HTTPText response(response_buffer, sizeof(response_buffer));
00027 
00028 // prototype
00029 void updateTime();
00030 void example_tweet1();
00031 void example_tweet2();
00032 void example_tweet3();
00033 void example_getUserData();
00034 
00035 int main()
00036 {
00037     pc.baud(115200);
00038 
00039     eth.init(); //Use DHCP
00040     printf("Initialized, MAC: %s\n", eth.getMACAddress());
00041 
00042     int ret;
00043     while ((ret = eth.connect()) != 0) {
00044         printf("Error eth.connect() - ret = %d\n", ret);
00045     }
00046 
00047     printf("Connected, IP: %s, MASK: %s, GW: %s\n",
00048            eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
00049 
00050     // requires accurate time, for OAuth Authorization.
00051     updateTime();
00052 
00053     while (1) {
00054 
00055         example_tweet1();
00056         //example_tweet2();
00057         //example_tweet3();
00058 
00059         example_getUserData();
00060 
00061         // Wait 60 seconds for next time.
00062         for (int t=0; t<60; t++) {
00063             myled = 1;
00064             wait(0.2);
00065             myled = 0;
00066             wait(0.8);
00067         }
00068 
00069         printf("\n");
00070     }
00071 }
00072 
00073 
00074 void updateTime()
00075 {
00076     printf("Trying to update time...\n");
00077 
00078     time_t ctTime;
00079     NTPResult result;
00080 
00081     while (1) {
00082         result = ntp.setTime("pool.ntp.org");
00083         //result = ntp.setTime("pool.ntp.org", NTP_DEFAULT_PORT, 2000);
00084 
00085         if (result == NTP_OK) {
00086             time(&ctTime);
00087             printf("Time is set to (UTC): %s\n", ctime(&ctTime));
00088             break;
00089         }
00090 
00091         switch (result) {
00092             case NTP_CONN:      ///<Connection error
00093                 printf("Connection error\n");
00094                 break;
00095             case NTP_TIMEOUT:   ///<Connection timeout
00096                 printf("Connection timeout\n");
00097                 break;
00098             case NTP_PRTCL:     ///<Protocol error
00099                 printf("Protocol error\n");
00100                 break;
00101             case NTP_DNS:       ///<Could not resolve name
00102                 printf("Could not resolve name\n");
00103                 break;
00104             default:
00105                 printf("Error result=%d\n", result);
00106                 break;
00107         }
00108 
00109         wait(5);
00110     }
00111 }
00112 
00113 
00114 void example_tweet1()
00115 {
00116     const char url[] = "https://api.twitter.com/1.1/statuses/update.json"
00117                        "?status=Hello World! - %s";
00118     char url2[128];
00119 
00120     time_t ctTime;
00121     time(&ctTime);
00122 
00123     snprintf(url2, sizeof(url2), url, ctime(&ctTime));
00124 
00125     HTTPResult result = oa4t.post(url2, &response);
00126 
00127     if (result == HTTP_OK) {
00128         printf("POST success.\n%s\n", response_buffer);
00129     } else {
00130         printf("POST error. (result = %d)\n", result);
00131     }
00132 }
00133 
00134 void example_tweet2()
00135 {
00136     const char url[] = "https://api.twitter.com/1.1/statuses/update.json";
00137 
00138     std::vector<std::string> post;
00139     post.reserve(1);
00140 
00141     char status[128];
00142 
00143     time_t ctTime;
00144     time(&ctTime);
00145 
00146     snprintf(status, sizeof(status), "status=Hello World! - %s", ctime(&ctTime));
00147     post.push_back(status);
00148 
00149     HTTPResult result = oa4t.post(url, &post, &response);
00150 
00151     if (result == HTTP_OK) {
00152         printf("POST success.\n%s\n", response_buffer);
00153     } else {
00154         printf("POST error. (result = %d)\n", result);
00155     }
00156 }
00157 
00158 void example_tweet3()
00159 {
00160     const char url[] = "https://api.twitter.com/1.1/statuses/update.json";
00161 
00162     std::vector<std::string> post;
00163     post.reserve(3);
00164 
00165     struct tm tmptr;
00166     char tmstr[34];
00167     char status[128];
00168     char location_lat[24];
00169     char location_long[24];
00170 
00171     time_t ctTime;
00172     time(&ctTime);
00173     ctTime += 9 * 60 * 60;  // Timezone: JST(+9h)
00174     localtime_r(&ctTime, &tmptr);
00175 
00176     // Tweets in Japanese
00177     strftime(tmstr, sizeof(tmstr), "%Y年%m月%d日 %H時%M分%S秒", &tmptr);
00178     snprintf(status, sizeof(status), "status=ハロー・ワールド! - %s", tmstr);
00179     post.push_back(status);
00180 
00181     // Option: add Location information
00182     snprintf(location_lat, sizeof(location_lat), "lat=%f", 35.359577);
00183     snprintf(location_long, sizeof(location_long), "long=%f", 138.731414);
00184     post.push_back(location_lat);
00185     post.push_back(location_long);
00186 
00187     HTTPResult result = oa4t.post(url, &post, &response);
00188 
00189     if (result == HTTP_OK) {
00190         printf("POST success.\n%s\n", response_buffer);
00191     } else {
00192         printf("POST error. (result = %d)\n", result);
00193     }
00194 }
00195 
00196 void example_getUserData()
00197 {
00198     const char url[] = "https://api.twitter.com/1.1/users/show.json"
00199                        "?screen_name=twitter";
00200 
00201     HTTPResult result = oa4t.get(url, &response);
00202 
00203     if (result == HTTP_OK) {
00204         printf("GET success.\n%s\n", response_buffer);
00205     } else {
00206         printf("GET error. (result = %d)\n", result);
00207     }
00208 }