Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface HTTPClient-wolfSSL NTPClient OAuth4Tw mbed-rtos mbed wolfSSL
Fork of CyaSSL-Twitter-OAuth4Tw by
main.cpp
00001 // Original: https://developer.mbed.org/users/ban4jp/code/CyaSSL-Twitter-OAuth4Tw/ 00002 00003 #include <string.h> 00004 #include "mbed.h" 00005 #include "EthernetInterface.h" 00006 #include "NTPClient.h" 00007 #include "OAuth4Tw.h" 00008 #include "lwip/dns.h" 00009 00010 00011 Serial pc(USBTX, USBRX); 00012 DigitalOut myled(LED1); 00013 EthernetInterface eth; 00014 NTPClient ntp; 00015 00016 OAuth4Tw oa4t("f3fcA4S61FcOz0VMLgia9ewn2",// Consumer key 00017 "Djaz2ooEf95b8yr3yE7DWS1XrYuwjBYZdz7QqnDKFTKNdMd8Ml", // Consumer secret 00018 "4044135739-da2aYe6fs6aCcpCKlqagVxM3rjDj5yPUaJaZ7O7", // Access token 00019 "uIrtt88FWWIz2yqpU9AkBeoLEHW5ls5ZdA9SoQvcGY47c");// Access token secret 00020 00021 00022 00023 #if defined(TARGET_LPC1768) 00024 #define RESPONSE_BUFFER_SIZE 512 00025 #elif defined(TARGET_K64F) || defined(TARGET_LPC4088) 00026 #define RESPONSE_BUFFER_SIZE 4096 00027 #else 00028 #error not tested platform. 00029 #endif 00030 00031 char response_buffer[RESPONSE_BUFFER_SIZE]; 00032 HTTPText response(response_buffer, sizeof(response_buffer)); 00033 00034 // prototype 00035 void updateTime(); 00036 void example_tweet1(); 00037 void example_tweet2(); 00038 void example_tweet3(); 00039 void example_getUserData(); 00040 void mm2(void); 00041 00042 Ticker mmTicker; 00043 volatile int sum = 0; 00044 00045 int main() 00046 { 00047 pc.baud(115200); 00048 00049 eth.init(); 00050 // eth.init("172.20.29.40","255.255.255.0","172.20.29.1"); 00051 // printf("Initialized, MAC: %s\n", eth.getMACAddress()); 00052 00053 // ip_addr_t primaryDnsServer; 00054 // ip4_addr_set_u32(&primaryDnsServer, ipaddr_addr("202.18.66.8")); 00055 // dns_setserver(0, &primaryDnsServer); 00056 00057 int ret; 00058 while ((ret = eth.connect()) != 0) { 00059 printf("Error eth.connect() - ret = %d\n", ret); 00060 } 00061 00062 printf("Connected, IP: %s, MASK: %s, GW: %s\n", 00063 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); 00064 00065 // requires accurate time, for OAuth Authorization. 00066 updateTime(); 00067 00068 00069 // example_getUserData(); 00070 // printf("\n"); 00071 00072 myled = 0; 00073 time_t ctTime; 00074 struct tm* lTime; 00075 int hour; 00076 int countmin = 0; 00077 00078 00079 /*tickerオブジェクトで0.1秒ごとにmm2関数を呼び出す*/ 00080 mmTicker.attach(&mm2, 0.5); 00081 00082 while (1) { 00083 time(&ctTime); 00084 lTime = localtime(&ctTime); 00085 hour = lTime->tm_hour; 00086 countmin = 5; 00087 if (12<=hour&&hour<=13) { 00088 countmin=15; 00089 } 00090 00091 00092 // Wait 60*5 seconds for next time. 00093 for (int t=0; t<60*countmin; t++) { 00094 myled = !myled; 00095 wait_ms(1000); 00096 printf("%d\n", sum); 00097 //time(&ctTime); 00098 //lTime = localtime(&ctTime); 00099 //hour = lTime->tm_hour; 00100 //if (12<=hour&&hour<=13) break; 00101 } 00102 example_tweet2(); 00103 } 00104 // mmTicker.detach(); 00105 } 00106 00107 00108 void updateTime() 00109 { 00110 printf("Trying to update time...\n"); 00111 00112 time_t ctTime; 00113 NTPResult result; 00114 00115 while (1) { 00116 result = ntp.setTime("ntp.nict.jp"); 00117 00118 if (result == NTP_OK) { 00119 time(&ctTime); 00120 printf("Time is set to (UTC): %s\n", ctime(&ctTime)); 00121 break; 00122 } 00123 00124 switch (result) { 00125 case NTP_CONN: ///<Connection error 00126 printf("Connection error\n"); 00127 break; 00128 case NTP_TIMEOUT: ///<Connection timeout 00129 printf("Connection timeout\n"); 00130 break; 00131 case NTP_PRTCL: ///<Protocol error 00132 printf("Protocol error\n"); 00133 break; 00134 case NTP_DNS: ///<Could not resolve name 00135 printf("Could not resolve name\n"); 00136 break; 00137 default: 00138 printf("Error result=%d\n", result); 00139 break; 00140 } 00141 00142 wait(5); 00143 } 00144 } 00145 00146 00147 00148 /*センサ安定時間*/ 00149 #define INIT 30.0 00150 /*ノイズ検出用*/ 00151 #define NOISE 0.1 00152 /*センサ再読み取り時間*/ 00153 #define DELAY 0.05 00154 00155 DigitalIn pA(p19); 00156 DigitalIn pB(p20); 00157 DigitalOut led2(LED2); 00158 00159 void mm2(void) 00160 { 00161 int nA = pA; 00162 int nB = pB; 00163 static int prevA = 0; 00164 static int prevB = 0; 00165 00166 int status = 0; 00167 static int prevUpStatus = 0; 00168 00169 if (prevA == nA) 00170 status = nA; 00171 if (prevB == nB) 00172 status |= (nB << 1); 00173 if (prevUpStatus == 0x03) { 00174 if (status == 0x02) 00175 sum++; 00176 else if (status == 0x01) 00177 sum--; 00178 } 00179 00180 prevA = nA; 00181 prevB = nB; 00182 00183 if (status) 00184 prevUpStatus = status; 00185 } 00186 00187 00188 void mm(void) { 00189 int a = 0; 00190 int b = 0; 00191 int c = 0; 00192 int d = 0; 00193 int plus = 0; 00194 int minus = 0; 00195 led2 = 0; 00196 00197 // pc.printf("a=%d b=%d c=%d d=%d\n",a,b,c,d); 00198 00199 if(pA){ 00200 wait(NOISE); 00201 if(pA){ 00202 a ++; 00203 // pc.printf("a=%d\n",a); 00204 wait(DELAY); 00205 if(pB){ 00206 wait(NOISE); 00207 if(pB){ 00208 b ++; 00209 // pc.printf("b=%d\n",b); 00210 wait(DELAY); 00211 plus ++; 00212 // pc.printf("plus=%d\n",plus); 00213 } 00214 } 00215 } 00216 wait(DELAY); 00217 } 00218 00219 if(pB){ 00220 wait(NOISE); 00221 if(pB){ 00222 c ++; 00223 // pc.printf("c=%d\n",c); 00224 wait(DELAY); 00225 if(pA){ 00226 wait(NOISE); 00227 if(pA){ 00228 d ++; 00229 // pc.printf("d=%d\n",d); 00230 wait(DELAY); 00231 minus ++; 00232 // pc.printf("minus=%d\n",minus); 00233 00234 } 00235 } 00236 } 00237 wait(DELAY); 00238 } 00239 sum = plus - minus ; 00240 // pc.printf("sum=%d\n",sum); 00241 } 00242 00243 void example_tweet1() 00244 { 00245 const char url[] = "https://api.twitter.com/1.1/statuses/update.json?status=Hello Everybody! Now, the number of people in the co-op - %s"; 00246 char url2[128]; 00247 00248 00249 time_t ctTime; 00250 00251 time(&ctTime); 00252 00253 snprintf(url2, sizeof(url2), url, ctime(&ctTime)); 00254 00255 HTTPResult result = oa4t.post(url2, &response); 00256 00257 if (result == HTTP_OK) { 00258 printf("POST success.\n%s\n", response_buffer); 00259 } else { 00260 printf("POST error. (result = %d)\n", result); 00261 } 00262 } 00263 00264 void example_tweet2(void) 00265 { 00266 const char url[] = "https://api.twitter.com/1.1/statuses/update.json"; 00267 wait(INIT); 00268 std::vector<std::string> post; 00269 post.reserve(1); 00270 00271 char status[128]; 00272 00273 time_t ctTime; 00274 time(&ctTime); 00275 00276 snprintf(status, sizeof(status), "status=Hello Everybody! Now, the number of people in the co-op - %d %s",sum, ctime(&ctTime)); 00277 post.push_back(status); 00278 00279 HTTPResult result = oa4t.post(url, &post, &response); 00280 00281 if (result == HTTP_OK) { 00282 printf("POST success.\n%s\n", response_buffer); 00283 } else { 00284 printf("POST error. (result = %d)\n", result); 00285 } 00286 } 00287 /* 00288 void example_tweet3() 00289 { 00290 const char url[] = "https://api.twitter.com/1.1/statuses/update.json"; 00291 00292 std::vector<std::string> post; 00293 post.reserve(3); 00294 00295 struct tm tmptr; 00296 char tmstr[34]; 00297 char status[128]; 00298 char location_lat[24]; 00299 char location_long[24]; 00300 00301 time_t ctTime; 00302 time(&ctTime); 00303 ctTime += 9 * 60 * 60; // Timezone: JST(+9h) 00304 localtime_r(&ctTime, &tmptr); 00305 00306 // Tweets in Japanese 00307 strftime(tmstr, sizeof(tmstr), "%Y年%m月%d日 %H時%M分%S秒", &tmptr); 00308 snprintf(status, sizeof(status), "status=ハロー・ワールド! - %s", tmstr); 00309 post.push_back(status); 00310 00311 // Option: add Location information 00312 snprintf(location_lat, sizeof(location_lat), "lat=%f", 35.359577); 00313 snprintf(location_long, sizeof(location_long), "long=%f", 138.731414); 00314 post.push_back(location_lat); 00315 post.push_back(location_long); 00316 00317 HTTPResult result = oa4t.post(url, &post, &response); 00318 00319 if (result == HTTP_OK) { 00320 printf("POST success.\n%s\n", response_buffer); 00321 } else { 00322 printf("POST error. (result = %d)\n", result); 00323 } 00324 } 00325 */ 00326 00327 void example_getUserData() 00328 { 00329 const char url[] = "https://api.twitter.com/1.1/users/show.json" 00330 "?screen_name=twitter"; 00331 00332 HTTPResult result = oa4t.get(url, &response); 00333 00334 if (result == HTTP_OK) { 00335 printf("GET success.\n%s\n", response_buffer); 00336 } else { 00337 printf("GET error. (result = %d)\n", result); 00338 } 00339 }
Generated on Fri Jul 22 2022 03:43:08 by
1.7.2
