Jack Hansdampf / ESP8266WebserverGSOE_F401
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266Webserver.cpp Source File

ESP8266Webserver.cpp

00001 #include "ESP8266Webserver.h"
00002 //#include "string"
00003 
00004 ESP8266Webserver::ESP8266Webserver()
00005 {
00006     bool erg;
00007     //HAL_Delay(1000);
00008     DigitalOut ch_pd(CH_PD);
00009     DigitalOut rst(RST); 
00010     ch_pd=1;
00011     rst=0;
00012     HAL_Delay(100);
00013     rst=1;
00014     hs_count=0;
00015     clientIdx=0;
00016     if (pDebug) printf("\nATCmdParser with ESP8266 example");
00017     port=Port;
00018     
00019     dbg=pDebug;
00020     _serial = new BufferedSerial(tx,rx, ESP8266_DEFAULT_BAUD_RATE);
00021     _parser = new ATCmdParser(_serial, "\r\n",1000);
00022     _parser->debug_on(true);
00023 
00024     _parser->set_timeout(2000);
00025     
00026 //AT
00027     if (pDebug) printf("\nATCmdParser: AT\r\n");
00028     _parser->send("AT");
00029     
00030     if (_parser->recv("OK")) {
00031         if (pDebug) printf("\nAntw.: OK\r\n");
00032     } else {
00033         printf("\nATCmdParser: AT OK failed\r\n");
00034     }
00035 
00036     if (station==true) 
00037     { 
00038         listAPs();
00039         printf("%s",ipadr);
00040     }
00041     else {
00042         _parser->send("ATE0");
00043         strcpy(ipadr,ip);
00044     
00045         if (pDebug) printf("\nATCmdParser: //AT+CWMODE=2 \r\n");
00046         
00047         _parser->send("AT+CWMODE=2");
00048         HAL_Delay(1000);
00049         
00050         if (_parser->recv("OK")) {
00051             if (pDebug) printf("\nAntw.: OK\r\n");
00052         } else {
00053             printf("\nATCmdParser: AT+CWMODE=2 OK failed\r\n");
00054         }
00055 
00056 //AT+CWMODE? 
00057     if (pDebug) printf("\nATCmdParser: //AT+CWMODE? \r\n");
00058 
00059     _parser->send("AT+CWMODE?");
00060     
00061     if (_parser->recv("OK")) {
00062         if (pDebug) printf("\nAntw.: OK\r\n");
00063     } else {
00064         printf("\nATCmdParser: AT+CWMODE? OK failed\r\n");
00065     }
00066     
00067     
00068 //AT+CIPAP="192.168.5.1","192.168.5.1","255.255.255.0"
00069         if (pDebug) printf("\nATCmdParser: //AT+CIPAP=\"%s\",\"%s\",\"%s\" \r\n",ipad.c_str(),ipad.c_str(),netzmaske);
00070         do
00071         {
00072             _parser->send("AT+CIPAP=\"%s\",\"%s\",\"%s\"",ipad.c_str(),ipad.c_str(),netzmaske);
00073             erg=_parser->recv("OK");
00074             if (erg==true) {
00075                 if (pDebug) printf("\nAntw.: OK\r\n");
00076             } else {
00077                 printf("\nATCmdParser: AT+CIPAP OK failed\r\n");
00078                 HAL_Delay(1000);
00079             }           
00080         }
00081         while (erg==false);
00082         
00083      
00084     
00085 //AT+CIPMUX=1
00086     if (pDebug) printf("\nATCmdParser: //AT+CIPMUX=1");
00087     _parser->send("AT+CIPMUX=1");
00088     
00089     if (_parser->recv("OK")) {
00090         if (pDebug) printf("\nAntw.: OK\r\n");
00091     } else {
00092         printf("\nATCmdParser: AT+CIPMUX=1 OK failed\r\n");
00093     }
00094     }
00095 }
00096 
00097 int ESP8266Webserver::on(const char* handlestring,Callback< void()> func)
00098 {
00099   if (hs_count>9) return -1;
00100   else
00101   {
00102     strcpy(hs[hs_count],handlestring); 
00103     if (dbg) printf("\r\n\r\n%s, %s\r\n",handlestring,hs[hs_count]);
00104     cbs[hs_count]=func;
00105     hs_count++;
00106     return 0;
00107   }
00108     
00109 }
00110 
00111 int ESP8266Webserver::begin(void)
00112 {
00113 //AT+CIPSERVER=1,80
00114     if (dbg) printf("\nATCmdParser: //AT+CIPSERVER=1,%d",Port);
00115     _parser->send("AT+CIPSERVER=1,%d",Port);
00116     if (_parser->recv("OK")) {
00117         if (dbg) printf("\nAntw.: OK\r\n");
00118         return 0;
00119     } else {
00120         printf("\nATCmdParser: AT+CIPSERVER=1 OK failed\r\n");
00121         return -1;
00122     }
00123 }
00124 
00125 //durchsucht recbuf nach suchstring
00126 bool ESP8266Webserver::beinhaltet(char* suchstring)
00127 {
00128     char* referer;
00129     char* fundort;
00130     char* favicon;
00131     char* post;
00132     post=strstr(recbuf,"POST");
00133     favicon=strstr(recbuf,"favicon");
00134     if (favicon!=NULL) return true;
00135     referer=strstr(recbuf,"Referer");
00136     fundort=strstr(recbuf, suchstring);
00137     if (strstr(recbuf,"ERROR")==NULL)
00138     if( fundort != NULL&&(fundort<referer||referer==NULL)) return true;
00139     if (fundort != NULL&&post!=NULL) return true;
00140     return false;
00141 }
00142 
00143 int ESP8266Webserver::handleClient(void)
00144 {
00145     char favicon[]="favicon.ico";
00146     const char ico[1150]={
00147 0x00,0x00,0x01,0x00,0x01,0x00,0x10,0x10,0x00,0x00,0x01,0x00,0x20,0x00,0x68,0x04,0x00,0x00,
00148 0x16,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x01,0x00,
00149 0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
00150 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00151 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,
00152 0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00153 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00154 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,
00155 0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00156 0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00157 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00158 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00159 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00160 0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00161 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,
00162 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,
00163 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,
00164 0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,
00165 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,
00166 0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00167 0xFF,0xFF,0x00,0x00,0x00,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,
00168 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,
00169 0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,
00170 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,
00171 0x91,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,
00172 0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00173 0xFF,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,
00174 0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,
00175 0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00176 0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,
00177 0x00,0xFF,0x00,0x00,0x00,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,
00178 0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,
00179 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00180 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,
00181 0x91,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,
00182 0x07,0x03,0x91,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00183 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,
00184 0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00185 0xFF,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,0x00,0xFF,
00186 0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00187 0xFF,0xFF,0x00,0x00,0x00,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,
00188 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00189 0xFF,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,
00190 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,
00191 0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,
00192 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,
00193 0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFF,
00194 0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,
00195 0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x03,0x91,0xFF,
00196 0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,
00197 0xFF,0xFF,0x00,0x00,0x00,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,
00198 0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,
00199 0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,
00200 0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,
00201 0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,
00202 0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,
00203 0x91,0xFF,0x07,0x03,0x91,0xFF,0x07,0x03,0x91,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,
00204 0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,
00205 0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,
00206 0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,
00207 0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
00208 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
00209 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
00210 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
00211 
00212  };
00213     int value,value2;
00214     static int diag=0;
00215     if (_serial->readable())
00216         {   
00217             if (_parser->recv("+IPD,%d,%d:",&clientID[clientIdx],&value2)) 
00218             {
00219                 if (dbg) printf("\r\n >>> Client ID=%d, Anzahl=%d",clientID[clientIdx],value2);
00220                 _parser->read(recbuf, value2);
00221 
00222 
00223                 if (dbg) printf("\r\nBuf=\n%s\n\n",recbuf);
00224 
00225                 if (beinhaltet(favicon))
00226                 {
00227                     printf("\n\nfavicon request %d\n\n",diag);
00228                     diag++;
00229                     send(404,"image/x-icon","");
00230                 }
00231                 else
00232                 {
00233                     gefunden=false;
00234                     for (int i=0;i<hs_count&&!gefunden;i++)
00235                     {                    
00236                         if (beinhaltet(hs[i]))
00237                         {                       
00238                             (cbs[i])();                       
00239                             gefunden=true;
00240                         }
00241                     } 
00242                 }
00243                 if (dbg) for (int i=0;i<hs_count;i++)
00244                 {
00245                     printf("\r\n%s\r\n",hs[i]);
00246                 }               
00247                 if (dbg) printf("\r\n>>> Got\r\n");
00248             }          
00249         }
00250 
00251     return 0;
00252 }
00253 
00254 
00255 int ESP8266Webserver::send(int HTTPStatus,const char* Mimetype, string webseite)
00256 {
00257     return ESP8266Webserver::send(HTTPStatus,Mimetype, webseite.c_str());
00258 }
00259 
00260 int ESP8266Webserver::send(int HTTPStatus,const char* Mimetype, const char* webseite)
00261 {
00262     char hilf[40];
00263     char statustext[40];
00264     
00265     sprintf(statustext,"OK");
00266  
00267     switch (HTTPStatus)
00268     {   
00269     case 404: sprintf(statustext,"Not Found"); break;
00270     case 206: sprintf(statustext,"Partial Content"); break;
00271     case 401: sprintf(statustext,"Unauthorized"); break;
00272     case 403: sprintf(statustext,"Forbidden"); break;
00273     case 307:
00274     case 302: sprintf(statustext,"Moved Temporarily"); break;
00275     case 301: sprintf(statustext,"Moved Permanently"); break;
00276     case 304: sprintf(statustext,"Not Modified"); break;
00277     
00278     }
00279     
00280     
00281     //printf("\r\nHallo\r\n");
00282     Aufrufe++;
00283     switch (HTTPStatus)
00284     {
00285     case 206:
00286     case 200: sprintf(sendstring,"HTTP/1.1 %d %s\n",HTTPStatus,statustext); 
00287         strcat(sendstring,"Date: Wed, 23 Apr 2021 04:36:25 GMT\n");
00288         strcat(sendstring,"Connection: close\n");
00289         strcat(sendstring,"Content-Type: ");
00290         strcat(sendstring,Mimetype);
00291         strcat(sendstring,"\n");
00292         sprintf(hilf,"%d",strlen(webseite));
00293         strcat(sendstring,"Content-Length: ");
00294         strcat(sendstring,hilf);
00295         strcat(sendstring,"\n\n");
00296         strcat(sendstring,webseite);
00297         break;
00298        
00299     case 301:
00300     case 302: sprintf(sendstring,"HTTP/1.1 %d %s\n",HTTPStatus,statustext); 
00301         strcat(sendstring,"Date: Wed, 23 Apr 2021 04:36:25 GMT\n");
00302         strcat(sendstring,"Connection: close\n");
00303         strcat(sendstring,"Location: http://www.gsoe.de/");
00304         strcat(sendstring,"\n\n");
00305         break;   
00306         
00307     default: sprintf(sendstring,"HTTP/1.1 %d %s\n",HTTPStatus,statustext); 
00308         strcat(sendstring,"Date: Wed, 23 Apr 2021 04:36:25 GMT\n");
00309         strcat(sendstring,"Connection: close\n");
00310        // strcat(sendstring,"Content-Type: ");
00311        // strcat(sendstring,Mimetype);
00312        // strcat(sendstring,"\n");
00313         strcat(sendstring,"Content-Length: 0");
00314         strcat(sendstring,"\n\n");
00315         break;
00316     }
00317 
00318     if (dbg) printf("len=%d, inhalt=\n%s",strlen(sendstring),sendstring);
00319 
00320     _parser->debug_on(true);
00321     _parser->send("AT+CIPSEND=%d,%d",clientID[clientIdx],strlen(sendstring));  
00322     _parser->debug_on(false);          
00323     _parser->write(sendstring,strlen(sendstring));  
00324     HAL_Delay(200);                
00325     _parser->send("AT+CIPCLOSE=%d",clientID[clientIdx]);
00326     return 0;
00327 }
00328 
00329 int ESP8266Webserver::send(int HTTPStatus,const char* Mimetype, const char* webseite, int length)
00330 {
00331     char hilf[40];
00332     char statustext[40];
00333     
00334     sprintf(statustext,"OK");
00335  
00336     switch (HTTPStatus)
00337     {   
00338     case 404: sprintf(statustext,"Not Found"); break;
00339     case 206: sprintf(statustext,"Partial Content"); break;
00340     case 401: sprintf(statustext,"Unauthorized"); break;
00341     case 403: sprintf(statustext,"Forbidden"); break;
00342     case 307:
00343     case 302: sprintf(statustext,"Moved Temporarily"); break;
00344     case 301: sprintf(statustext,"Moved Permanently"); break;
00345     case 304: sprintf(statustext,"Not Modified"); break;
00346     
00347     }
00348     
00349     
00350     //printf("\r\nHallo\r\n");
00351     Aufrufe++;
00352     switch (HTTPStatus)
00353     {
00354     case 206:
00355     case 200: sprintf(sendstring,"HTTP/1.1 %d %s\n",HTTPStatus,statustext); 
00356         strcat(sendstring,"Date: Wed, 23 Apr 2021 04:36:25 GMT\n");
00357         strcat(sendstring,"Connection: close\n");
00358         strcat(sendstring,"Content-Type: ");
00359         strcat(sendstring,Mimetype);
00360         strcat(sendstring,"\n");
00361         sprintf(hilf,"%d",length);
00362         strcat(sendstring,"Content-Length: ");
00363         strcat(sendstring,hilf);
00364         strcat(sendstring,"\n\n");
00365         strcat(sendstring,webseite);
00366         break;
00367         
00368     default: sprintf(sendstring,"HTTP/1.1 %d %s\n",HTTPStatus,statustext); 
00369         strcat(sendstring,"Date: Wed, 23 Apr 2021 04:36:25 GMT\n");
00370         strcat(sendstring,"Connection: close\n");
00371        // strcat(sendstring,"Content-Type: ");
00372        // strcat(sendstring,Mimetype);
00373        // strcat(sendstring,"\n");
00374         strcat(sendstring,"Content-Length: 0");
00375         strcat(sendstring,"\n\n");
00376         break;
00377     }
00378 
00379     if (dbg) printf("len=%d, inhalt=\n%s",strlen(sendstring),sendstring);
00380 
00381     _parser->debug_on(true);
00382     _parser->send("AT+CIPSEND=%d,%d",clientID[clientIdx],strlen(sendstring)+length);  
00383     _parser->debug_on(false);          
00384     _parser->write(sendstring,strlen(sendstring));  
00385     HAL_Delay(200);                
00386     _parser->send("AT+CIPCLOSE=%d",clientID[clientIdx]);
00387     return 0;
00388 }
00389 
00390 
00391 void ESP8266Webserver::debugOn(bool pD)
00392 {
00393     dbg=pD;
00394     _parser->debug_on(pD);  
00395 }
00396 
00397 const char* ESP8266Webserver::gibWert(string suchstring)
00398 {
00399     return ESP8266Webserver::gibWert(suchstring.c_str());
00400 }
00401 
00402 const char* ESP8266Webserver::gibWert(const char* suchstring)
00403 {
00404     static char hilf[20];
00405     
00406     char *fundort;
00407     char *error;
00408     char* referer;
00409     char* favicon;
00410     char* post;
00411     post=strstr(recbuf,"POST");
00412     favicon=strstr(recbuf,"favicon");
00413     //if (favicon!=NULL) return NULL;
00414     error=strstr(recbuf,"ERROR");
00415     //if (error==NULL) return NULL;
00416     
00417     referer=strstr(recbuf,"Referer");
00418     
00419     int i=0;
00420     fundort=strstr(recbuf,suchstring);
00421     if (post==NULL)
00422     {
00423         if (fundort!=NULL&&(fundort<referer||referer==NULL))
00424         {
00425             fundort=fundort+strlen(suchstring)+1;
00426             while(i<20 && fundort[i]!=38 && fundort[i]>32 ) //space, &
00427             {
00428                 hilf[i]=fundort[i];
00429                 i++;
00430             }
00431             hilf[i]=0;
00432             if (dbg) printf("\r\nsuchergebnis=%s\r\n",hilf);
00433             return hilf;
00434         }
00435         else return NULL;
00436     }
00437     else
00438     {
00439        if (fundort!=NULL)
00440        {
00441             fundort=fundort+strlen(suchstring)+1;
00442             while(i<20 && fundort[i]!='&' && i<sizeof(fundort) ) 
00443             {
00444                 hilf[i]=fundort[i];
00445                 i++;
00446             }
00447             hilf[i]=0;
00448             if (dbg) printf("\r\nsuchergebnis=%s\r\n",hilf);
00449             return hilf;           
00450        }
00451        else return NULL;
00452     }
00453 }
00454 
00455 void ESP8266Webserver::listAPs()
00456 {
00457     int ips[4];
00458     //_parser->debug_on(true);
00459     _parser->send("AT+CWMODE=1"); 
00460     HAL_Delay(300);
00461     while (_parser->recv("OK")==false);
00462     HAL_Delay(1000);
00463     if (scanAPs==true)
00464     {
00465         _parser->send("AT+CWLAP");
00466         while (!_serial->readable());
00467         HAL_Delay(10000);
00468         printf("\r\nBuf=");
00469         while (_serial->readable())
00470                 {
00471                     _parser->read(recbuf, 1);
00472                     printf("%s",recbuf); 
00473                 }   
00474         printf("\r\n"); 
00475     }
00476     for (int i=0;i<5;i++)
00477     {
00478         _parser->send("AT+CWJAP=\"%s\",\"%s\"",ssid,passwort);  
00479         HAL_Delay(1000);
00480         if (_parser->recv("WIFI GOT IP")) i=6;
00481     } 
00482     _parser->send("AT+CIFSR");
00483     while (_parser->recv("+CIFSR:STAIP,")==false) 
00484     {
00485         _parser->send("AT+CIFSR");
00486         HAL_Delay(100);
00487     }
00488     
00489 //    _parser->read(ipadr,15);
00490     _parser->scanf("\"%d.%d.%d.%d\"",&ips[0],&ips[1],&ips[2],&ips[3]);
00491     sprintf(ipadr,"%d.%d.%d.%d",ips[0],ips[1],ips[2],ips[3]);
00492     printf("%s",ipadr);
00493         
00494     _parser->send("AT+CIPMUX=1");
00495     while (_parser->recv("OK")==false);
00496     
00497     //_parser->debug_on(false);
00498 }
00499 
00500 char* ESP8266Webserver::gibIP()
00501 {
00502     static char ipa[16];
00503     int j=0;
00504     for (int i=0;i<strlen(ipadr);i++)
00505     {
00506         if (i==0 && ipadr[0]=='"'){}
00507         else
00508         {        
00509         switch (ipadr[i])
00510         {
00511             case '0':
00512             case '1':
00513             case '2':
00514             case '3':
00515             case '4':
00516             case '5':
00517             case '6':
00518             case '7':
00519             case '8':
00520             case '9':
00521             case '.': ipa[j]=ipadr[i];
00522                         j++;
00523                         break;
00524             default: i=10000; break;
00525         }
00526         if (i==10000) break;
00527         }
00528     }
00529     ipa[j+1]='\0';
00530 
00531     return ipa;
00532 }
00533