Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ESP8266_mdm.cpp
00001 /** 00002 *@section DESCRIPTION 00003 * ESP8266_mdm Library 00004 * This sets up the ESP8266 as a server or client. 00005 * It detect \r\n sequence from the ESP8266 to chunk the response into lines. This then drives a state machine. 00006 *@section LICENSE 00007 * Copyright (c) 2016, Malcolm McCulloch 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining a copy 00010 * of this software and associated documentation files (the "Software"), to deal 00011 * in the Software without restriction, including without limitation the rights 00012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00013 * copies of the Software, and to permit persons to whom the Software is 00014 * furnished to do so, subject to the following conditions: 00015 * 00016 * The above copyright notice and this permission notice shall be included in 00017 * all copies or substantial portions of the Software. 00018 * 00019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00025 * THE SOFTWARE. 00026 * @file "ESP8266_mdm.cpp" 00027 */ 00028 #include "ESP8266_mdm.h " 00029 00030 // ********************* 00031 // * Protected variables: * 00032 // ********************* 00033 char* ESP8266_mdm::startCmnds[10]= { 00034 "AT", 00035 "AT", 00036 "AT", 00037 "AT+RST", 00038 "AT+CWMODE=1", 00039 "AT+CWJAP=\"epgmdm\",\"3F7540BC59\"", 00040 "AT+CIFSR", 00041 "AT+CIPMUX=1", 00042 "AT+CIPSERVER=1,8080", 00043 "AT+CIPSEND=0," 00044 }; 00045 char* ESP8266_mdm::startResp[10]= { 00046 "OK", 00047 "OK", 00048 "OK", 00049 "OK", 00050 "WIFI GOT IP", 00051 "OK", 00052 "OK", 00053 "+CIFSR:STAIP", 00054 "OK", 00055 "+IPD" 00056 }; 00057 00058 int ESP8266_mdm::startMaxState=7; 00059 00060 // ********************* 00061 // * Public functions: * 00062 // ********************* 00063 00064 ESP8266_mdm::ESP8266_mdm(RawSerial *esp, int server, RawSerial *pc, PinName rstPn):esp(esp),server(server),pc(pc),rstPn(rstPn) 00065 { 00066 reset = new DigitalOut(rstPn,1); 00067 esp->baud(115200); 00068 esp->attach(callback(this, &ESP8266_mdm::dev_recv)); 00069 reset->write(0); 00070 // Init variables 00071 buffer=(char *)calloc(BUFF_SIZE,1); 00072 bufferPnt=0; 00073 ready=0; 00074 state=0; 00075 // Create hardware 00076 last = new Timer(); 00077 00078 00079 //esp->attach(this,&ESP8266_mdm::dev_recv, Serial::RxIrq); 00080 // 00081 // Setting up interrupts 00082 // 00083 INFO("Reseting"); 00084 wait(1.5); 00085 reset->write(1); 00086 pc->printf("Start up\n\r"); 00087 wait(2.0); 00088 esp->printf("AT\r\n"); 00089 00090 INFO("Starting up v5"); 00091 00092 // Startup 00093 command = startCmnds; 00094 responseRequired = startResp; 00095 maxState = startMaxState; 00096 timeOut=11; 00097 DBG("Start Do Loop"); 00098 doLoop(0); 00099 DBG ("DONE"); 00100 while(1); 00101 00102 } 00103 00104 // ********************* 00105 // * Protected functions: * 00106 // ********************* 00107 00108 /** 00109 * Device has received a byte 00110 */ 00111 void ESP8266_mdm::dev_recv() 00112 { 00113 char c; 00114 if(bufferPnt==0) { 00115 memset(buffer,0,BUFF_SIZE); 00116 } 00117 while(esp->readable()) { 00118 c = (char)esp->getc(); 00119 //if (state>=maxState){ 00120 pc->putc(c); 00121 // } 00122 00123 buffer[bufferPnt]=c; 00124 bufferPnt++; 00125 if (bufferPnt>1024) { 00126 ready=1; 00127 } 00128 if ((c==0x0a)||(c==0x0d)) { 00129 ready=1; 00130 } else if (c==0x0a) { 00131 if (bufferPnt>1) { 00132 if (buffer[bufferPnt -2]==0x0d) { 00133 ready=1; 00134 break; 00135 } 00136 } 00137 } 00138 if (!esp->readable()) { 00139 wait_us(100); 00140 } 00141 } 00142 } 00143 00144 /** 00145 * Checks if pattern is in test. Returns 1 for true, 0 for no. -1 if busy. -2 ERROR 00146 * 00147 */ 00148 int ESP8266_mdm::OKResponse(char *test, const char *pattern) 00149 { 00150 00151 char *p= strstr(test,pattern); 00152 if (p!=NULL) { 00153 return 1; 00154 } 00155 p= strstr(test,"busy"); 00156 if (p!=NULL) { 00157 return -1; 00158 } 00159 p= strstr(test,"ERROR"); 00160 if (p!=NULL) { 00161 return -2; 00162 } 00163 return 0; 00164 } 00165 /** 00166 * Loops through until state = maxState; 00167 */ 00168 void ESP8266_mdm::doLoop(int initState) 00169 { 00170 int resp; 00171 state = initState; 00172 // last->reset(); 00173 // last->start(); 00174 DBG("[%d] next %s : Waiting for %s",state,command[state],responseRequired[state]); 00175 00176 while (1) { 00177 // printf(" {t %d}", *last); 00178 // Timeout code 00179 /* if ((*last)>timeOut){ 00180 WARN("Timeout on [%d] next %s : Waiting for %s",state,command[state],responseRequired[state]); 00181 last->reset(); 00182 if (state>0) { 00183 esp->printf("%s\r\n",command[state-1]); 00184 } else { 00185 esp->printf("%s\r\n",command[state]); 00186 } 00187 00188 } 00189 */ 00190 if (ready) { 00191 ready=0; 00192 last->reset(); 00193 bufferPnt=0; 00194 printf("<%s>\r\n",buffer); 00195 DBG("[%d] next %s : Waiting for %s",state,command[state],responseRequired[state]); 00196 resp=OKResponse(buffer,responseRequired[state]); 00197 switch (resp) { 00198 case 0: { // not yet 00199 break; 00200 } 00201 case 1: { //Yes 00202 esp->printf("%s\r\n",command[state]); 00203 state++; 00204 break; 00205 } 00206 case -1: { // busy 00207 if (state>0) { 00208 esp->printf("%s\r\n",command[state-1]); 00209 } else { 00210 esp->printf("%s\r\n",command[state]); 00211 } 00212 break; 00213 } 00214 case -2: { //ERROR 00215 esp->printf("AT\r\n"); 00216 wait(1); 00217 if (state>0) { 00218 esp->printf("%s\r\n",command[state-1]); 00219 } else { 00220 esp->printf("%s\r\n",command[state]); 00221 } 00222 break; 00223 } 00224 } // switch 00225 } // if 00226 } // while 00227 last->stop(); 00228 }
Generated on Mon Jul 18 2022 10:08:58 by
