![](/media/cache/profiles/266dc8d468ccd5a7def18714b2647e92.jpg.50x50_q85.jpg)
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
Revision 6:f5189d4f109f, committed 2015-07-18
- Comitter:
- ban4jp
- Date:
- Sat Jul 18 16:23:11 2015 +0000
- Parent:
- 5:f0c0128cde62
- Commit message:
- Rewrite
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r f0c0128cde62 -r f5189d4f109f main.cpp --- a/main.cpp Tue Jul 14 09:54:39 2015 +0000 +++ b/main.cpp Sat Jul 18 16:23:11 2015 +0000 @@ -4,6 +4,7 @@ #include "NTPClient.h" #include "OAuth4Tw.h" +Serial pc(USBTX, USBRX); DigitalOut myled(LED1); EthernetInterface eth; NTPClient ntp; @@ -14,33 +15,64 @@ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); // Access token secret #if defined(TARGET_LPC1768) -static char res_buffer[512]; -#elif defined(TARGET_K64F) -static char res_buffer[4096]; +#define RESPONSE_BUFFER_SIZE 512 +#elif defined(TARGET_K64F) || defined(TARGET_LPC4088) +#define RESPONSE_BUFFER_SIZE 4096 #else #error not tested platform. #endif +char response_buffer[RESPONSE_BUFFER_SIZE]; +HTTPText response(response_buffer, sizeof(response_buffer)); + +// prototype +void updateTime(); +void example_tweet1(); +void example_tweet2(); +void example_tweet3(); +void example_getUserData(); + int main() { - int ret = eth.init(); //Use DHCP - if (!ret) { - printf("Initialized, MAC: %s\n", eth.getMACAddress()); - } else { - printf("Error eth.init() - ret = %d\n", ret); - return -1; + pc.baud(115200); + + eth.init(); //Use DHCP + printf("Initialized, MAC: %s\n", eth.getMACAddress()); + + int ret; + while ((ret = eth.connect()) != 0) { + printf("Error eth.connect() - ret = %d\n", ret); } - ret = eth.connect(); - if (!ret) { - printf("Connected, IP: %s, MASK: %s, GW: %s\n", - eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); - } else { - printf("Error eth.connect() - ret = %d\n", ret); - return -1; + printf("Connected, IP: %s, MASK: %s, GW: %s\n", + eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); + + // requires accurate time, for OAuth Authorization. + updateTime(); + + while (1) { + + example_tweet1(); + //example_tweet2(); + //example_tweet3(); + + example_getUserData(); + + // Wait 60 seconds for next time. + for (int t=0; t<60; t++) { + myled = 1; + wait(0.2); + myled = 0; + wait(0.8); + } + + printf("\n"); } +} +void updateTime() +{ printf("Trying to update time...\n"); time_t ctTime; @@ -71,71 +103,106 @@ break; default: printf("Error result=%d\n", result); - return -1; + break; } wait(5); } +} - HTTPText res(res_buffer, sizeof(res_buffer)); +void example_tweet1() +{ + const char url[] = "https://api.twitter.com/1.1/statuses/update.json" + "?status=Hello World! - %s"; + char url2[128]; - while (1) { -#if 0 - { - char url[128]; - - time(&ctTime); - - snprintf(url, sizeof(url), - "https://api.twitter.com/1.1/statuses/update.json" - "?status=Hello World! - %s", ctime(&ctTime)); + time_t ctTime; + time(&ctTime); - res_buffer[0] = '\0'; - HTTPResult result = oa4t.post(url, &res); - printf("POST result = %d\n%s\n", result, res_buffer); - } -#else - { - std::vector<std::string> post; - post.reserve(3); + snprintf(url2, sizeof(url2), url, ctime(&ctTime)); + + HTTPResult result = oa4t.post(url2, &response); - char status[64]; - char location_lat[24]; - char location_long[24]; - - time(&ctTime); + if (result == HTTP_OK) { + printf("POST success.\n%s\n", response_buffer); + } else { + printf("POST error. (result = %d)\n", result); + } +} - snprintf(status, sizeof(status), "status=Hello Fujiyama! - %s", ctime(&ctTime)); - snprintf(location_lat, sizeof(location_lat), "lat=%f", 35.359577); - snprintf(location_long, sizeof(location_long), "long=%f", 138.731414); - post.push_back(status); - post.push_back(location_lat); - post.push_back(location_long); +void example_tweet2() +{ + const char url[] = "https://api.twitter.com/1.1/statuses/update.json"; - res_buffer[0] = '\0'; - HTTPResult result = oa4t.post("https://api.twitter.com/1.1/statuses/update.json", &post, &res); - printf("POST result = %d\n%s\n", result, res_buffer); - } -#endif + std::vector<std::string> post; + post.reserve(1); + + char status[128]; - { - const char url[] = "https://api.twitter.com/1.1/users/show.json" - "?screen_name=twitter"; + time_t ctTime; + time(&ctTime); - res_buffer[0] = '\0'; - HTTPResult result = oa4t.get(url, &res); - printf("GET result = %d\n%s\n", result, res_buffer); - } + snprintf(status, sizeof(status), "status=Hello World! - %s", ctime(&ctTime)); + post.push_back(status); - // Wait 60 seconds for next time. - for (int t=0; t<60; t++) { - myled = 1; - wait(0.2); - myled = 0; - wait(0.8); - } + HTTPResult result = oa4t.post(url, &post, &response); - printf("\n"); + if (result == HTTP_OK) { + printf("POST success.\n%s\n", response_buffer); + } else { + printf("POST error. (result = %d)\n", result); } } + +void example_tweet3() +{ + const char url[] = "https://api.twitter.com/1.1/statuses/update.json"; + + std::vector<std::string> post; + post.reserve(3); + + struct tm tmptr; + char tmstr[34]; + char status[128]; + char location_lat[24]; + char location_long[24]; + + time_t ctTime; + time(&ctTime); + ctTime += 9 * 60 * 60; // Timezone: JST(+9h) + localtime_r(&ctTime, &tmptr); + + // Tweets in Japanese + strftime(tmstr, sizeof(tmstr), "%Y年%m月%d日 %H時%M分%S秒", &tmptr); + snprintf(status, sizeof(status), "status=ハロー・ワールド! - %s", tmstr); + post.push_back(status); + + // Option: add Location information + snprintf(location_lat, sizeof(location_lat), "lat=%f", 35.359577); + snprintf(location_long, sizeof(location_long), "long=%f", 138.731414); + post.push_back(location_lat); + post.push_back(location_long); + + HTTPResult result = oa4t.post(url, &post, &response); + + if (result == HTTP_OK) { + printf("POST success.\n%s\n", response_buffer); + } else { + printf("POST error. (result = %d)\n", result); + } +} + +void example_getUserData() +{ + const char url[] = "https://api.twitter.com/1.1/users/show.json" + "?screen_name=twitter"; + + HTTPResult result = oa4t.get(url, &response); + + if (result == HTTP_OK) { + printf("GET success.\n%s\n", response_buffer); + } else { + printf("GET error. (result = %d)\n", result); + } +}