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