東芝FlashAirを使って、直接ツイートするツイッタークライアントです。 supertweetやStewgate UなどのAPIプロキシを必要としません。 このプログラムは、mbed Advent Calendar 2014 をきっかけに作成されました。 http://www.adventar.org/calendars/523

Dependencies:   OAuth4Tw iSDIO mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "iSDIO.h"
00003 #include "HTTPClient.h"
00004 #include "OAuth4Tw.h"
00005 
00006 #if defined(TARGET_LPC11U24)
00007 SD_iSDIO sd(p11, p12, p13, p14, "sd"); // MOSI, MISO, SCLK, SSEL
00008 #elif defined(TARGET_LPC1114)
00009 SD_iSDIO sd(p11, p12, p13, p14, "sd"); // MOSI, MISO, SCLK, SSEL
00010 #elif defined(TARGET_LPC1549)
00011 SD_iSDIO sd(D11, D12, D13, D10, "sd"); // MOSI, MISO, SCLK, SSEL
00012 #else
00013 #error
00014 #endif
00015 
00016 Serial pc(USBTX, USBRX);
00017 
00018 OAuth4Tw oa4t("XXXXXXXXXXXXXXXXXXXXXXXXX",                          // Consumer key
00019               "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // Consumer secret
00020               "000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // Access token
00021               "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");     // Access token secret
00022 
00023 //**************************************************
00024 uint8_t buffer[512];
00025 void printByte(uint8_t value)
00026 {
00027     pc.printf("%x", value >> 4);
00028     pc.printf("%x", value & 0xF);
00029 }
00030 void printBytes(uint8_t* p, uint32_t len)
00031 {
00032     for (int i = 0; i < len; ++i) {
00033         printByte(p[i]);
00034     }
00035 }
00036 void printIPAddress(uint8_t* p)
00037 {
00038     pc.printf("%d", p[0]);
00039     pc.printf(".");
00040     pc.printf("%d", p[1]);
00041     pc.printf(".");
00042     pc.printf("%d", p[2]);
00043     pc.printf(".");
00044     pc.printf("%d", p[3]);
00045 }
00046 uint8_t iSDIO_status()
00047 {
00048     pc.printf("\nRead iSDIO Status Register");
00049     // Read iSDIO Status Register (E7 1.10 2.2.2.1)
00050     memset(buffer, 0, 0x200);
00051     if (!sd.readExtMemory(1, 1, 0x400, 0x200, buffer)) {
00052         return false;
00053     }
00054 #if 0
00055     for (int i = 0; i < 0x200; i++) {
00056         pc.printf("%2x ", buffer[i]);
00057         if ((i & 0xf) == 0xf) pc.printf("\n");
00058     }
00059 #endif
00060     // Show values in the common status area.
00061     pc.printf("\n == iSDIO Status Registers == ");
00062     pc.printf("\n [0400h] Command Write Status: ");
00063     if (buffer[0x000] & 0x01) pc.printf("CWU ");
00064     if (buffer[0x000] & 0x02) pc.printf("CWA ");
00065     pc.printf("\n [0420h] iSDIO Status: ");
00066     if (buffer[0x020] & 0x01) pc.printf("CRU ");
00067     if (buffer[0x020] & 0x02) pc.printf("ESU ");
00068     if (buffer[0x020] & 0x04) pc.printf("MCU ");
00069     if (buffer[0x020] & 0x08) pc.printf("ASU ");
00070     pc.printf("\n [0422h] iSDIO Int Enable: ");
00071     if (buffer[0x022] & 0x01) pc.printf("CRU_ENA ");
00072     if (buffer[0x022] & 0x02) pc.printf("ESU_ENA ");
00073     if (buffer[0x022] & 0x04) pc.printf("MCU_ENA ");
00074     if (buffer[0x022] & 0x08) pc.printf("ASU_ENA ");
00075     pc.printf("\n [0424h] Error Status: ");
00076     if (buffer[0x024] & 0x01) pc.printf("CRE ");
00077     if (buffer[0x024] & 0x02) pc.printf("CWE ");
00078     if (buffer[0x024] & 0x04) pc.printf("RRE ");
00079     if (buffer[0x024] & 0x08) pc.printf("APE ");
00080     pc.printf("\n [0426h] Memory Status: ");
00081     if (buffer[0x026] & 0x01) pc.printf("MEX ");
00082     if (buffer[0x026] & 0x02) pc.printf("FAT ");
00083     for (int i = 0; i < 8; ++i) {
00084         uint8_t addr = 0x40 + i * 0x14;
00085         pc.printf("\n [04");
00086         printByte(addr);
00087         pc.printf("h] Command Response Status #");
00088         pc.printf("%d", i + 1);
00089         pc.printf(": ");
00090         if (buffer[addr] & 0x01) {
00091             pc.printf("id = ");
00092             pc.printf("%d", get_u16(buffer + addr + 2));
00093             pc.printf(", sequence id = ");
00094             pc.printf("%d", get_u32(buffer + addr + 4));
00095             pc.printf(", status = ");
00096             switch (buffer[addr + 8]) {
00097                 case 0x00:
00098                     pc.printf("Initial");
00099                     break;
00100                 case 0x01:
00101                     pc.printf("Command Processing");
00102                     break;
00103                 case 0x02:
00104                     pc.printf("Command Rejected");
00105                     break;
00106                 case 0x03:
00107                     pc.printf("Process Succeeded");
00108                     break;
00109                 case 0x04:
00110                     pc.printf("Process Terminated");
00111                     break;
00112                 default:
00113                     pc.printf("Process Failed ");
00114                     pc.printf("%h", buffer[addr + 8]);
00115                     break;
00116             }
00117         } else {
00118             pc.printf("Not registered");
00119         }
00120     }
00121     // Show values in the application status area.
00122     pc.printf("\n == Wireless LAN Status Registers ==");
00123     pc.printf("\n [0500h] DLNA Status: ");
00124     if (buffer[0x100] & 0x01) pc.printf("ULR ");
00125     if (buffer[0x100] & 0x02) pc.printf("DLU ");
00126     if (buffer[0x100] & 0x04) pc.printf("CBR ");
00127     if (buffer[0x100] & 0x08) pc.printf("CDR ");
00128     pc.printf("\n [0501h] P2P Status: ");
00129     if (buffer[0x101] & 0x01) pc.printf("ILU ");
00130     if (buffer[0x101] & 0x02) pc.printf("FLU ");
00131     pc.printf("\n [0502h] PTP Status: ");
00132     if (buffer[0x102] & 0x01) pc.printf("RPO ");
00133     if (buffer[0x102] & 0x02) pc.printf("RPD ");
00134     if (buffer[0x102] & 0x04) pc.printf("RPC ");
00135     if (buffer[0x102] & 0x08) pc.printf("CPI ");
00136     if (buffer[0x102] & 0x10) pc.printf("DPI ");
00137     if (buffer[0x102] & 0x20) pc.printf("CIL ");
00138     pc.printf("\n [0504h] Application: ");
00139     pc.printf((char *)buffer[0x104]);
00140     pc.printf("\n [0506h] WLAN: ");
00141     if ((buffer[0x106] & 0x01) == 0x00) pc.printf("No Scan, ");
00142     if ((buffer[0x106] & 0x01) == 0x01) pc.printf("Scanning, ");
00143     if ((buffer[0x106] & 0x06) == 0x00) pc.printf("No WPS, ");
00144     if ((buffer[0x106] & 0x06) == 0x02) pc.printf("WPS with PIN, ");
00145     if ((buffer[0x106] & 0x06) == 0x04) pc.printf("WPS with PBC, ");
00146     if ((buffer[0x106] & 0x08) == 0x00) pc.printf("Group Client, ");
00147     if ((buffer[0x106] & 0x08) == 0x08) pc.printf("Group Owner ");
00148     if ((buffer[0x106] & 0x10) == 0x00) pc.printf("STA, ");
00149     if ((buffer[0x106] & 0x10) == 0x10) pc.printf("AP, ");
00150     if ((buffer[0x106] & 0x60) == 0x00) pc.printf("Initial, ");
00151     if ((buffer[0x106] & 0x60) == 0x20) pc.printf("Infrastructure, ");
00152     if ((buffer[0x106] & 0x60) == 0x40) pc.printf("Wi-Fi Direct, ");
00153     if ((buffer[0x106] & 0x80) == 0x00) pc.printf("No Connection, ");
00154     if ((buffer[0x106] & 0x80) == 0x80) pc.printf("Connected, ");
00155     pc.printf("\n [0508h] SSID: ");
00156     for (int i = 0; i < 32 && buffer[0x108 + i] != 0; ++i) {
00157         pc.printf("%c", (char)buffer[0x108 + i]);
00158     }
00159     pc.printf("\n [0528h] Encryption Mode: ");
00160     switch (buffer[0x128]) {
00161         case 0 :
00162             pc.printf("Open System and no encryption");
00163             break;
00164         case 1 :
00165             pc.printf("Open System and WEP");
00166             break;
00167         case 2 :
00168             pc.printf("Shared Key and WEP");
00169             break;
00170         case 3 :
00171             pc.printf("WPA-PSK and TKIP");
00172             break;
00173         case 4 :
00174             pc.printf("WPA-PSK and AES");
00175             break;
00176         case 5 :
00177             pc.printf("WPA2-PSK and TKIP");
00178             break;
00179         case 6 :
00180             pc.printf("WPA2-PSK and AES");
00181             break;
00182         default:
00183             pc.printf("Unknown");
00184     }
00185     pc.printf("\n [0529h] Signal Strength: ");
00186     pc.printf("%d", buffer[0x129]);
00187     pc.printf("\n [052Ah] Channel: ");
00188     if (buffer[0x12A] == 0) pc.printf("No connection");
00189     else pc.printf("%d", buffer[0x12A]);
00190     pc.printf("\n [0530h] MAC Address: ");
00191     printBytes(buffer + 0x130, 6);
00192     pc.printf("\n [0540h] ID: ");
00193     for (int i = 0; i < 16 && buffer[0x140 + i] != 0; ++i) {
00194         pc.printf("%c", (char)buffer[0x140 + i]);
00195     }
00196     pc.printf("\n [0550h] IP Address: ");
00197     printIPAddress(buffer + 0x150);
00198     pc.printf("\n [0554h] Subnet Mask: ");
00199     printIPAddress(buffer + 0x154);
00200     pc.printf("\n [0558h] Default Gateway: ");
00201     printIPAddress(buffer + 0x158);
00202     pc.printf("\n [055Ch] Preferred DNS Server: ");
00203     printIPAddress(buffer + 0x15C);
00204     pc.printf("\n [0560h] Alternate DNS Server: ");
00205     printIPAddress(buffer + 0x160);
00206     pc.printf("\n [0564h] Proxy Server: ");
00207     if ((buffer[0x164] & 0x01) == 0x00) pc.printf("Disabled");
00208     if ((buffer[0x164] & 0x01) == 0x01) pc.printf("Enabled");
00209     pc.printf("\n [0570h] Date: ");
00210     pc.printf("%d", buffer[0x171] + 1980);
00211     pc.printf("-");
00212     pc.printf("%d", buffer[0x170] >> 4);
00213     pc.printf("-");
00214     pc.printf("%d", buffer[0x170] & 0xF);
00215     pc.printf("\n [0572h] Time: ");
00216     pc.printf("%d", buffer[0x173] >> 3);
00217     pc.printf(":");
00218     pc.printf("%d", buffer[0x172] << 3 | buffer[0x170] >> 3);
00219     pc.printf(":");
00220     pc.printf("%d", (buffer[0x172] & 0x1F) * 2);
00221     pc.printf("\n [0574h] HTTP Status: ");
00222     pc.printf("%d", buffer[0x174] & 0xEF);
00223     if ((buffer[0x174] & 0x80) == 0x00) pc.printf(" (No Processing)");
00224     if ((buffer[0x174] & 0x80) == 0x80) pc.printf(" (Processing)");
00225     pc.printf("\n [0575h] Power Save Management: ");
00226     if ((buffer[0x175] & 0x01) == 0x00) pc.printf("Power Save Mode Off");
00227     if ((buffer[0x175] & 0x01) == 0x01) pc.printf("Power Save Mode On");
00228     pc.printf("\n [0576h] File System Management: ");
00229     if ((buffer[0x176] & 0x01) == 0x00) pc.printf("FS Information may be modified");
00230     if ((buffer[0x176] & 0x01) == 0x01) pc.printf("FS Information shall not be modified");
00231     pc.printf("\n");
00232 
00233 #if 0
00234     memset(buffer, 0, 0x200);
00235     if (!sd2->readExtMemory(1, 1, 0x600, 0x100, buffer)) {
00236         return false;
00237     }
00238 
00239     for (int i = 0; i < 0x100; i++) {
00240         pc.printf("%2x ", buffer[i]);
00241         if ((i & 0xf) == 0xf) pc.printf("\n");
00242     }
00243 #endif
00244 
00245     return true;
00246 }
00247 //**************************************************
00248 
00249 int main()
00250 {
00251     pc.printf("start.\n");
00252 
00253     //Initialise card
00254     FILE *fp = fopen("/sd/mbed.txt", "w");
00255     fclose(fp);
00256 
00257     // WiFi Connection --------------------
00258 
00259     // Connection wait
00260     while(1) {
00261         memset(buffer, 0, 0x14);
00262         if (!sd.readExtMemory(1, 1, 0x506, 0x14, buffer)) {
00263             return -1;
00264         }
00265         uint8_t resp = get_u8(buffer);
00266         if ((resp & 0x80) != 0) {
00267             break;
00268         }
00269         pc.printf(".\n");
00270         wait_ms(1000);
00271     }
00272 
00273     // Dump status
00274     if (iSDIO_status() == false) {
00275         pc.printf("Failed to read status.\n");
00276         return -1;
00277     }
00278 
00279     // Abort command
00280     for (int i = 0; i < 8; ++i) {
00281         uint8_t addr = 0x40 + i * 0x14;
00282         if (buffer[addr] & 0x01) {
00283             int abortSequenceId = get_u32(buffer + addr + 4);
00284             int sequenceId = sd.getSequenceId();
00285             memset(buffer, 0, 512);
00286             uint8_t* p = buffer;
00287             p = put_command_header(p, 1, 0);
00288             p = put_command_info_header(p, 0x12, sequenceId, 2);
00289             p = put_u32_arg(p, abortSequenceId);
00290             put_command_header(buffer, 1, (p - buffer));
00291             if ( sd.writeExtDataPort(1, 1, 0x000, buffer) == false ) {
00292                 return -1;
00293             }
00294             sd.waitResponse(sequenceId);
00295         }
00296     }
00297     for (int i = 0; i < 16; i++) {
00298         if (!sd.readExtDataPort(1, 1, 0x200, buffer)) {
00299             return -1;
00300         }
00301     }
00302 
00303     // Update time --------------------
00304 
00305     HTTPClient http;
00306     char str[512];
00307 
00308     pc.printf("Trying to update time...\n");
00309     int ret = http.get("http://ntp-a1.nict.go.jp/cgi-bin/jst", str, 128);
00310     if (ret) {
00311         pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00312         return -1;
00313     }
00314 
00315     pc.printf("Page fetched successfully - read %d characters\n", strlen(str));
00316     pc.printf("Result: %s\n", str);
00317 
00318     if (strncmp(str, "<HTML>", 6) != 0) {
00319         pc.printf("format error 1\n");
00320         return -1;
00321     }
00322     if (strncmp(&str[69], "</HTML>", 7) != 0) {
00323         pc.printf("format error 2\n");
00324         return -1;
00325     }
00326 
00327     str[56] = '\0';
00328     //pc.printf("%d\n", atol(&str[46]));
00329     set_time( atol(&str[46]) );
00330 
00331     time_t ctTime = time(NULL);
00332     pc.printf("Time is set to (UTC): %s\n", ctime(&ctTime));
00333 
00334     // Tweet --------------------
00335 
00336     std::string uri = "https://api.twitter.com/1.1/statuses/update.json";
00337 
00338     uri += "?status=";
00339     uri += OAuth4Tw::url_escape("Hello World!");
00340     uri += OAuth4Tw::url_escape(ctime(&ctTime));
00341 
00342     std::string postarg;
00343     std::string postres = oa4t.post(uri.c_str(), postarg);
00344     pc.printf("postres: %s\n", postres.c_str());
00345 
00346 }