Client

Dependencies:   mbed

Fork of SERVER_WIFI_CODE by iX

main.cpp

Committer:
epgmdm
Date:
2016-06-08
Revision:
0:7176e20dbdf6
Child:
1:e2385bba1ab1

File content as of revision 0:7176e20dbdf6:

#include "mbed.h"

#define DEBUG
#define INFOMESSAGES
#define WARNMESSAGES
#define ERRMESSAGES

#define FUNCNAME "MAIN"

#ifdef DEBUG
#define DBG(x, ...) pc.printf("    ["FUNCNAME" : DBG] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__);
#else
#define DBG(x, ...)
#endif

#ifdef ERRMESSAGES
#define ERR(x, ...) pc.printf(" ["FUNCNAME" : ERR] "x"\r\n", ##__VA_ARGS__);
#else
#define ERR(x, ...)
#endif

#ifdef WARNMESSAGES
#define WARN(x, ...) printf("["FUNCNAME" : WARN] "x"\r\n", ##__VA_ARGS__);
#else
#define WARN(x, ...)
#endif

#ifdef INFOMESSAGES
#define INFO(x, ...) pc->printf("["FUNCNAME" : INFO] "x"\r\n", ##__VA_ARGS__);
#else
#define INFO(x, ...)
#endif

#define BUFF_SIZE 2048
RawSerial  pc(USBTX, USBRX);
//RawSerial  dev(D1, D0);
RawSerial  dev(p28, p27); //tx,rx
DigitalOut led1(LED1);
DigitalOut led4(LED4);
DigitalOut reset(p26,1);
volatile int state=0;
volatile int ready=0;
char *buffer;
unsigned int bufferPnt=0;

void dev_recv()
{
    char c;

    int count = 0;
    led1 = !led1;
    if(bufferPnt==0) {
        memset(buffer,0,BUFF_SIZE);
    }
    while(dev.readable()) {
        c = (char)dev.getc();
        pc.putc(c);
        buffer[bufferPnt]=c;
        bufferPnt++;
        if (bufferPnt>1024){
            ready=1;
            }
        if (c==0x0a) {
            if (bufferPnt>1) {
                if (buffer[bufferPnt -2]==0x0d) {
                    ready=1;
                    break;
                }
            }
        }
        if (!dev.readable()){
          wait(0.001);
        }
    }
}

void pc_recv()
{
    char c;
    led4 = !led4;
    while(pc.readable()) {
        c=(char)pc.getc();
        dev.putc(c);
        pc.putc(c);

    }
}

char * OKResponse(char *test, const char *pattern)
{
    char *p= strstr(test,pattern);
    if (p==NULL) {
        //   DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p);
        return NULL;
    } else {
        //  DBG("YAY Test=<%s> Patter=<%s>  [p=%s]",test,pattern,p);
    }
    return p;
}
int main()
{
    buffer=(char *)calloc(BUFF_SIZE,1);
    reset=0;

    pc.baud(115200);
    dev.baud(115200);
    pc.attach(&pc_recv, Serial::RxIrq);
    dev.attach(&dev_recv, Serial::RxIrq);
    pc.printf("Start up\n\r");
    wait(1.5);
    reset=1;
    char * resp=NULL;
    pc.printf("Here \n\r");
    while(1) {
        if (ready) {
            ready=0;
            bufferPnt=0;
            pc.printf("%d]%s\n\r",state,buffer);
            switch (state) {
                case 0: {
                    resp=OKResponse(buffer,"WIFI GOT IP");
                    if (resp!=NULL) {
                        DBG(">>>>>>HERE");
                        wait(1);
                        dev.printf("AT\r\n");
                        state++;
                    }
                    break;
                }
                case 1:
                case 2: {
                    resp=OKResponse(buffer,"OK");
                    if (resp!=NULL) {
                        DBG(">>>>>>HERE 2");
                        dev.printf("AT\r\n");
                        state++;
                    }
                    break;
                }
                case 3: {
                    resp=OKResponse(buffer,"OK");
                    if (resp!=NULL) {
                        DBG(">>>>>>HERE 3");
                        dev.printf("AT+RST\r\n");
                        state++;
                    }

                    break;
                }
                case 4: {
                    resp=OKResponse(buffer,"WIFI GOT IP");
                    if (resp!=NULL) {
                        DBG(">>>>>>HERE 4");
                        dev.printf("AT+CWMODE=1\r\n");
                        state++;
                    }

                    break;
                }
                case 5: {
                    resp=OKResponse(buffer,"OK");
                    if (resp!=NULL) {
                        DBG(">>>>>>HERE 5");
                        dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
                        state++;
                    }

                    break;
                }
                case 6: {
                    resp=OKResponse(buffer,"OK");
                    if (resp!=NULL) {
                        DBG(">>>>>>HERE 5");
                        wait(1);
                        dev.printf("AT+CIFSR\r\n");
                        state++;
                    }

                    break;
                }
                case 7: {
                    resp=OKResponse(buffer,"OK");
                    if (resp!=NULL) {
                        DBG(">>>>>>HERE 5");
                        wait(1);
                        dev.printf("AT+CIFSR\r\n");
                        state++;
                    }

                    break;
                }


            }
        }
        __WFI();
    }
}