working code

Dependencies:   mbed

Fork of Working_Client_Code by iX

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define DEBUG
00004 #define INFOMESSAGES
00005 #define WARNMESSAGES
00006 #define ERRMESSAGES
00007 
00008 #define FUNCNAME "MAIN"
00009 
00010 #ifdef NoDEBUG
00011 #define DBG(x, ...) pc.printf("    ["FUNCNAME" : DBG] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__);
00012 #else
00013 #define DBG(x, ...)
00014 #endif
00015 
00016 #ifdef ERRMESSAGES
00017 #define ERR(x, ...) pc.printf(" ["FUNCNAME" : ERR] "x"\r\n", ##__VA_ARGS__);
00018 #else
00019 #define ERR(x, ...)
00020 #endif
00021 
00022 #ifdef WARNMESSAGES
00023 #define WARN(x, ...) printf("["FUNCNAME" : WARN] "x"\r\n", ##__VA_ARGS__);
00024 #else
00025 #define WARN(x, ...)
00026 #endif
00027 
00028 #ifdef INFOMESSAGES
00029 #define INFO(x, ...) pc.printf("["FUNCNAME" : INFO] "x"\r\n", ##__VA_ARGS__);
00030 #else
00031 #define INFO(x, ...)
00032 #endif
00033 
00034 #define BUFF_SIZE 2048
00035 RawSerial  pc(USBTX, USBRX);
00036 //RawSerial  dev(D1, D0);
00037 RawSerial  dev(p9, p10); //tx,rx
00038 DigitalOut led1(LED1);
00039 DigitalOut led4(LED4);
00040 DigitalOut reset(p21,1);
00041 volatile int state=0;
00042 volatile int ready=0;
00043 
00044 char ipAddress[20];
00045 char macAddress[32];
00046 char *buffer;
00047 unsigned int bufferPnt=0;
00048 
00049 void dev_recv()
00050 {
00051     char c;
00052 
00053     int count = 0;
00054     led1 = !led1;
00055     if(bufferPnt==0) {
00056         memset(buffer,0,BUFF_SIZE);
00057     }
00058     while(dev.readable()) {
00059         c = (char)dev.getc();
00060 #ifdef DEBUG
00061        pc.putc(c);
00062 #endif
00063         buffer[bufferPnt]=c;
00064         bufferPnt++;
00065         if (bufferPnt>1024) {
00066             ready=1;
00067         }
00068        if ((c==0x0a)||(c==0x0d)){
00069              ready=1;
00070              }else
00071         if (c==0x0a) {
00072             if (bufferPnt>1) {
00073                 if (buffer[bufferPnt -2]==0x0d) {
00074                     ready=1;
00075                     break;
00076                 }
00077             }
00078         }
00079         if (!dev.readable()) {
00080             wait_us(100);
00081         }
00082     }
00083 }
00084 
00085 void pc_recv()
00086 {
00087     char c;
00088     led4 = !led4;
00089     while(pc.readable()) {
00090         c=(char)pc.getc();
00091         dev.putc(c);
00092         pc.putc(c);
00093         if(c==13) {
00094             dev.putc(10);
00095             pc.putc(10);
00096             }
00097 
00098     }
00099 }
00100 
00101 char * OKResponse(char *test, const char *pattern)
00102 {
00103     char *p= strstr(test,pattern);
00104     if (p==NULL) {
00105         //   DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p);
00106         return NULL;
00107     } else {
00108         //  DBG("YAY Test=<%s> Patter=<%s>  [p=%s]",test,pattern,p);
00109     }
00110     return p;
00111 }
00112 
00113 
00114 //A FUNCTION TO FIND THE RANGE-----------------------------------------------------------
00115 
00116 int obstruction_finder()
00117 {
00118     AnalogIn ranger(p15);
00119     float r;
00120     float d;
00121     int obstruction;
00122     r = ranger;
00123     d = r / 0.00084;
00124     if (d < 20) {
00125         obstruction = 1;
00126     } else {
00127         obstruction = 0;
00128     }
00129     return obstruction;
00130 }
00131 
00132 //A FUNCTION TO FIND THE RANGE--------------------------------------------------------------
00133 
00134 int main()
00135 {
00136     int range=3456;
00137 
00138     buffer=(char *)calloc(BUFF_SIZE,1);
00139     reset=0;
00140 
00141     pc.baud(115200);
00142     dev.baud(115200);
00143     pc.attach(&pc_recv, Serial::RxIrq);
00144     dev.attach(&dev_recv, Serial::RxIrq);
00145     pc.printf("Start up 3\n\r");
00146     wait(1.5);
00147     reset=1;
00148     char * resp=NULL;
00149     pc.printf("Here \n\r");
00150     while(1) {
00151         if (ready) {
00152             ready=0;
00153             bufferPnt=0;
00154             INFO("[%d]",state);
00155             switch (state) {
00156                 case 0: {
00157                     resp=OKResponse(buffer,"WIFI GOT IP");
00158                     if (resp!=NULL) {
00159                         wait(1);
00160                         dev.printf("AT\r\n");
00161                         state++;
00162                     }
00163                     break;
00164                 }
00165                 case 1:
00166                 case 2: {
00167                     resp=OKResponse(buffer,"OK");
00168                     if (resp!=NULL) {
00169                         dev.printf("AT\r\n");
00170                         state++;
00171                     }
00172                     break;
00173                 }
00174                 case 3: {
00175                     resp=OKResponse(buffer,"OK");
00176                     if (resp!=NULL) {
00177                         dev.printf("AT+RST\r\n");
00178                         state++;
00179                     }
00180 
00181                     break;
00182                 }
00183                 case 4: {
00184                     resp=OKResponse(buffer,"WIFI GOT IP");
00185                     if (resp!=NULL) {
00186                         dev.printf("AT+CWMODE=1\r\n");
00187                         state++;
00188                     }
00189 
00190                     break;
00191                 }
00192                 case 5: {
00193                     resp=OKResponse(buffer,"OK");
00194                     if (resp!=NULL) {
00195                         
00196                         dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
00197                         state++;
00198                     }
00199 
00200                     break;
00201                 }
00202                 case 6: {
00203                     resp=OKResponse(buffer,"OK");
00204                     if (resp!=NULL) {
00205                         wait(10);
00206                         dev.printf("AT+CIFSR\r\n");
00207                         state++;
00208                     }
00209 
00210                     break;
00211                 }
00212                 case 7: {
00213                     resp=OKResponse(buffer,"+CIFSR:STAIP,");
00214                     if (resp!=NULL) {
00215                         char *strt = strtok(buffer,"\"");
00216                         strcpy(ipAddress,strtok(NULL,"\""));
00217                         strtok(NULL,"\"");
00218                          strcpy(macAddress,strtok(NULL,"\""));
00219                         INFO("mac Address = %s", macAddress);
00220                         INFO("IP Address = %s", ipAddress);
00221                           dev.printf("AT+CIPMUX=1\r\n");
00222                         state++;
00223                     }
00224 
00225                     break;
00226                 }
00227                 case 8: {
00228                     resp=OKResponse(buffer,"OK");
00229                     if (resp!=NULL) {
00230                        INFO("Ready");
00231                        //dev.printf("AT+CIPSERVER= 1,6060\r\n"); 
00232                        dev.printf("AT+CIPSTART=0,\"TCP\",\"192.168.1.8\",6060\r\n");
00233                        state++;
00234                     }
00235 
00236                      break;
00237                 }
00238                 case 9: { resp=OKResponse(buffer,"OK");
00239                     if (resp!=NULL) {
00240                        INFO("Ready");
00241                        wait(0.2);
00242                        char buf[32];
00243                        range = obstruction_finder();
00244                        sprintf(buf,"OB=%01d\r\n",range);
00245                         //dev.printf("AT+CIPSERVER= 1,6060"); 
00246                        dev.printf("AT+CIPSEND=0,%d\r\n",strlen(buf));  //10 = length of string being sent
00247                        wait(0.2);
00248                         dev.printf("%s",buf);  //10 = length of string being sent
00249                      //range=(range++)%6000;
00250             
00251                        state=9;
00252                     }
00253                     pc.printf("%s\n\r",buffer);
00254 
00255                      break;
00256 
00257             }            
00258                 
00259 
00260           
00261         }            
00262         }
00263         __WFI();
00264     }
00265 }