display your latest tweet with WiFly RN-XV and AD128160-UART

Dependencies:   AD128160_kazushi_branch HTTPClient mbed PowerControl WiflyInterface

Fork of FontTest3 by Sim mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Twitter Mbed with WiFly RN-XV
00002 // 
00003 // coded by Kazushi Mukaiyama (http://www.kazushi.info/)
00004 // refer to cookbook of WiFly(https://mbed.org/cookbook/wifly)
00005 // refer to GingaX's notebook (http://www31.atwiki.jp/gingax/pages/63.html)
00006 
00007 #include "mbed.h"
00008 #include "PowerControl/PowerControl.h"
00009 #include "PowerControl/EthernetPowerControl.h"
00010 #include "WiflyInterface.h"
00011 #include "HTTPClient.h"
00012 #include "sjis_utf16.h"
00013 #include "s_Lcd.h"
00014 
00015 #define countof(x) ( sizeof(x) / sizeof(x[0]) )
00016 
00017 #define SSID "your_ssid"
00018 #define PASS "your_pass"
00019 
00020 #define HTTP_URI "http://api.supertweet.net/1.1/statuses/user_timeline.json?screen_name=pla3c&count=1"
00021 #define HTTP_USER "your_id"
00022 #define HTTP_PASS "your_pass"
00023 
00024 #define INTERVAL_SEC 90.0
00025 
00026 //#define _DEBUG
00027 
00028 WiflyInterface wifly(p13, p14, p15, p16, SSID, PASS, WPA); // TX, RX, reset, connection_status
00029 HTTPClient http;
00030 DigitalOut led1(LED1), led2(LED2);
00031 #ifdef _DEBUG
00032     Serial pc(USBTX, USBRX);
00033 #endif
00034 
00035 char buf[512];
00036 
00037 unsigned char kstatus = 0;
00038 bool estate = false;
00039 unsigned char unibuf[4];
00040 unsigned char codes[512];
00041 
00042 unsigned short convert(unsigned short utf){
00043     unsigned short sjis = NULL; 
00044     for(int i=0; i<countof(sjis2utf16)/2; i++){
00045         if(utf==sjis2utf16[i*2+1]){
00046             sjis = sjis2utf16[i*2];
00047             break;
00048         }
00049     }
00050     return sjis;
00051 }
00052 
00053 bool parseCode(const unsigned char* s){
00054     unsigned int i = 0;
00055     unsigned char c;
00056     
00057     bool em = false;
00058     while((c = *s++) != '\0') {
00059         if(estate && kstatus==4){ // 1st digit of utf-16
00060             unibuf[3] = c;
00061             char *endptr;
00062             unsigned short code = convert(strtol((char*)unibuf, &endptr, 16));
00063             codes[i] = code>>8 & 0xFF;
00064             i++;
00065             codes[i] = code & 0xFF;
00066             i++;
00067             if(code==0x8149) em = true;
00068     #ifdef _DEBUG
00069             pc.printf("%s, %x\r\n", unibuf, code);
00070     #endif
00071             kstatus = 0;
00072             estate = false;
00073         } else if(estate && kstatus==3){ // 2nd digit of utf-16
00074             unibuf[2] = c;
00075             kstatus = 4;
00076         } else if(estate && kstatus==2){ // 3rd digit of utf-16
00077             unibuf[1] = c;
00078             kstatus = 3;
00079         } else if(estate && kstatus==1){ // 4th digit of utf-16
00080             unibuf[0] = c;
00081             kstatus = 2;
00082         } else if(estate && c=='u'){
00083             kstatus = 1;
00084         } else if(estate && !c=='u'){
00085             drawc(c);
00086             kstatus = 0;
00087             estate = false;
00088         } else if(c=='\\'){ // escape code
00089             estate = true;
00090         } else { // 4x8font
00091             codes[i] = c;
00092             i++;
00093             if(c=='!') em = true;
00094         }
00095     }
00096     
00097     codes[i]= '\0';
00098     
00099     return em;
00100 }
00101 
00102 void drawString(const unsigned char *s){
00103     unsigned char c;
00104     while((c = *s++) != '\0') drawc(c);
00105 }
00106 
00107 int connect(){
00108     char localip[20];
00109     
00110     wifly.init(); //Use DHCP
00111     while (!wifly.connect());
00112     
00113 #ifdef _DEBUG
00114     pc.printf("ip %s\r\n", wifly.getIPAddress());
00115 #endif
00116 
00117     sprintf(localip, "ip %s", wifly.getIPAddress());
00118     drawString((unsigned char*)localip); newline();
00119         
00120     return 0;
00121 }
00122 
00123 void parseBuffer () {
00124     led2 = 1;
00125     
00126     // parse buffer    
00127     char* c = strtok(buf, "\"");
00128          while(c!=NULL){
00129             char* c = strtok(NULL, "\"");
00130             char key[4];
00131             sprintf(key, "%.4s", c);
00132             if(strcmp(key, "text")==0){
00133                 c = strtok(NULL, "\"");
00134                 c = strtok(NULL, "\"");
00135 #ifdef _DEBUG
00136                 pc.printf("%s\r\n", c);
00137 #endif
00138                 bool emergency = parseCode((unsigned char*)c);
00139                 if(emergency) color(0xf800); else color(0x001f);
00140                 cls();
00141                 color(0xffff);
00142                 drawString(codes); newline();
00143                 if(emergency) bmp(0,80,1); else bmp(0,80,0); // comment out if you don't use on-memory images
00144                 break;
00145             }
00146         }
00147     
00148     led2 = 0;
00149 }
00150 
00151 int main() {
00152     int r;
00153     char* msg;
00154     led1 = 0;
00155     
00156     PHY_PowerDown();
00157     Peripheral_PowerDown(0x7D7F6FE1);    
00158 
00159 #ifdef _DEBUG
00160     pc.baud(9600);
00161 #endif
00162     
00163     s_Lcdinit();
00164     
00165     color(0x001f);
00166     cls();
00167     color(0xffff);
00168 
00169     msg = "connect";
00170     drawString((unsigned char*)msg); newline();
00171     r = connect();
00172     if(r<0){
00173         NVIC_SystemReset();
00174     }
00175     
00176     wait_ms(1000);
00177     
00178     msg = "start twitter";
00179     drawString((unsigned char*)msg); newline();
00180 
00181     http.basicAuth(HTTP_USER, HTTP_PASS);
00182 
00183     wait_ms(1000);
00184     
00185     while(1) {
00186         led1 = !led1; wait_ms(500); led1 = !led1; // blink
00187         
00188         int ret = http.get(HTTP_URI, buf, 351); // out put only 40 zenkaku characters
00189         if(!ret){
00190             parseBuffer();
00191         }else{
00192             sprintf((char*)codes, "Error - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
00193             color(0x001f);
00194             cls();
00195             color(0xffff);
00196             drawString(codes); newline();
00197             if(ret==8){
00198                 drawString("disconnect..."); newline();
00199                 wifly.disconnect();
00200                 wifly.init();
00201                 drawString("reconnect..."); newline();
00202                 while (!wifly.connect());
00203                 drawString("done"); newline();
00204             }
00205         }
00206         
00207 #ifdef _DEBUG
00208         if(!ret){
00209             pc.printf("Page fetched successfully - read %d characters\r\n", strlen(buf));
00210             pc.printf("Result: %s\r\n", buf);
00211         }else{
00212             pc.printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
00213         }
00214 #endif
00215         wait(INTERVAL_SEC);
00216    }
00217 }