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

Committer:
ban4jp
Date:
Sun Feb 01 17:39:08 2015 +0000
Revision:
2:392b8e079c7a
Parent:
1:4dfa9d41f414
Child:
4:fcadeb9bdcbd
Update library.; Fixed the NTP processing.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
geodenx 0:00a2d3570824 1 #include <string.h>
geodenx 0:00a2d3570824 2 #include "mbed.h"
ban4jp 1:4dfa9d41f414 3 #include "EthernetInterface.h"
ban4jp 1:4dfa9d41f414 4 #include "NTPClient.h"
geodenx 0:00a2d3570824 5 #include "OAuth4Tw.h"
geodenx 0:00a2d3570824 6
geodenx 0:00a2d3570824 7 DigitalOut myled(LED1);
ban4jp 1:4dfa9d41f414 8 EthernetInterface eth;
ban4jp 1:4dfa9d41f414 9 NTPClient ntp;
ban4jp 1:4dfa9d41f414 10
ban4jp 1:4dfa9d41f414 11 OAuth4Tw oa4t("XXXXXXXXXXXXXXXXXXXXXXXXX", // Consumer key
ban4jp 1:4dfa9d41f414 12 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // Consumer secret
ban4jp 1:4dfa9d41f414 13 "000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // Access token
ban4jp 1:4dfa9d41f414 14 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); // Access token secret
geodenx 0:00a2d3570824 15
ban4jp 1:4dfa9d41f414 16 int main()
ban4jp 1:4dfa9d41f414 17 {
ban4jp 1:4dfa9d41f414 18 int ret = eth.init(); //Use DHCP
ban4jp 1:4dfa9d41f414 19 if (!ret) {
ban4jp 1:4dfa9d41f414 20 printf("Initialized, MAC: %s\n", eth.getMACAddress());
ban4jp 1:4dfa9d41f414 21 } else {
ban4jp 1:4dfa9d41f414 22 printf("Error eth.init() - ret = %d\n", ret);
geodenx 0:00a2d3570824 23 return -1;
geodenx 0:00a2d3570824 24 }
geodenx 0:00a2d3570824 25
ban4jp 1:4dfa9d41f414 26 ret = eth.connect();
ban4jp 1:4dfa9d41f414 27 if (!ret) {
ban4jp 1:4dfa9d41f414 28 printf("Connected, IP: %s, MASK: %s, GW: %s\n",
ban4jp 1:4dfa9d41f414 29 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
ban4jp 1:4dfa9d41f414 30 } else {
ban4jp 1:4dfa9d41f414 31 printf("Error eth.connect() - ret = %d\n", ret);
ban4jp 1:4dfa9d41f414 32 return -1;
ban4jp 1:4dfa9d41f414 33 }
ban4jp 1:4dfa9d41f414 34
ban4jp 1:4dfa9d41f414 35 printf("Trying to update time...\n");
ban4jp 1:4dfa9d41f414 36
ban4jp 2:392b8e079c7a 37 time_t ctTime;
ban4jp 2:392b8e079c7a 38 NTPResult result;
ban4jp 2:392b8e079c7a 39
ban4jp 2:392b8e079c7a 40 while(1) {
ban4jp 2:392b8e079c7a 41 result = ntp.setTime("pool.ntp.org");
ban4jp 2:392b8e079c7a 42 //result = ntp.setTime("pool.ntp.org", NTP_DEFAULT_PORT, 2000);
ban4jp 2:392b8e079c7a 43
ban4jp 2:392b8e079c7a 44 if (result == NTP_OK) {
ban4jp 2:392b8e079c7a 45 time(&ctTime);
ban4jp 2:392b8e079c7a 46 printf("Time is set to (UTC): %s\n", ctime(&ctTime));
ban4jp 2:392b8e079c7a 47 break;
ban4jp 2:392b8e079c7a 48 }
ban4jp 2:392b8e079c7a 49
ban4jp 2:392b8e079c7a 50 switch (result) {
ban4jp 2:392b8e079c7a 51 case NTP_CONN: ///<Connection error
ban4jp 2:392b8e079c7a 52 printf("Connection error\n");
ban4jp 2:392b8e079c7a 53 break;
ban4jp 2:392b8e079c7a 54 case NTP_TIMEOUT: ///<Connection timeout
ban4jp 2:392b8e079c7a 55 printf("Connection timeout\n");
ban4jp 2:392b8e079c7a 56 break;
ban4jp 2:392b8e079c7a 57 case NTP_PRTCL: ///<Protocol error
ban4jp 2:392b8e079c7a 58 printf("Protocol error\n");
ban4jp 2:392b8e079c7a 59 break;
ban4jp 2:392b8e079c7a 60 case NTP_DNS: ///<Could not resolve name
ban4jp 2:392b8e079c7a 61 printf("Could not resolve name\n");
ban4jp 2:392b8e079c7a 62 break;
ban4jp 2:392b8e079c7a 63 default:
ban4jp 2:392b8e079c7a 64 printf("Error result=%d\n", result);
ban4jp 2:392b8e079c7a 65 return -1;
ban4jp 2:392b8e079c7a 66 }
ban4jp 2:392b8e079c7a 67
ban4jp 2:392b8e079c7a 68 wait(5);
ban4jp 1:4dfa9d41f414 69 }
ban4jp 1:4dfa9d41f414 70
ban4jp 1:4dfa9d41f414 71 std::string uri = "https://api.twitter.com/1.1/statuses/update.json";
geodenx 0:00a2d3570824 72 uri += "?status=";
geodenx 0:00a2d3570824 73 uri += OAuth4Tw::url_escape("Hello World!");
ban4jp 2:392b8e079c7a 74 uri += OAuth4Tw::url_escape(" - ");
ban4jp 2:392b8e079c7a 75 uri += OAuth4Tw::url_escape(ctime(&ctTime));
geodenx 0:00a2d3570824 76 std::string postarg;
geodenx 0:00a2d3570824 77 std::string postres = oa4t.post(uri.c_str(), postarg);
geodenx 0:00a2d3570824 78 printf("postres: %s\n", postres.c_str());
geodenx 0:00a2d3570824 79
geodenx 0:00a2d3570824 80 while (1) {
geodenx 0:00a2d3570824 81 myled = 1;
geodenx 0:00a2d3570824 82 wait(0.2);
geodenx 0:00a2d3570824 83 myled = 0;
geodenx 0:00a2d3570824 84 wait(0.2);
geodenx 0:00a2d3570824 85 }
geodenx 0:00a2d3570824 86 }