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.
EDUVANCE_WIFI_SHIELD.cpp@3:a0b26799134f, 2016-05-29 (annotated)
- Committer:
- gauresh2798
- Date:
- Sun May 29 22:16:29 2016 +0000
- Revision:
- 3:a0b26799134f
- Parent:
- 2:d5c9337fdce1
Final and working (goes to ap mode if no wifi server found, then sets ssid and password from user input, and finally joins the server to host page )(add ons: power cycle or reset will not remove the ssid stored in esp8266, connects automatically)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gauresh2798 | 0:c359fee24b6c | 1 | |
gauresh2798 | 0:c359fee24b6c | 2 | #include "mbed.h" |
gauresh2798 | 0:c359fee24b6c | 3 | #include "EDUVANCE_WIFI_SHIELD.h" |
gauresh2798 | 0:c359fee24b6c | 4 | //CONSTANTS FOR MODES |
gauresh2798 | 0:c359fee24b6c | 5 | #define STATIONMODE 10 |
gauresh2798 | 0:c359fee24b6c | 6 | #define APMODE 20 |
gauresh2798 | 3:a0b26799134f | 7 | int AP_LOGIN =0; |
gauresh2798 | 3:a0b26799134f | 8 | int STATION_LOGIN= 0; |
gauresh2798 | 0:c359fee24b6c | 9 | Serial pc(USBTX, USBRX);// COMPUTER DISPLAY |
gauresh2798 | 0:c359fee24b6c | 10 | Serial esp(PTE0, PTE1); //ESP8266 SERIAL |
gauresh2798 | 1:52cad7057640 | 11 | extern int modeselector=0; |
gauresh2798 | 0:c359fee24b6c | 12 | char ssid[32]; |
gauresh2798 | 0:c359fee24b6c | 13 | char pwd[32]; |
gauresh2798 | 3:a0b26799134f | 14 | bool aplogin=false; |
gauresh2798 | 0:c359fee24b6c | 15 | const char ch = 's'; |
gauresh2798 | 3:a0b26799134f | 16 | char *getreq,*ssid_new,*pass_new,*left,*right,*pad_string; |
gauresh2798 | 0:c359fee24b6c | 17 | //ADD DEVICES,PINS AND LEDS HERE |
gauresh2798 | 0:c359fee24b6c | 18 | DigitalOut led1(LED_RED); |
gauresh2798 | 3:a0b26799134f | 19 | int port=8266; |
gauresh2798 | 0:c359fee24b6c | 20 | |
gauresh2798 | 0:c359fee24b6c | 21 | |
gauresh2798 | 0:c359fee24b6c | 22 | |
gauresh2798 | 0:c359fee24b6c | 23 | |
gauresh2798 | 0:c359fee24b6c | 24 | Timer t1; |
gauresh2798 | 0:c359fee24b6c | 25 | Timer t2; |
gauresh2798 | 0:c359fee24b6c | 26 | int setval=0; |
gauresh2798 | 0:c359fee24b6c | 27 | |
gauresh2798 | 0:c359fee24b6c | 28 | int bufflen, DataRX, count, getcount, replycount, servreq, timeout; |
gauresh2798 | 0:c359fee24b6c | 29 | int bufl, ipdLen, linkID, weberror, webcounter; |
gauresh2798 | 0:c359fee24b6c | 30 | |
gauresh2798 | 0:c359fee24b6c | 31 | |
gauresh2798 | 0:c359fee24b6c | 32 | char webcount[8]; |
gauresh2798 | 0:c359fee24b6c | 33 | |
gauresh2798 | 0:c359fee24b6c | 34 | char timebuf[30]; |
gauresh2798 | 0:c359fee24b6c | 35 | char type[16]; |
gauresh2798 | 0:c359fee24b6c | 36 | char type1[16]; |
gauresh2798 | 0:c359fee24b6c | 37 | char channel[2]; |
gauresh2798 | 0:c359fee24b6c | 38 | char cmdbuff[32]; |
gauresh2798 | 0:c359fee24b6c | 39 | char replybuff[512]; |
gauresh2798 | 0:c359fee24b6c | 40 | char webdata[1024]; // This may need to be bigger depending on WEB browser used |
gauresh2798 | 0:c359fee24b6c | 41 | char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added |
gauresh2798 | 0:c359fee24b6c | 42 | int mode_sel=0; |
gauresh2798 | 0:c359fee24b6c | 43 | |
gauresh2798 | 3:a0b26799134f | 44 | // set server port |
gauresh2798 | 0:c359fee24b6c | 45 | int SERVtimeout =5; // set server timeout in seconds incase link breaks.void callback(void); |
gauresh2798 | 0:c359fee24b6c | 46 | |
gauresh2798 | 0:c359fee24b6c | 47 | //TAKES SSID, PASSWORD AND PORT ADDRESS |
gauresh2798 | 0:c359fee24b6c | 48 | void get_ssid_pass(char ssid_f[32],char pass_f[32],int port_f){ |
gauresh2798 | 0:c359fee24b6c | 49 | pc.printf("\n\r+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 50 | pc.printf("\n\r+++++++++++++++++++++ INITIALIZING ++++++++++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 51 | pc.printf("\n\r+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 52 | pc.printf("\r\n SSID is %s, PASSWORD is %s, PORT number is %d \r\n",ssid_f,pass_f,port_f); |
gauresh2798 | 0:c359fee24b6c | 53 | strcpy(ssid,ssid_f); |
gauresh2798 | 0:c359fee24b6c | 54 | strcpy(pwd,pass_f); |
gauresh2798 | 3:a0b26799134f | 55 | // port=8266; |
gauresh2798 | 0:c359fee24b6c | 56 | } |
gauresh2798 | 0:c359fee24b6c | 57 | |
gauresh2798 | 0:c359fee24b6c | 58 | //SETS BAUD RATE |
gauresh2798 | 0:c359fee24b6c | 59 | void setbaudrate(int baud_f){ |
gauresh2798 | 0:c359fee24b6c | 60 | pc.baud(baud_f); |
gauresh2798 | 0:c359fee24b6c | 61 | esp.baud(baud_f); |
gauresh2798 | 0:c359fee24b6c | 62 | } |
gauresh2798 | 0:c359fee24b6c | 63 | |
gauresh2798 | 0:c359fee24b6c | 64 | // Serial Interrupt read ESP data |
gauresh2798 | 0:c359fee24b6c | 65 | void callback() { |
gauresh2798 | 0:c359fee24b6c | 66 | while (esp.readable()) {webbuff[count] = esp.getc();count++;} |
gauresh2798 | 0:c359fee24b6c | 67 | if(strlen(webbuff)>bufflen){DataRX=1;} |
gauresh2798 | 0:c359fee24b6c | 68 | } |
gauresh2798 | 0:c359fee24b6c | 69 | //SENDS COMMANDS TO ESP |
gauresh2798 | 0:c359fee24b6c | 70 | void SendCMD(void){ |
gauresh2798 | 0:c359fee24b6c | 71 | esp.printf("%s", cmdbuff); |
gauresh2798 | 0:c359fee24b6c | 72 | } |
gauresh2798 | 0:c359fee24b6c | 73 | //GETS REPLY IN THE FORM OF STRING |
gauresh2798 | 0:c359fee24b6c | 74 | void getreply(void){ |
gauresh2798 | 0:c359fee24b6c | 75 | memset(replybuff, '\0', sizeof(replybuff)); |
gauresh2798 | 0:c359fee24b6c | 76 | t1.reset(); t1.start();replycount=0; |
gauresh2798 | 0:c359fee24b6c | 77 | while(t1.read_ms()< timeout && replycount < getcount) { |
gauresh2798 | 0:c359fee24b6c | 78 | if(esp.readable()) { |
gauresh2798 | 0:c359fee24b6c | 79 | replybuff[replycount] = esp.getc();replycount++; |
gauresh2798 | 0:c359fee24b6c | 80 | } |
gauresh2798 | 0:c359fee24b6c | 81 | } |
gauresh2798 | 0:c359fee24b6c | 82 | t1.stop(); |
gauresh2798 | 0:c359fee24b6c | 83 | } |
gauresh2798 | 0:c359fee24b6c | 84 | |
gauresh2798 | 0:c359fee24b6c | 85 | //READ THE RESPONSE AND TRIGGERS THE ACTIONS [CONTROLS LEDS] |
gauresh2798 | 0:c359fee24b6c | 86 | void ReadWebData() |
gauresh2798 | 0:c359fee24b6c | 87 | { |
gauresh2798 | 0:c359fee24b6c | 88 | wait_ms(200); |
gauresh2798 | 0:c359fee24b6c | 89 | esp.attach(NULL); |
gauresh2798 | 0:c359fee24b6c | 90 | count=0;DataRX=0;weberror=0; |
gauresh2798 | 0:c359fee24b6c | 91 | memset(webdata, '\0', sizeof(webdata)); |
gauresh2798 | 0:c359fee24b6c | 92 | |
gauresh2798 | 0:c359fee24b6c | 93 | int x = strcspn (webbuff,"+"); |
gauresh2798 | 0:c359fee24b6c | 94 | if(x){ |
gauresh2798 | 0:c359fee24b6c | 95 | strcpy(webdata, webbuff + x);weberror=0; |
gauresh2798 | 0:c359fee24b6c | 96 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
gauresh2798 | 0:c359fee24b6c | 97 | |
gauresh2798 | 0:c359fee24b6c | 98 | |
gauresh2798 | 0:c359fee24b6c | 99 | if( strstr(webdata, "led1=1") != NULL ) {led1=0;} |
gauresh2798 | 0:c359fee24b6c | 100 | if( strstr(webdata, "led1=0") != NULL ) {led1=1;} |
gauresh2798 | 0:c359fee24b6c | 101 | |
gauresh2798 | 0:c359fee24b6c | 102 | sprintf(channel, "%d",linkID); |
gauresh2798 | 0:c359fee24b6c | 103 | if (strstr(webdata, "GET") != NULL) {servreq=1;} |
gauresh2798 | 0:c359fee24b6c | 104 | if (strstr(webdata, "POST") != NULL) {servreq=1;} |
gauresh2798 | 0:c359fee24b6c | 105 | webcounter++; |
gauresh2798 | 0:c359fee24b6c | 106 | sprintf(webcount, "%d",webcounter); |
gauresh2798 | 0:c359fee24b6c | 107 | } |
gauresh2798 | 0:c359fee24b6c | 108 | else { |
gauresh2798 | 0:c359fee24b6c | 109 | memset(webbuff, '\0', sizeof(webbuff)); |
gauresh2798 | 0:c359fee24b6c | 110 | esp.attach(&callback);weberror=1; |
gauresh2798 | 0:c359fee24b6c | 111 | } |
gauresh2798 | 0:c359fee24b6c | 112 | } |
gauresh2798 | 0:c359fee24b6c | 113 | |
gauresh2798 | 0:c359fee24b6c | 114 | |
gauresh2798 | 0:c359fee24b6c | 115 | |
gauresh2798 | 0:c359fee24b6c | 116 | |
gauresh2798 | 0:c359fee24b6c | 117 | //READ THE RESPONSE AND TRIGGERS THE ACTIONS [CONTROLS LEDS] |
gauresh2798 | 0:c359fee24b6c | 118 | void ReadWebData_register() |
gauresh2798 | 0:c359fee24b6c | 119 | { |
gauresh2798 | 0:c359fee24b6c | 120 | wait_ms(200); |
gauresh2798 | 0:c359fee24b6c | 121 | esp.attach(NULL); |
gauresh2798 | 0:c359fee24b6c | 122 | count=0;DataRX=0;weberror=0; |
gauresh2798 | 0:c359fee24b6c | 123 | memset(webdata, '\0', sizeof(webdata)); |
gauresh2798 | 0:c359fee24b6c | 124 | |
gauresh2798 | 0:c359fee24b6c | 125 | int x = strcspn (webbuff,"+"); |
gauresh2798 | 0:c359fee24b6c | 126 | if(x){ |
gauresh2798 | 0:c359fee24b6c | 127 | strcpy(webdata, webbuff + x);weberror=0; |
gauresh2798 | 0:c359fee24b6c | 128 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
gauresh2798 | 0:c359fee24b6c | 129 | |
gauresh2798 | 0:c359fee24b6c | 130 | |
gauresh2798 | 0:c359fee24b6c | 131 | //setval=1; |
gauresh2798 | 0:c359fee24b6c | 132 | // if( strstr(webdata, "led1=1") != NULL ) {led1=0;} |
gauresh2798 | 0:c359fee24b6c | 133 | // if( strstr(webdata, "led1=0") != NULL ) {led1=1;} |
gauresh2798 | 0:c359fee24b6c | 134 | sprintf(channel, "%d",linkID); |
gauresh2798 | 0:c359fee24b6c | 135 | if (strstr(webdata, "GET") != NULL) {servreq=1;} |
gauresh2798 | 0:c359fee24b6c | 136 | if (strstr(webdata, "POST") != NULL) {servreq=1;} |
gauresh2798 | 0:c359fee24b6c | 137 | webcounter++; |
gauresh2798 | 0:c359fee24b6c | 138 | |
gauresh2798 | 0:c359fee24b6c | 139 | sprintf(webcount, "%d",webcounter); |
gauresh2798 | 2:d5c9337fdce1 | 140 | if (strstr(webdata, "ssid") != NULL) { |
gauresh2798 | 2:d5c9337fdce1 | 141 | newcre(webdata); } |
gauresh2798 | 0:c359fee24b6c | 142 | } |
gauresh2798 | 0:c359fee24b6c | 143 | else { |
gauresh2798 | 0:c359fee24b6c | 144 | memset(webbuff, '\0', sizeof(webbuff)); |
gauresh2798 | 0:c359fee24b6c | 145 | esp.attach(&callback);weberror=1; |
gauresh2798 | 0:c359fee24b6c | 146 | } |
gauresh2798 | 0:c359fee24b6c | 147 | } |
gauresh2798 | 0:c359fee24b6c | 148 | |
gauresh2798 | 0:c359fee24b6c | 149 | |
gauresh2798 | 0:c359fee24b6c | 150 | |
gauresh2798 | 0:c359fee24b6c | 151 | |
gauresh2798 | 0:c359fee24b6c | 152 | |
gauresh2798 | 0:c359fee24b6c | 153 | // Static WEB page TO DISPLAY |
gauresh2798 | 0:c359fee24b6c | 154 | void sendpage() |
gauresh2798 | 0:c359fee24b6c | 155 | { |
gauresh2798 | 0:c359fee24b6c | 156 | // WEB page data |
gauresh2798 | 0:c359fee24b6c | 157 | strcpy(webbuff, "<!DOCTYPE html>"); |
gauresh2798 | 0:c359fee24b6c | 158 | strcat(webbuff, "<html><head><title>ESP8266 Mbed</title></head>"); |
gauresh2798 | 0:c359fee24b6c | 159 | strcat(webbuff, "<body>"); |
gauresh2798 | 0:c359fee24b6c | 160 | |
gauresh2798 | 0:c359fee24b6c | 161 | strcat(webbuff, "<p><form method=\"GET\"></p>"); |
gauresh2798 | 0:c359fee24b6c | 162 | |
gauresh2798 | 0:c359fee24b6c | 163 | |
gauresh2798 | 0:c359fee24b6c | 164 | if(led1==1){strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> Red LED off"); |
gauresh2798 | 0:c359fee24b6c | 165 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > Red LED on");} |
gauresh2798 | 0:c359fee24b6c | 166 | else{strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > Red LED off"); |
gauresh2798 | 0:c359fee24b6c | 167 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> Red LED on");} |
gauresh2798 | 0:c359fee24b6c | 168 | |
gauresh2798 | 0:c359fee24b6c | 169 | strcat(webbuff, "<p><input type=\"submit\" value=\"send-refresh\" ></form></p>"); |
gauresh2798 | 0:c359fee24b6c | 170 | |
gauresh2798 | 0:c359fee24b6c | 171 | |
gauresh2798 | 0:c359fee24b6c | 172 | strcat(webbuff, "</body></html>"); |
gauresh2798 | 0:c359fee24b6c | 173 | // end of WEB page data |
gauresh2798 | 0:c359fee24b6c | 174 | bufl = strlen(webbuff); // get total page buffer length |
gauresh2798 | 0:c359fee24b6c | 175 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
gauresh2798 | 0:c359fee24b6c | 176 | timeout=200;getcount=7; |
gauresh2798 | 0:c359fee24b6c | 177 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 178 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 179 | SendWEB(); // send web page |
gauresh2798 | 0:c359fee24b6c | 180 | memset(webbuff, '\0', sizeof(webbuff)); |
gauresh2798 | 0:c359fee24b6c | 181 | sendcheck(); |
gauresh2798 | 0:c359fee24b6c | 182 | } |
gauresh2798 | 0:c359fee24b6c | 183 | |
gauresh2798 | 0:c359fee24b6c | 184 | void sendpage_register() |
gauresh2798 | 0:c359fee24b6c | 185 | { |
gauresh2798 | 0:c359fee24b6c | 186 | // WEB page data |
gauresh2798 | 0:c359fee24b6c | 187 | strcpy(webbuff, "<!DOCTYPE html>"); |
gauresh2798 | 0:c359fee24b6c | 188 | strcat(webbuff, "<html><head><title>ESP8266 Mbed</title></head>"); |
gauresh2798 | 0:c359fee24b6c | 189 | strcat(webbuff, "<body>"); |
gauresh2798 | 0:c359fee24b6c | 190 | |
gauresh2798 | 0:c359fee24b6c | 191 | strcat(webbuff, "<form method=\"GET\">"); |
gauresh2798 | 0:c359fee24b6c | 192 | |
gauresh2798 | 0:c359fee24b6c | 193 | // strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > Red LED off"); |
gauresh2798 | 0:c359fee24b6c | 194 | // strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> Red LED on"); |
gauresh2798 | 0:c359fee24b6c | 195 | strcat(webbuff, "<input type=\"text\" name=\"ssid\"> ssid"); |
gauresh2798 | 0:c359fee24b6c | 196 | strcat(webbuff, "<br><input type=\"text\" name=\"pass\" > password"); |
gauresh2798 | 0:c359fee24b6c | 197 | |
gauresh2798 | 0:c359fee24b6c | 198 | |
gauresh2798 | 0:c359fee24b6c | 199 | |
gauresh2798 | 0:c359fee24b6c | 200 | strcat(webbuff, "<input type=\"submit\" value=\"send-refresh\" >"); |
gauresh2798 | 0:c359fee24b6c | 201 | strcat(webbuff,"</form>") ; |
gauresh2798 | 0:c359fee24b6c | 202 | |
gauresh2798 | 0:c359fee24b6c | 203 | |
gauresh2798 | 0:c359fee24b6c | 204 | strcat(webbuff, "</body></html>"); |
gauresh2798 | 0:c359fee24b6c | 205 | // end of WEB page data |
gauresh2798 | 0:c359fee24b6c | 206 | bufl = strlen(webbuff); // get total page buffer length |
gauresh2798 | 0:c359fee24b6c | 207 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
gauresh2798 | 0:c359fee24b6c | 208 | timeout=200;getcount=7; |
gauresh2798 | 0:c359fee24b6c | 209 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 210 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 211 | SendWEB(); // send web page |
gauresh2798 | 0:c359fee24b6c | 212 | memset(webbuff, '\0', sizeof(webbuff)); |
gauresh2798 | 0:c359fee24b6c | 213 | sendcheck(); |
gauresh2798 | 0:c359fee24b6c | 214 | |
gauresh2798 | 0:c359fee24b6c | 215 | |
gauresh2798 | 0:c359fee24b6c | 216 | |
gauresh2798 | 0:c359fee24b6c | 217 | } |
gauresh2798 | 0:c359fee24b6c | 218 | |
gauresh2798 | 0:c359fee24b6c | 219 | // wait for ESP "SEND OK" reply, then close IP to load web page |
gauresh2798 | 0:c359fee24b6c | 220 | void sendcheck() |
gauresh2798 | 0:c359fee24b6c | 221 | { |
gauresh2798 | 0:c359fee24b6c | 222 | weberror=1;timeout=500;getcount=24; |
gauresh2798 | 0:c359fee24b6c | 223 | t2.reset();t2.start(); |
gauresh2798 | 0:c359fee24b6c | 224 | while(weberror==1 && t2.read() <5){ |
gauresh2798 | 0:c359fee24b6c | 225 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 226 | if (strstr(replybuff, "SEND OK") != NULL) {weberror=0;} // wait for valid SEND OK |
gauresh2798 | 0:c359fee24b6c | 227 | } |
gauresh2798 | 0:c359fee24b6c | 228 | if(weberror==1){ // restart connection |
gauresh2798 | 0:c359fee24b6c | 229 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
gauresh2798 | 0:c359fee24b6c | 230 | timeout=500;getcount=10; |
gauresh2798 | 0:c359fee24b6c | 231 | SendCMD();getreply(); |
gauresh2798 | 0:c359fee24b6c | 232 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
gauresh2798 | 0:c359fee24b6c | 233 | timeout=500;getcount=10; |
gauresh2798 | 0:c359fee24b6c | 234 | SendCMD();getreply(); |
gauresh2798 | 0:c359fee24b6c | 235 | } |
gauresh2798 | 0:c359fee24b6c | 236 | else{ |
gauresh2798 | 0:c359fee24b6c | 237 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
gauresh2798 | 0:c359fee24b6c | 238 | SendCMD();} |
gauresh2798 | 0:c359fee24b6c | 239 | t2.reset(); |
gauresh2798 | 0:c359fee24b6c | 240 | } |
gauresh2798 | 0:c359fee24b6c | 241 | |
gauresh2798 | 0:c359fee24b6c | 242 | // Large WEB buffer data send |
gauresh2798 | 0:c359fee24b6c | 243 | void SendWEB() |
gauresh2798 | 0:c359fee24b6c | 244 | { |
gauresh2798 | 0:c359fee24b6c | 245 | int i=0; |
gauresh2798 | 0:c359fee24b6c | 246 | if(esp.writeable()) { |
gauresh2798 | 0:c359fee24b6c | 247 | while(webbuff[i]!='\0') {esp.putc(webbuff[i]);i++;} |
gauresh2798 | 0:c359fee24b6c | 248 | } |
gauresh2798 | 0:c359fee24b6c | 249 | } |
gauresh2798 | 0:c359fee24b6c | 250 | |
gauresh2798 | 0:c359fee24b6c | 251 | void ATcheck(void){ |
gauresh2798 | 0:c359fee24b6c | 252 | //FIRST AT COMMAND |
gauresh2798 | 0:c359fee24b6c | 253 | pc.printf("\r\n++++++++++++++++++++ AT RESPONSE ESP ++++++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 254 | strcpy(cmdbuff,"AT\r\n"); |
gauresh2798 | 0:c359fee24b6c | 255 | timeout=8000; |
gauresh2798 | 0:c359fee24b6c | 256 | getcount=1000; |
gauresh2798 | 0:c359fee24b6c | 257 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 258 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 259 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 0:c359fee24b6c | 260 | } |
gauresh2798 | 0:c359fee24b6c | 261 | |
gauresh2798 | 0:c359fee24b6c | 262 | void ATReset(void){ |
gauresh2798 | 0:c359fee24b6c | 263 | //RESET ESP |
gauresh2798 | 0:c359fee24b6c | 264 | pc.printf("+++++++++++++++++++++++++ Resetting ESP ++++++++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 265 | strcpy(cmdbuff,"AT+RST\r\n"); |
gauresh2798 | 0:c359fee24b6c | 266 | timeout=2000;getcount=600; |
gauresh2798 | 0:c359fee24b6c | 267 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 268 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 269 | pc.printf(replybuff); |
gauresh2798 | 0:c359fee24b6c | 270 | pc.printf("%d",count); |
gauresh2798 | 0:c359fee24b6c | 271 | } |
gauresh2798 | 0:c359fee24b6c | 272 | |
gauresh2798 | 0:c359fee24b6c | 273 | void ListAccessPts(void){ |
gauresh2798 | 0:c359fee24b6c | 274 | //list access points |
gauresh2798 | 0:c359fee24b6c | 275 | pc.printf("++++++++++++++++++ Listing Access POINTS NEARBY +++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 276 | strcpy(cmdbuff,"AT+CWLAP\r\n"); |
gauresh2798 | 0:c359fee24b6c | 277 | timeout=8000; |
gauresh2798 | 0:c359fee24b6c | 278 | getcount=1000; |
gauresh2798 | 0:c359fee24b6c | 279 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 280 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 281 | //strcpy(replybuff,"\0"); |
gauresh2798 | 0:c359fee24b6c | 282 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 0:c359fee24b6c | 283 | //const char *tempdata=replybuff; |
gauresh2798 | 0:c359fee24b6c | 284 | //return replybuff; |
gauresh2798 | 0:c359fee24b6c | 285 | if (strstr(cmdbuff, ssid) != NULL){mode_sel=STATIONMODE;} |
gauresh2798 | 0:c359fee24b6c | 286 | else{mode_sel=APMODE;} |
gauresh2798 | 0:c359fee24b6c | 287 | } |
gauresh2798 | 0:c359fee24b6c | 288 | |
gauresh2798 | 0:c359fee24b6c | 289 | void StationModeSet(void){ |
gauresh2798 | 0:c359fee24b6c | 290 | //SET CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
gauresh2798 | 0:c359fee24b6c | 291 | pc.printf("+++++++++++++++++++ SETTING ESP IN STATION MODE +++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 292 | strcpy(cmdbuff,"AT+CWMODE=1\r\n"); |
gauresh2798 | 0:c359fee24b6c | 293 | timeout=8000; |
gauresh2798 | 0:c359fee24b6c | 294 | getcount=1000; |
gauresh2798 | 0:c359fee24b6c | 295 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 296 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 297 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 0:c359fee24b6c | 298 | } |
gauresh2798 | 0:c359fee24b6c | 299 | |
gauresh2798 | 0:c359fee24b6c | 300 | void ApModeSet(void){ |
gauresh2798 | 0:c359fee24b6c | 301 | //SET CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
gauresh2798 | 0:c359fee24b6c | 302 | pc.printf("+++++++++++++++++++++ SETTING ESP IN AP MODE +++++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 303 | strcpy(cmdbuff,"AT+CWMODE=2\r\n"); |
gauresh2798 | 0:c359fee24b6c | 304 | timeout=8000; |
gauresh2798 | 0:c359fee24b6c | 305 | getcount=1000; |
gauresh2798 | 0:c359fee24b6c | 306 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 307 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 308 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 0:c359fee24b6c | 309 | } |
gauresh2798 | 0:c359fee24b6c | 310 | |
gauresh2798 | 0:c359fee24b6c | 311 | void ConnectWifiServer(void){ |
gauresh2798 | 0:c359fee24b6c | 312 | |
gauresh2798 | 0:c359fee24b6c | 313 | pc.printf("\n+++++++++++++++ Connecting to WIFI SERVER (AP) +++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 314 | pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); |
gauresh2798 | 0:c359fee24b6c | 315 | strcpy(cmdbuff, "AT+CWJAP=\""); |
gauresh2798 | 0:c359fee24b6c | 316 | strcat(cmdbuff, ssid); |
gauresh2798 | 0:c359fee24b6c | 317 | strcat(cmdbuff, "\",\""); |
gauresh2798 | 0:c359fee24b6c | 318 | strcat(cmdbuff, pwd); |
gauresh2798 | 0:c359fee24b6c | 319 | strcat(cmdbuff, "\"\r\n"); |
gauresh2798 | 0:c359fee24b6c | 320 | timeout=8000; |
gauresh2798 | 0:c359fee24b6c | 321 | getcount=1000; |
gauresh2798 | 0:c359fee24b6c | 322 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 323 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 324 | pc.printf("%s\r\n",cmdbuff); |
gauresh2798 | 0:c359fee24b6c | 325 | } |
gauresh2798 | 0:c359fee24b6c | 326 | |
gauresh2798 | 0:c359fee24b6c | 327 | void Commtype(void){ |
gauresh2798 | 0:c359fee24b6c | 328 | // set CIPMUX to 0=Single,1=Multi |
gauresh2798 | 0:c359fee24b6c | 329 | pc.printf("+++++++++++++++++ Setting Connection of ESP in MULTI ++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 330 | strcpy(cmdbuff,"AT+CIPMUX=1\r\n"); |
gauresh2798 | 0:c359fee24b6c | 331 | timeout=500; |
gauresh2798 | 0:c359fee24b6c | 332 | getcount=10; |
gauresh2798 | 0:c359fee24b6c | 333 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 334 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 335 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 0:c359fee24b6c | 336 | } |
gauresh2798 | 0:c359fee24b6c | 337 | |
gauresh2798 | 0:c359fee24b6c | 338 | void StartServer_AT(void){ |
gauresh2798 | 0:c359fee24b6c | 339 | //START SERVER |
gauresh2798 | 0:c359fee24b6c | 340 | pc.printf("\n++++++++++++++++++++++ STARTING SERVER OF ESP +++++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 341 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
gauresh2798 | 0:c359fee24b6c | 342 | timeout=500;getcount=10; |
gauresh2798 | 0:c359fee24b6c | 343 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 344 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 345 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
gauresh2798 | 0:c359fee24b6c | 346 | timeout=500;getcount=50; |
gauresh2798 | 0:c359fee24b6c | 347 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 348 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 349 | } |
gauresh2798 | 0:c359fee24b6c | 350 | void getIP(void){ |
gauresh2798 | 0:c359fee24b6c | 351 | //GET IP |
gauresh2798 | 0:c359fee24b6c | 352 | pc.printf("\n Getting Server IP \r\n"); |
gauresh2798 | 0:c359fee24b6c | 353 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
gauresh2798 | 0:c359fee24b6c | 354 | timeout=1000;getcount=50; |
gauresh2798 | 0:c359fee24b6c | 355 | while(weberror==0){ |
gauresh2798 | 0:c359fee24b6c | 356 | SendCMD();getreply(); |
gauresh2798 | 0:c359fee24b6c | 357 | if (strstr(replybuff, "0.0.0.0") == NULL) {weberror=1;} // wait for valid IP |
gauresh2798 | 0:c359fee24b6c | 358 | } |
gauresh2798 | 0:c359fee24b6c | 359 | pc.printf("\n Enter WEB address in your browser \r\n\n"); |
gauresh2798 | 0:c359fee24b6c | 360 | replybuff[strlen(replybuff) - 8] = '\0'; |
gauresh2798 | 0:c359fee24b6c | 361 | char* IP = replybuff + 5; |
gauresh2798 | 0:c359fee24b6c | 362 | sprintf(webdata," http://%s:%d", IP, port); |
gauresh2798 | 0:c359fee24b6c | 363 | pc.printf(webdata); |
gauresh2798 | 0:c359fee24b6c | 364 | } |
gauresh2798 | 3:a0b26799134f | 365 | void disp_ssid(void){ |
gauresh2798 | 0:c359fee24b6c | 366 | |
gauresh2798 | 3:a0b26799134f | 367 | pc.printf("\n\r++++++++++ DISPLAY CURRENT SSID ++++++++++\r\n"); |
gauresh2798 | 3:a0b26799134f | 368 | strcpy(cmdbuff,"AT+CWJAP?\r\n"); |
gauresh2798 | 3:a0b26799134f | 369 | timeout=8000; |
gauresh2798 | 3:a0b26799134f | 370 | getcount=1000; |
gauresh2798 | 3:a0b26799134f | 371 | SendCMD(); |
gauresh2798 | 3:a0b26799134f | 372 | getreply(); |
gauresh2798 | 3:a0b26799134f | 373 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 3:a0b26799134f | 374 | |
gauresh2798 | 3:a0b26799134f | 375 | |
gauresh2798 | 3:a0b26799134f | 376 | } |
gauresh2798 | 0:c359fee24b6c | 377 | void finishup(void){ |
gauresh2798 | 0:c359fee24b6c | 378 | //END PART |
gauresh2798 | 0:c359fee24b6c | 379 | bufflen=200;count=0; |
gauresh2798 | 0:c359fee24b6c | 380 | pc.printf("\n\n++++++++++++++++++++++++ READY ++++++++++++++++++++++++++\r\n\n"); |
gauresh2798 | 0:c359fee24b6c | 381 | esp.attach(&callback); |
gauresh2798 | 0:c359fee24b6c | 382 | } |
gauresh2798 | 0:c359fee24b6c | 383 | |
gauresh2798 | 0:c359fee24b6c | 384 | |
gauresh2798 | 0:c359fee24b6c | 385 | |
gauresh2798 | 0:c359fee24b6c | 386 | int startserver(void){ |
gauresh2798 | 3:a0b26799134f | 387 | pc.printf("++++++++++++++++++++++++ START MAIN SERVER ++++++++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 388 | ATcheck(); |
gauresh2798 | 0:c359fee24b6c | 389 | ATReset(); |
gauresh2798 | 0:c359fee24b6c | 390 | StationModeSet(); |
gauresh2798 | 0:c359fee24b6c | 391 | |
gauresh2798 | 0:c359fee24b6c | 392 | //list access points |
gauresh2798 | 0:c359fee24b6c | 393 | pc.printf("++++++++++++++++++++++++ Listing Access ESP ++++++++++++++++++++++\r\n"); |
gauresh2798 | 0:c359fee24b6c | 394 | strcpy(cmdbuff,"AT+CWLAP\r\n"); |
gauresh2798 | 0:c359fee24b6c | 395 | timeout=8000; |
gauresh2798 | 0:c359fee24b6c | 396 | getcount=1000; |
gauresh2798 | 0:c359fee24b6c | 397 | SendCMD(); |
gauresh2798 | 0:c359fee24b6c | 398 | getreply(); |
gauresh2798 | 0:c359fee24b6c | 399 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 3:a0b26799134f | 400 | /* |
gauresh2798 | 0:c359fee24b6c | 401 | if (strstr(replybuff, ssid) != NULL) { |
gauresh2798 | 0:c359fee24b6c | 402 | |
gauresh2798 | 0:c359fee24b6c | 403 | t2.reset();t2.start(); |
gauresh2798 | 0:c359fee24b6c | 404 | return 10; |
gauresh2798 | 0:c359fee24b6c | 405 | } |
gauresh2798 | 0:c359fee24b6c | 406 | else{ |
gauresh2798 | 0:c359fee24b6c | 407 | t2.reset();t2.start(); |
gauresh2798 | 0:c359fee24b6c | 408 | return 20; |
gauresh2798 | 0:c359fee24b6c | 409 | } |
gauresh2798 | 3:a0b26799134f | 410 | */ |
gauresh2798 | 3:a0b26799134f | 411 | return 0; |
gauresh2798 | 0:c359fee24b6c | 412 | } |
gauresh2798 | 0:c359fee24b6c | 413 | |
gauresh2798 | 0:c359fee24b6c | 414 | |
gauresh2798 | 1:52cad7057640 | 415 | void closeserver_AT(void){ |
gauresh2798 | 1:52cad7057640 | 416 | pc.printf("+++++++++++++++++ CLOSE THE SERVER ++++++++++++++\r\n"); |
gauresh2798 | 1:52cad7057640 | 417 | |
gauresh2798 | 1:52cad7057640 | 418 | sprintf(cmdbuff,"AT+CIPCLOSE=%d\r\n", linkID); |
gauresh2798 | 1:52cad7057640 | 419 | timeout=500; |
gauresh2798 | 1:52cad7057640 | 420 | getcount=10; |
gauresh2798 | 1:52cad7057640 | 421 | SendCMD(); |
gauresh2798 | 1:52cad7057640 | 422 | getreply(); |
gauresh2798 | 1:52cad7057640 | 423 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 1:52cad7057640 | 424 | |
gauresh2798 | 1:52cad7057640 | 425 | |
gauresh2798 | 1:52cad7057640 | 426 | |
gauresh2798 | 1:52cad7057640 | 427 | strcpy(cmdbuff,"AT+CIPSERVER=0\r\n"); |
gauresh2798 | 1:52cad7057640 | 428 | timeout=500; |
gauresh2798 | 1:52cad7057640 | 429 | getcount=10; |
gauresh2798 | 1:52cad7057640 | 430 | SendCMD(); |
gauresh2798 | 1:52cad7057640 | 431 | getreply(); |
gauresh2798 | 1:52cad7057640 | 432 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 1:52cad7057640 | 433 | |
gauresh2798 | 1:52cad7057640 | 434 | ATReset(); |
gauresh2798 | 1:52cad7057640 | 435 | |
gauresh2798 | 1:52cad7057640 | 436 | |
gauresh2798 | 1:52cad7057640 | 437 | } |
gauresh2798 | 0:c359fee24b6c | 438 | |
gauresh2798 | 3:a0b26799134f | 439 | void StationConfig_NO_SSID_NEED(void){ |
gauresh2798 | 3:a0b26799134f | 440 | |
gauresh2798 | 3:a0b26799134f | 441 | pc.printf("\n\r------------------- IN THE STATION MODE(MATCHING SSID) -------------------\r\n"); |
gauresh2798 | 3:a0b26799134f | 442 | Commtype(); |
gauresh2798 | 3:a0b26799134f | 443 | StartServer_AT(); |
gauresh2798 | 3:a0b26799134f | 444 | getIP(); |
gauresh2798 | 3:a0b26799134f | 445 | finishup(); |
gauresh2798 | 3:a0b26799134f | 446 | |
gauresh2798 | 3:a0b26799134f | 447 | } |
gauresh2798 | 3:a0b26799134f | 448 | |
gauresh2798 | 3:a0b26799134f | 449 | void StationConfig_SSID_NEED(void){ |
gauresh2798 | 3:a0b26799134f | 450 | pc.printf("\n\r------------------- IN THE STATION MODE(NON MATCHED SSID) -------------------\r\n"); |
gauresh2798 | 3:a0b26799134f | 451 | StationModeSet(); |
gauresh2798 | 0:c359fee24b6c | 452 | ConnectWifiServer(); |
gauresh2798 | 0:c359fee24b6c | 453 | Commtype(); |
gauresh2798 | 0:c359fee24b6c | 454 | StartServer_AT(); |
gauresh2798 | 0:c359fee24b6c | 455 | getIP(); |
gauresh2798 | 0:c359fee24b6c | 456 | finishup(); |
gauresh2798 | 3:a0b26799134f | 457 | |
gauresh2798 | 0:c359fee24b6c | 458 | } |
gauresh2798 | 3:a0b26799134f | 459 | |
gauresh2798 | 0:c359fee24b6c | 460 | void ApConfig(void){ |
gauresh2798 | 0:c359fee24b6c | 461 | pc.printf("\n\r--------------------- IN THE AP MODE -----------------------\r\n"); |
gauresh2798 | 0:c359fee24b6c | 462 | ApModeSet(); |
gauresh2798 | 0:c359fee24b6c | 463 | Commtype(); |
gauresh2798 | 0:c359fee24b6c | 464 | StartServer_AT(); |
gauresh2798 | 0:c359fee24b6c | 465 | getIP(); |
gauresh2798 | 0:c359fee24b6c | 466 | finishup(); |
gauresh2798 | 3:a0b26799134f | 467 | modeselector=20; |
gauresh2798 | 0:c359fee24b6c | 468 | } |
gauresh2798 | 0:c359fee24b6c | 469 | void request_fetch_station(void){ |
gauresh2798 | 0:c359fee24b6c | 470 | |
gauresh2798 | 0:c359fee24b6c | 471 | if(DataRX==1){ |
gauresh2798 | 0:c359fee24b6c | 472 | ReadWebData(); |
gauresh2798 | 0:c359fee24b6c | 473 | |
gauresh2798 | 0:c359fee24b6c | 474 | if (servreq == 1 && weberror == 0){sendpage();} |
gauresh2798 | 0:c359fee24b6c | 475 | esp.attach(&callback); |
gauresh2798 | 0:c359fee24b6c | 476 | pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type); |
gauresh2798 | 0:c359fee24b6c | 477 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
gauresh2798 | 0:c359fee24b6c | 478 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
gauresh2798 | 0:c359fee24b6c | 479 | pc.printf(" -------------------------------------\n\n"); |
gauresh2798 | 0:c359fee24b6c | 480 | //strcpy(lasthit, timebuf); |
gauresh2798 | 0:c359fee24b6c | 481 | servreq=0; |
gauresh2798 | 0:c359fee24b6c | 482 | } |
gauresh2798 | 0:c359fee24b6c | 483 | } |
gauresh2798 | 0:c359fee24b6c | 484 | |
gauresh2798 | 0:c359fee24b6c | 485 | |
gauresh2798 | 0:c359fee24b6c | 486 | void request_fetch_ap(void){ |
gauresh2798 | 0:c359fee24b6c | 487 | if(DataRX==1){ |
gauresh2798 | 0:c359fee24b6c | 488 | |
gauresh2798 | 0:c359fee24b6c | 489 | |
gauresh2798 | 0:c359fee24b6c | 490 | ReadWebData_register(); |
gauresh2798 | 0:c359fee24b6c | 491 | |
gauresh2798 | 0:c359fee24b6c | 492 | if (servreq == 1 && weberror == 0){sendpage_register();} |
gauresh2798 | 0:c359fee24b6c | 493 | esp.attach(&callback); |
gauresh2798 | 0:c359fee24b6c | 494 | pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type); |
gauresh2798 | 0:c359fee24b6c | 495 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
gauresh2798 | 0:c359fee24b6c | 496 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
gauresh2798 | 0:c359fee24b6c | 497 | pc.printf(" -------------------------------------\n\n"); |
gauresh2798 | 2:d5c9337fdce1 | 498 | |
gauresh2798 | 0:c359fee24b6c | 499 | //strcpy(lasthit, timebuf); |
gauresh2798 | 0:c359fee24b6c | 500 | |
gauresh2798 | 0:c359fee24b6c | 501 | servreq=0; |
gauresh2798 | 0:c359fee24b6c | 502 | |
gauresh2798 | 2:d5c9337fdce1 | 503 | } |
gauresh2798 | 2:d5c9337fdce1 | 504 | |
gauresh2798 | 1:52cad7057640 | 505 | |
gauresh2798 | 0:c359fee24b6c | 506 | } |
gauresh2798 | 0:c359fee24b6c | 507 | |
gauresh2798 | 0:c359fee24b6c | 508 | char* getname(char *strd,const char s[2],int part){ |
gauresh2798 | 0:c359fee24b6c | 509 | char *temp; |
gauresh2798 | 0:c359fee24b6c | 510 | char *token,*dataarr[2]; |
gauresh2798 | 0:c359fee24b6c | 511 | int i=0; |
gauresh2798 | 0:c359fee24b6c | 512 | |
gauresh2798 | 0:c359fee24b6c | 513 | token = strtok(strd, s); |
gauresh2798 | 0:c359fee24b6c | 514 | |
gauresh2798 | 0:c359fee24b6c | 515 | while( token != NULL ) |
gauresh2798 | 0:c359fee24b6c | 516 | { |
gauresh2798 | 0:c359fee24b6c | 517 | dataarr[i]=token; |
gauresh2798 | 0:c359fee24b6c | 518 | i++; |
gauresh2798 | 0:c359fee24b6c | 519 | token = strtok(NULL, s); |
gauresh2798 | 0:c359fee24b6c | 520 | } |
gauresh2798 | 0:c359fee24b6c | 521 | |
gauresh2798 | 0:c359fee24b6c | 522 | if(part==1){temp=dataarr[0];} |
gauresh2798 | 0:c359fee24b6c | 523 | else if(part==2){temp=dataarr[1];} |
gauresh2798 | 0:c359fee24b6c | 524 | return temp; |
gauresh2798 | 0:c359fee24b6c | 525 | |
gauresh2798 | 0:c359fee24b6c | 526 | } |
gauresh2798 | 0:c359fee24b6c | 527 | void newcre(char webstring[]){ |
gauresh2798 | 0:c359fee24b6c | 528 | // char webstring[]="http://192.168.4.1:8266/ssid=hellogfg&pwd=thisispwd"; |
gauresh2798 | 0:c359fee24b6c | 529 | char *pass_l,*ssid_temp,*pass_temp; |
gauresh2798 | 0:c359fee24b6c | 530 | getreq = strchr(webstring, ch); |
gauresh2798 | 0:c359fee24b6c | 531 | pc.printf("\n\r Desired substring: %s\n\r", getreq); |
gauresh2798 | 0:c359fee24b6c | 532 | |
gauresh2798 | 0:c359fee24b6c | 533 | left = getname(getreq,"&",1); |
gauresh2798 | 0:c359fee24b6c | 534 | right = getname(getreq,"&",2); |
gauresh2798 | 0:c359fee24b6c | 535 | ssid_temp=getname(left,"=",2); |
gauresh2798 | 0:c359fee24b6c | 536 | pass_l=getname(right,"=",2); |
gauresh2798 | 0:c359fee24b6c | 537 | pass_temp=getname(pass_l," ",1); |
gauresh2798 | 0:c359fee24b6c | 538 | pc.printf( "\n\r new ssid is %s\n", ssid_temp ); |
gauresh2798 | 0:c359fee24b6c | 539 | pc.printf( "\n\r new password is %s\n", pass_temp ); |
gauresh2798 | 2:d5c9337fdce1 | 540 | if(strlen(ssid_temp)>4 && strlen(pass_temp)>4){ |
gauresh2798 | 1:52cad7057640 | 541 | get_ssid_pass(ssid_temp,pass_temp,8266); |
gauresh2798 | 1:52cad7057640 | 542 | // wait(5); |
gauresh2798 | 1:52cad7057640 | 543 | closeserver_AT(); |
gauresh2798 | 1:52cad7057640 | 544 | modeselector=10; |
gauresh2798 | 3:a0b26799134f | 545 | StationConfig_SSID_NEED(); |
gauresh2798 | 1:52cad7057640 | 546 | request_fetch_station(); |
gauresh2798 | 2:d5c9337fdce1 | 547 | } |
gauresh2798 | 3:a0b26799134f | 548 | } |
gauresh2798 | 3:a0b26799134f | 549 | |
gauresh2798 | 3:a0b26799134f | 550 | |
gauresh2798 | 3:a0b26799134f | 551 | |
gauresh2798 | 3:a0b26799134f | 552 | char* rtrim(char* string, char junk) |
gauresh2798 | 3:a0b26799134f | 553 | { |
gauresh2798 | 3:a0b26799134f | 554 | char* original = string + strlen(string); |
gauresh2798 | 3:a0b26799134f | 555 | while(*--original == junk); |
gauresh2798 | 3:a0b26799134f | 556 | *(original + 1) = '\0'; |
gauresh2798 | 3:a0b26799134f | 557 | return string; |
gauresh2798 | 3:a0b26799134f | 558 | } |
gauresh2798 | 3:a0b26799134f | 559 | char* ltrim(char *string, char junk) |
gauresh2798 | 3:a0b26799134f | 560 | { |
gauresh2798 | 3:a0b26799134f | 561 | char* original = string; |
gauresh2798 | 3:a0b26799134f | 562 | char *p = original; |
gauresh2798 | 3:a0b26799134f | 563 | int trimmed = 0; |
gauresh2798 | 3:a0b26799134f | 564 | do |
gauresh2798 | 3:a0b26799134f | 565 | { |
gauresh2798 | 3:a0b26799134f | 566 | if (*original != junk || trimmed) |
gauresh2798 | 3:a0b26799134f | 567 | { |
gauresh2798 | 3:a0b26799134f | 568 | trimmed = 1; |
gauresh2798 | 3:a0b26799134f | 569 | *p++ = *original; |
gauresh2798 | 3:a0b26799134f | 570 | } |
gauresh2798 | 3:a0b26799134f | 571 | } |
gauresh2798 | 3:a0b26799134f | 572 | while (*original++ != '\0'); |
gauresh2798 | 3:a0b26799134f | 573 | return string; |
gauresh2798 | 3:a0b26799134f | 574 | } |
gauresh2798 | 3:a0b26799134f | 575 | void data_sep_ssid(char replyback[],char ch){ |
gauresh2798 | 3:a0b26799134f | 576 | |
gauresh2798 | 3:a0b26799134f | 577 | |
gauresh2798 | 3:a0b26799134f | 578 | char *reply; |
gauresh2798 | 3:a0b26799134f | 579 | reply = strchr(replyback, ch); |
gauresh2798 | 3:a0b26799134f | 580 | pad_string = getname(reply,",",1); |
gauresh2798 | 3:a0b26799134f | 581 | printf( "\n\r ssid is %s\n\r", pad_string ); |
gauresh2798 | 3:a0b26799134f | 582 | |
gauresh2798 | 3:a0b26799134f | 583 | |
gauresh2798 | 3:a0b26799134f | 584 | } |
gauresh2798 | 3:a0b26799134f | 585 | void verifyssid(void){ |
gauresh2798 | 3:a0b26799134f | 586 | char *newd,*readb; |
gauresh2798 | 3:a0b26799134f | 587 | |
gauresh2798 | 3:a0b26799134f | 588 | //GET INITIAL ACCESS |
gauresh2798 | 3:a0b26799134f | 589 | ATReset(); |
gauresh2798 | 3:a0b26799134f | 590 | StationModeSet(); |
gauresh2798 | 3:a0b26799134f | 591 | pc.printf("++++++++++++++++++++++++ INITIAL Listing Access ESP ++++++++++++++++++++++\r\n"); |
gauresh2798 | 3:a0b26799134f | 592 | strcpy(cmdbuff,"AT+CWLAP\r\n"); |
gauresh2798 | 3:a0b26799134f | 593 | timeout=8000; |
gauresh2798 | 3:a0b26799134f | 594 | getcount=1000; |
gauresh2798 | 3:a0b26799134f | 595 | SendCMD(); |
gauresh2798 | 3:a0b26799134f | 596 | getreply(); |
gauresh2798 | 3:a0b26799134f | 597 | pc.printf("%s\r\n",replybuff); |
gauresh2798 | 3:a0b26799134f | 598 | newd=replybuff; |
gauresh2798 | 3:a0b26799134f | 599 | //RECEIVE STORED SSID |
gauresh2798 | 3:a0b26799134f | 600 | disp_ssid(); |
gauresh2798 | 3:a0b26799134f | 601 | int mo; |
gauresh2798 | 3:a0b26799134f | 602 | if (strstr(replybuff, "No AP") != NULL){ |
gauresh2798 | 3:a0b26799134f | 603 | //DEVICE NEWLY INSTALLED |
gauresh2798 | 3:a0b26799134f | 604 | pc.printf("++++++++++++++++++++++++ DEVICE NEWLY INSTALLED -> REGISTERATION ++++++++++++++++++++++\r\n"); |
gauresh2798 | 3:a0b26799134f | 605 | mo=startserver(); |
gauresh2798 | 3:a0b26799134f | 606 | t2.reset();t2.start(); |
gauresh2798 | 3:a0b26799134f | 607 | ApConfig(); |
gauresh2798 | 3:a0b26799134f | 608 | } |
gauresh2798 | 3:a0b26799134f | 609 | else{ |
gauresh2798 | 3:a0b26799134f | 610 | data_sep_ssid(replybuff,'"'); |
gauresh2798 | 3:a0b26799134f | 611 | printf("The final ssid is %s\n\r", ltrim(rtrim( pad_string, '"'), '"')); |
gauresh2798 | 3:a0b26799134f | 612 | readb=ltrim(rtrim( pad_string, '"'), '"'); |
gauresh2798 | 3:a0b26799134f | 613 | |
gauresh2798 | 3:a0b26799134f | 614 | |
gauresh2798 | 3:a0b26799134f | 615 | if (strstr(newd, readb) != NULL) { |
gauresh2798 | 3:a0b26799134f | 616 | pc.printf("++++++++++++++++++++++++ SSID PRESENT ++++++++++++++++++++++\r\n"); |
gauresh2798 | 3:a0b26799134f | 617 | get_ssid_pass(readb,"",8266); |
gauresh2798 | 3:a0b26799134f | 618 | // AP_LOGIN=30; |
gauresh2798 | 3:a0b26799134f | 619 | mo=startserver(); |
gauresh2798 | 3:a0b26799134f | 620 | t2.reset();t2.start(); |
gauresh2798 | 3:a0b26799134f | 621 | StationConfig_NO_SSID_NEED(); |
gauresh2798 | 3:a0b26799134f | 622 | |
gauresh2798 | 3:a0b26799134f | 623 | modeselector=10; |
gauresh2798 | 3:a0b26799134f | 624 | } |
gauresh2798 | 3:a0b26799134f | 625 | |
gauresh2798 | 3:a0b26799134f | 626 | } |
gauresh2798 | 0:c359fee24b6c | 627 | } |