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

/media/uploads/kazushi2008/_scaled_twittermbed_scene.jpg /media/uploads/kazushi2008/_scaled_twittermbed.jpg

Please see at http://kazushi-lab.c.fun.ac.jp/pukiwiki/index.php?TwitterMbed

main.cpp

Committer:
kazushi2008
Date:
2013-02-25
Revision:
9:86dd65b94dca
Parent:
8:22eb078985b1
Child:
10:94f45d37174f

File content as of revision 9:86dd65b94dca:

// Twitter Mbed with Gain Span WiFi
// 
// coded by Kazushi Mukaiyama (http://www.kazushi.info/)
// refer to gs fan's notebook (http://mbed.org/users/gsfan/notebook/gainspan_wifi/)
// refer to GingaX's notebook (http://www31.atwiki.jp/gingax/pages/63.html)

#include "mbed.h"
#include "PowerControl/PowerControl.h"
#include "PowerControl/EthernetPowerControl.h"
#include "GSwifi.h"
#include "sjis_utf16.h"
#include "s_Lcd.h"

#define countof(x) ( sizeof(x) / sizeof(x[0]) )

#define SECURE GSSEC_WPA_PSK
#define SSID "WIFISSID"
#define PASS "abcdef1234"

#define HTTP_HOST "api.supertweet.net" // SuperTweet.net
//#define HTTP_URI "/1.1/statuses/show.json?id=281639966776369152"
#define HTTP_URI "/1.1/statuses/user_timeline.json?screen_name=pla3c&count=1"
#define HTTP_USER "user"
#define HTTP_PASS "pass"
#define INTERVAL_SEC 60.0

//#define _DEBUG

GSwifi gs(p13, p14); // TX, RX (no flow control)
DigitalOut led1(LED1), led2(LED2);
#ifdef _DEBUG
    Serial pc(USBTX, USBRX);
#endif

unsigned char kstatus = 0;
bool estate = false;
unsigned char unibuf[4];
unsigned char codes[512];
bool emergency = false;

unsigned short convert(unsigned short utf){
    unsigned short sjis = NULL; 
    for(int i=0; i<countof(sjis2utf16)/2; i++){
        if(utf==sjis2utf16[i*2+1]){
            sjis = sjis2utf16[i*2];
            break;
        }
    }
    return sjis;
}

void parseCode(const unsigned char* s){
    unsigned int i = 0;
    unsigned char c;
    
    emergency = false;
    while((c = *s++) != '\0') {
        if(estate && kstatus==4){ // 1st digit of utf-16
            unibuf[3] = c;
            char *endptr;
            unsigned short code = convert(strtol((char*)unibuf, &endptr, 16));
            codes[i] = code>>8 & 0xFF;
            i++;
            codes[i] = code & 0xFF;
            i++;
            if(code==0x8149) emergency = true;
    #ifdef _DEBUG
            pc.printf("%s, %x\r\n", unibuf, code);
    #endif
            kstatus = 0;
            estate = false;
        } else if(estate && kstatus==3){ // 2nd digit of utf-16
            unibuf[2] = c;
            kstatus = 4;
        } else if(estate && kstatus==2){ // 3rd digit of utf-16
            unibuf[1] = c;
            kstatus = 3;
        } else if(estate && kstatus==1){ // 4th digit of utf-16
            unibuf[0] = c;
            kstatus = 2;
        } else if(estate && c=='u'){
            kstatus = 1;
        } else if(estate && !c=='u'){
            drawc(c);
            kstatus = 0;
            estate = false;
        } else if(c=='\\'){ // escape code
            estate = true;
        } else { // 4x8font
            codes[i] = c;
            i++;
            if(c=='!') emergency = true;
        }
    }
    
    codes[i]= '\0';
}

void drawString(const unsigned char *s){
    unsigned char c;
    while((c = *s++) != '\0') drawc(c);
}

int connect(){
    IpAddr ipaddr, netmask, gateway, nameserver;
    
    //gs.command("AT&F", GSRES_NORMAL);
    
    if (gs.connect(SECURE, SSID, PASS, 1, 5)) {
        drawString("WiFi connection error"); newline();
#ifdef _DEBUG
        pc.printf("WiFi connection error\r\n");
#endif
        return -1;
    }
    
    gs.getAddress(ipaddr, netmask, gateway, nameserver);
    if(nameserver.isNull()) {
        nameserver = IpAddr(192,168,1,1);
        gs.setAddress(ipaddr, netmask, gateway, nameserver);
        gs.getAddress(ipaddr, netmask, gateway, nameserver);
    }
#ifdef _DEBUG
    pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
    pc.printf("gw %d.%d.%d.%d\r\n", gateway[0], gateway[1], gateway[2], gateway[3]);
    pc.printf("ns %d.%d.%d.%d\r\n", nameserver[0], nameserver[1], nameserver[2], nameserver[3]);
#endif
    char localip[20];
    sprintf(localip, "ip %d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
    drawString((unsigned char*)localip); newline();
    sprintf(localip, "gw %d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]);
    drawString((unsigned char*)localip); newline();
    sprintf(localip, "ns %d.%d.%d.%d", nameserver[0], nameserver[1], nameserver[2], nameserver[3]);
    drawString((unsigned char*)localip); newline();
    
    delete [] localip;
    
    return 0;
}

void onGsReceive (int cid, int len) {
    int i;
    char buf[GS_DATA_SIZE + 1];
    
    led2 = 1;
    i = gs.recv(cid, buf, len);
    buf[i] = 0;
    
    // parse buffer    
    char* c = strtok(buf, "\r\n");
    if(strcmp(c, "200 OK")==0){
        while(c!=NULL){
            c = strtok(NULL, "\"");
            char key[4];
            sprintf(key, "%.4s", c);
            if(strcmp(key, "text")==0){
                c = strtok(NULL, "\"");
                c = strtok(NULL, "\"");
#ifdef _DEBUG
                pc.printf("%s\r\n", c);
#endif
                //c = strtok(c, "@");
                parseCode((unsigned char*)c);
                if(emergency) color(0xf800); else color(0x001f);
                cls();
                color(0xffff);
                drawString(codes); newline();
                //c = strtok(NULL, "@");
                //drawString((unsigned char*)c); newline();
                break;
            }
        }
    }
    
    led2 = 0;
    
    delete [] buf;
}

int main() {
    int r;
    char* msg;
    Host host;
    led1 = 0;
    
    PHY_PowerDown();
    Peripheral_PowerDown(0x7D7F6FE1);    
    gs.setBaud(115200);
#ifdef _DEBUG
    pc.baud(115200);
#endif
    
    s_Lcdinit();
    
    color(0x001f);
    cls();
    color(0xffff);

    msg = "connect";
    drawString((unsigned char*)msg); newline();
    r = connect();
    if(r<0){
        NVIC_SystemReset();
    }
    
    msg = "start twitter";
    drawString((unsigned char*)msg); newline();
    
    host.setName(HTTP_HOST);

    while(1) {
        led1 = !led1; wait_ms(500); led1 = !led1; // blink
        
        r = gs.httpGet(host, HTTP_URI, HTTP_USER, HTTP_PASS, 0, &onGsReceive);
        gs.poll();       
        if (!gs.isConnected(r)) {
            color(0x001f);
            cls();
            msg = "reconnect";
            drawString((unsigned char*)msg); newline();
            wait_ms(5);
            r = gs.disconnect();
            r = connect();
        }

        wait(INTERVAL_SEC);
   }
   
}