working code

Dependencies:   mbed

Fork of Working_Client_Code by iX

main.cpp

Committer:
jcochra
Date:
2016-06-10
Revision:
7:1119c6781d17
Parent:
6:1c35ca876e09
Child:
8:f277f86f75bb

File content as of revision 7:1119c6781d17:

#include "mbed.h"

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

#define FUNCNAME "MAIN"

#ifdef NoDEBUG
#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(p9, p10); //tx,rx
DigitalOut led1(LED1);
DigitalOut led4(LED4);
DigitalOut reset(p21,1);
volatile int state=0;
volatile int ready=0;

char ipAddress[20];
char macAddress[32];
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();
#ifdef DEBUG
       pc.putc(c);
#endif
        buffer[bufferPnt]=c;
        bufferPnt++;
        if (bufferPnt>1024) {
            ready=1;
        }
       if ((c==0x0a)||(c==0x0d)){
             ready=1;
             }else
        if (c==0x0a) {
            if (bufferPnt>1) {
                if (buffer[bufferPnt -2]==0x0d) {
                    ready=1;
                    break;
                }
            }
        }
        if (!dev.readable()) {
            wait_us(100);
        }
    }
}

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

    }
}

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;
}


//A FUNCTION TO FIND THE RANGE-----------------------------------------------------------

int obstruction_finder()
{
    AnalogIn ranger(p15);
    float r;
    float d;
    int obstruction;
    r = ranger;
    d = r / 0.00084;
    if (d < 20) {
        obstruction = 1;
    } else {
        obstruction = 0;
    }
    return obstruction;
}

//A FUNCTION TO FIND THE RANGE--------------------------------------------------------------

int main()
{
    int range=3456;

    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 3\n\r");
    wait(1.5);
    reset=1;
    char * resp=NULL;
    pc.printf("Here \n\r");
    while(1) {
        if (ready) {
            ready=0;
            bufferPnt=0;
            INFO("[%d]",state);
            switch (state) {
                case 0: {
                    resp=OKResponse(buffer,"WIFI GOT IP");
                    if (resp!=NULL) {
                        wait(1);
                        dev.printf("AT\r\n");
                        state++;
                    }
                    break;
                }
                case 1:
                case 2: {
                    resp=OKResponse(buffer,"OK");
                    if (resp!=NULL) {
                        dev.printf("AT\r\n");
                        state++;
                    }
                    break;
                }
                case 3: {
                    resp=OKResponse(buffer,"OK");
                    if (resp!=NULL) {
                        dev.printf("AT+RST\r\n");
                        state++;
                    }

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

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

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

                    break;
                }
                case 7: {
                    resp=OKResponse(buffer,"+CIFSR:STAIP,");
                    if (resp!=NULL) {
                        char *strt = strtok(buffer,"\"");
                        strcpy(ipAddress,strtok(NULL,"\""));
                        strtok(NULL,"\"");
                         strcpy(macAddress,strtok(NULL,"\""));
                        INFO("mac Address = %s", macAddress);
                        INFO("IP Address = %s", ipAddress);
                          dev.printf("AT+CIPMUX=1\r\n");
                        state++;
                    }

                    break;
                }
                case 8: {
                    resp=OKResponse(buffer,"OK");
                    if (resp!=NULL) {
                       INFO("Ready");
                       //dev.printf("AT+CIPSERVER= 1,6060\r\n"); 
                       dev.printf("AT+CIPSTART=0,\"TCP\",\"192.168.1.8\",6060\r\n");
                       state++;
                    }

                     break;
                }
                case 9: { resp=OKResponse(buffer,"OK");
                    if (resp!=NULL) {
                       INFO("Ready");
                       wait(2);
                       char buf[32];
                       range = obstruction_finder();
                       sprintf(buf,"Range=%04d\r\n",range);
                        //dev.printf("AT+CIPSERVER= 1,6060"); 
                       dev.printf("AT+CIPSEND=0,%d\r\n",strlen(buf));  //10 = length of string being sent
                       wait(2);
                        dev.printf("%s",buf);  //10 = length of string being sent
                     //range=(range++)%6000;
            
                       state=9;
                    }
                    pc.printf("%s\n\r",buffer);

                     break;

            }            
                

          
        }            
        }
        __WFI();
    }
}