iX / Mbed 2 deprecated Working_Client_Code

Dependencies:   mbed

Fork of SERVER_WIFI_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 int main()
00113 {
00114     int range=3456;
00115 
00116     buffer=(char *)calloc(BUFF_SIZE,1);
00117     reset=0;
00118 
00119     pc.baud(115200);
00120     dev.baud(115200);
00121     pc.attach(&pc_recv, Serial::RxIrq);
00122     dev.attach(&dev_recv, Serial::RxIrq);
00123     pc.printf("Start up 3\n\r");
00124     wait(1.5);
00125     reset=1;
00126     char * resp=NULL;
00127     pc.printf("Here \n\r");
00128     while(1) {
00129         if (ready) {
00130             ready=0;
00131             bufferPnt=0;
00132             INFO("[%d]",state);
00133             switch (state) {
00134                 case 0: {
00135                     resp=OKResponse(buffer,"WIFI GOT IP");
00136                     if (resp!=NULL) {
00137                         wait(1);
00138                         dev.printf("AT\r\n");
00139                         state++;
00140                     }
00141                     break;
00142                 }
00143                 case 1:
00144                 case 2: {
00145                     resp=OKResponse(buffer,"OK");
00146                     if (resp!=NULL) {
00147                         dev.printf("AT\r\n");
00148                         state++;
00149                     }
00150                     break;
00151                 }
00152                 case 3: {
00153                     resp=OKResponse(buffer,"OK");
00154                     if (resp!=NULL) {
00155                         dev.printf("AT+RST\r\n");
00156                         state++;
00157                     }
00158 
00159                     break;
00160                 }
00161                 case 4: {
00162                     resp=OKResponse(buffer,"WIFI GOT IP");
00163                     if (resp!=NULL) {
00164                         dev.printf("AT+CWMODE=1\r\n");
00165                         state++;
00166                     }
00167 
00168                     break;
00169                 }
00170                 case 5: {
00171                     resp=OKResponse(buffer,"OK");
00172                     if (resp!=NULL) {
00173                         
00174                         dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
00175                         state++;
00176                     }
00177 
00178                     break;
00179                 }
00180                 case 6: {
00181                     resp=OKResponse(buffer,"OK");
00182                     if (resp!=NULL) {
00183                         wait(10);
00184                         dev.printf("AT+CIFSR\r\n");
00185                         state++;
00186                     }
00187 
00188                     break;
00189                 }
00190                 case 7: {
00191                     resp=OKResponse(buffer,"+CIFSR:STAIP,");
00192                     if (resp!=NULL) {
00193                         char *strt = strtok(buffer,"\"");
00194                         strcpy(ipAddress,strtok(NULL,"\""));
00195                         strtok(NULL,"\"");
00196                          strcpy(macAddress,strtok(NULL,"\""));
00197                         INFO("mac Address = %s", macAddress);
00198                         INFO("IP Address = %s", ipAddress);
00199                           dev.printf("AT+CIPMUX=1\r\n");
00200                         state++;
00201                     }
00202 
00203                     break;
00204                 }
00205                 case 8: {
00206                     resp=OKResponse(buffer,"OK");
00207                     if (resp!=NULL) {
00208                        INFO("Ready");
00209                        //dev.printf("AT+CIPSERVER= 1,6060\r\n"); 
00210                        dev.printf("AT+CIPSTART=0,\"TCP\",\"192.168.1.8\",6060\r\n");
00211                        state++;
00212                     }
00213 
00214                      break;
00215                 }
00216                 case 9: { resp=OKResponse(buffer,"OK");
00217                     if (resp!=NULL) {
00218                        INFO("Ready");
00219                        wait(2);
00220                        char buf[32];
00221                        sprintf(buf,"Range=%04d\r\n",range);
00222                         //dev.printf("AT+CIPSERVER= 1,6060"); 
00223                        dev.printf("AT+CIPSEND=0,%d\r\n",strlen(buf));  //10 = length of string being sent
00224                        wait(2);
00225                         dev.printf("%s",buf);  //10 = length of string being sent
00226                        range=(range++)%6000;
00227             
00228                        state=9;
00229                     }
00230                     pc.printf("%s\n\r",buffer);
00231 
00232                      break;
00233 
00234             }
00235                         case 10: { resp=OKResponse(buffer,">");
00236                     if (resp!=NULL) {
00237                        INFO("Sending");
00238                         //dev.printf("AT+CIPSERVER= 1,6060"); 
00239                         // code to get range and format to max 4 digits to be added
00240                        dev.printf("Range=%04d\r\n",range);  //10 = length of string being sent
00241                        range=(range+500)%6000;
00242                        wait(2);
00243                        state=9;
00244                     }
00245                   //  pc.printf("%s\n\r",buffer);
00246 
00247                      break;
00248 
00249             }    
00250             
00251                 
00252 
00253           
00254         }            
00255         }
00256         __WFI();
00257     }
00258 }