Victor Dang
/
ESP8266_Wifi
my repo
main.cpp@3:214db5f41e07, 2019-03-22 (annotated)
- Committer:
- Victordang
- Date:
- Fri Mar 22 06:00:48 2019 +0000
- Revision:
- 3:214db5f41e07
- Parent:
- 2:3704512ed2d4
- Child:
- 4:d8270d048cc7
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Victordang | 2:3704512ed2d4 | 1 | // ESP-WROOM-02 Static page WEB server to control Mbed |
Victordang | 2:3704512ed2d4 | 2 | #include "mbed.h" |
Victordang | 2:3704512ed2d4 | 3 | |
Victordang | 0:c265e9b31b1d | 4 | |
Victordang | 0:c265e9b31b1d | 5 | Serial pc(USBTX, USBRX); |
Victordang | 2:3704512ed2d4 | 6 | Serial wroom(p28, p27); // tx, rx |
Victordang | 2:3704512ed2d4 | 7 | |
Victordang | 2:3704512ed2d4 | 8 | |
Victordang | 2:3704512ed2d4 | 9 | |
Victordang | 2:3704512ed2d4 | 10 | // Standard Mbed LED definitions |
Victordang | 2:3704512ed2d4 | 11 | DigitalOut led1(LED1); |
Victordang | 2:3704512ed2d4 | 12 | DigitalOut led2(LED2); |
Victordang | 2:3704512ed2d4 | 13 | DigitalOut led3(LED3); |
Victordang | 0:c265e9b31b1d | 14 | |
Victordang | 2:3704512ed2d4 | 15 | |
Victordang | 2:3704512ed2d4 | 16 | DigitalOut reset(p26); |
Victordang | 2:3704512ed2d4 | 17 | |
Victordang | 2:3704512ed2d4 | 18 | Timer t1; |
Victordang | 2:3704512ed2d4 | 19 | Timer t2; |
Victordang | 2:3704512ed2d4 | 20 | //////// |
Victordang | 2:3704512ed2d4 | 21 | |
Victordang | 2:3704512ed2d4 | 22 | char ssid[32] = "ssid"; // enter WiFi router ssid inside the quotes |
Victordang | 2:3704512ed2d4 | 23 | char pwd [32] = "password"; // enter WiFi router password inside the quotes |
Victordang | 0:c265e9b31b1d | 24 | char str[255]; |
Victordang | 2:3704512ed2d4 | 25 | void WROOMconfig(); |
Victordang | 2:3704512ed2d4 | 26 | /////// |
Victordang | 2:3704512ed2d4 | 27 | struct tm t; |
Victordang | 0:c265e9b31b1d | 28 | |
Victordang | 2:3704512ed2d4 | 29 | int bufflen, DataRX, counter, getcount, replycount, servreq, timeout; |
Victordang | 2:3704512ed2d4 | 30 | int bufl, ipdLen, linkID, weberror; |
Victordang | 2:3704512ed2d4 | 31 | |
Victordang | 2:3704512ed2d4 | 32 | char temp[10]; |
Victordang | 2:3704512ed2d4 | 33 | //char webcount[8]; |
Victordang | 2:3704512ed2d4 | 34 | char type[16]; |
Victordang | 2:3704512ed2d4 | 35 | char type1[16]; |
Victordang | 2:3704512ed2d4 | 36 | char channel[2]; |
Victordang | 2:3704512ed2d4 | 37 | char cmdbuff[32]; |
Victordang | 2:3704512ed2d4 | 38 | char replybuff[1024]; |
Victordang | 2:3704512ed2d4 | 39 | char webdata[1024]; // This may need to be bigger depending on WEB browser used |
Victordang | 2:3704512ed2d4 | 40 | char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added |
Victordang | 2:3704512ed2d4 | 41 | |
Victordang | 2:3704512ed2d4 | 42 | void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); |
Victordang | 2:3704512ed2d4 | 43 | |
Victordang | 0:c265e9b31b1d | 44 | |
Victordang | 2:3704512ed2d4 | 45 | |
Victordang | 2:3704512ed2d4 | 46 | |
Victordang | 2:3704512ed2d4 | 47 | int port =80; // set server port |
Victordang | 2:3704512ed2d4 | 48 | int SERVtimeout =5; // set server timeout in seconds in case link breaks. |
Victordang | 0:c265e9b31b1d | 49 | |
Victordang | 2:3704512ed2d4 | 50 | // Serial Interrupt read ESP data |
Victordang | 2:3704512ed2d4 | 51 | void callback() |
Victordang | 2:3704512ed2d4 | 52 | { |
Victordang | 2:3704512ed2d4 | 53 | //led3=1; |
Victordang | 2:3704512ed2d4 | 54 | while (wroom.readable()) { |
Victordang | 2:3704512ed2d4 | 55 | webbuff[counter] = wroom.getc(); |
Victordang | 2:3704512ed2d4 | 56 | counter++; |
Victordang | 2:3704512ed2d4 | 57 | } |
Victordang | 2:3704512ed2d4 | 58 | if(strlen(webbuff)>bufflen) { |
Victordang | 2:3704512ed2d4 | 59 | DataRX=1; |
Victordang | 2:3704512ed2d4 | 60 | //led3=0; |
Victordang | 2:3704512ed2d4 | 61 | } |
Victordang | 2:3704512ed2d4 | 62 | } |
Victordang | 0:c265e9b31b1d | 63 | |
Victordang | 0:c265e9b31b1d | 64 | int main() |
Victordang | 0:c265e9b31b1d | 65 | { |
Victordang | 2:3704512ed2d4 | 66 | reset=0; |
Victordang | 2:3704512ed2d4 | 67 | pc.baud(115200); |
Victordang | 2:3704512ed2d4 | 68 | pc.printf("\f\n\r------------ ESP-WROOM-02 Hardware Reset --------------\n\r"); |
Victordang | 0:c265e9b31b1d | 69 | wait(0.5); |
Victordang | 0:c265e9b31b1d | 70 | reset=1; |
Victordang | 2:3704512ed2d4 | 71 | led1=0,led2=0,led3=0; |
Victordang | 2:3704512ed2d4 | 72 | timeout=10; |
Victordang | 2:3704512ed2d4 | 73 | getcount=10; |
Victordang | 0:c265e9b31b1d | 74 | getreply(); |
Victordang | 2:3704512ed2d4 | 75 | wroom.baud(115200); // ESP-WROOM-02 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F |
Victordang | 2:3704512ed2d4 | 76 | WROOMconfig(); |
Victordang | 2:3704512ed2d4 | 77 | |
Victordang | 2:3704512ed2d4 | 78 | |
Victordang | 2:3704512ed2d4 | 79 | startserver(); |
Victordang | 0:c265e9b31b1d | 80 | |
Victordang | 0:c265e9b31b1d | 81 | while(1) { |
Victordang | 2:3704512ed2d4 | 82 | if(DataRX==1) { |
Victordang | 2:3704512ed2d4 | 83 | ReadWebData(); |
Victordang | 2:3704512ed2d4 | 84 | |
Victordang | 2:3704512ed2d4 | 85 | if (servreq == 1 && weberror == 0) { |
Victordang | 2:3704512ed2d4 | 86 | sendpage(); |
Victordang | 2:3704512ed2d4 | 87 | } |
Victordang | 2:3704512ed2d4 | 88 | wroom.attach(&callback); |
Victordang | 2:3704512ed2d4 | 89 | 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); |
Victordang | 2:3704512ed2d4 | 90 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
Victordang | 2:3704512ed2d4 | 91 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
Victordang | 2:3704512ed2d4 | 92 | pc.printf(" -------------------------------------\n\n"); |
Victordang | 2:3704512ed2d4 | 93 | //strcpy(lasthit, timebuf); |
Victordang | 2:3704512ed2d4 | 94 | servreq=0; |
Victordang | 2:3704512ed2d4 | 95 | } |
Victordang | 2:3704512ed2d4 | 96 | } |
Victordang | 2:3704512ed2d4 | 97 | } |
Victordang | 2:3704512ed2d4 | 98 | // Static WEB page |
Victordang | 2:3704512ed2d4 | 99 | void sendpage() |
Victordang | 2:3704512ed2d4 | 100 | { |
Victordang | 2:3704512ed2d4 | 101 | |
Victordang | 2:3704512ed2d4 | 102 | // WEB page data |
Victordang | 2:3704512ed2d4 | 103 | strcpy(webbuff, "<!DOCTYPE html>"); |
Victordang | 2:3704512ed2d4 | 104 | strcat(webbuff, "<html><head><title>IISEC - Matsui Lab</title><meta charset=\"UTF-8\"></head>"); |
Victordang | 2:3704512ed2d4 | 105 | strcat(webbuff, "<body>"); |
Victordang | 3:214db5f41e07 | 106 | strcat(webbuff, "<div style=\"text-align:center; background-color:#FBFEFC; color:#040712;\"><h1>情報セキュリティ大学院 <br> 松井研 - 2019</h1>"); |
Victordang | 2:3704512ed2d4 | 107 | strcat(webbuff, "<br><h1>ESP-WROOM-02 and LPC7168 Webサーバー</h1>"); |
Victordang | 3:214db5f41e07 | 108 | strcat(webbuff, "</div><br /><hr><hr>\r\n"); |
Victordang | 2:3704512ed2d4 | 109 | |
Victordang | 2:3704512ed2d4 | 110 | strcat(webbuff, "<p><div align=\"center\"><form method=\"POST\"><strong>"); |
Victordang | 2:3704512ed2d4 | 111 | if(led1==0) { |
Victordang | 2:3704512ed2d4 | 112 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> LED 1 off"); |
Victordang | 2:3704512ed2d4 | 113 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > LED 1 on"); |
Victordang | 2:3704512ed2d4 | 114 | } else { |
Victordang | 2:3704512ed2d4 | 115 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > LED 1 off"); |
Victordang | 2:3704512ed2d4 | 116 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> LED 1 on"); |
Victordang | 2:3704512ed2d4 | 117 | } |
Victordang | 2:3704512ed2d4 | 118 | if(led2==0) { |
Victordang | 2:3704512ed2d4 | 119 | strcat(webbuff, "<p><input type=\"radio\" name=\"led2\" value=\"0\" checked> LED 2 off"); |
Victordang | 2:3704512ed2d4 | 120 | strcat(webbuff, "<br><input type=\"radio\" name=\"led2\" value=\"1\" > LED 2 on"); |
Victordang | 2:3704512ed2d4 | 121 | } else { |
Victordang | 2:3704512ed2d4 | 122 | strcat(webbuff, "<p><input type=\"radio\" name=\"led2\" value=\"0\" > LED 2 off"); |
Victordang | 2:3704512ed2d4 | 123 | strcat(webbuff, "<br><input type=\"radio\" name=\"led2\" value=\"1\" checked> LED 2 on"); |
Victordang | 2:3704512ed2d4 | 124 | } |
Victordang | 2:3704512ed2d4 | 125 | if(led3==0) { |
Victordang | 2:3704512ed2d4 | 126 | strcat(webbuff, "<p><input type=\"radio\" name=\"led3\" value=\"0\" checked> LED 3 off"); |
Victordang | 2:3704512ed2d4 | 127 | strcat(webbuff, "<br><input type=\"radio\" name=\"led3\" value=\"1\" > LED 3 on"); |
Victordang | 2:3704512ed2d4 | 128 | } else { |
Victordang | 2:3704512ed2d4 | 129 | strcat(webbuff, "<p><input type=\"radio\" name=\"led3\" value=\"0\" > LED 3 off"); |
Victordang | 2:3704512ed2d4 | 130 | strcat(webbuff, "<br><input type=\"radio\" name=\"led3\" value=\"1\" checked> LED 3 on"); |
Victordang | 2:3704512ed2d4 | 131 | } |
Victordang | 2:3704512ed2d4 | 132 | |
Victordang | 3:214db5f41e07 | 133 | strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send\" style=\"background: #FDC03C;"); |
Victordang | 3:214db5f41e07 | 134 | strcat(webbuff, "background-image:-webkit-linear-gradient(top, #FDC03C, #FDD43C);"); |
Victordang | 3:214db5f41e07 | 135 | strcat(webbuff, "background-image:linear-gradient(to bottom, #FDC03C, #FDD43C);"); |
Victordang | 3:214db5f41e07 | 136 | strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#000000;font-size:20px;padding:"); |
Victordang | 2:3704512ed2d4 | 137 | strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); |
Victordang | 3:214db5f41e07 | 138 | strcat(webbuff, "background: #FD9A3C;"); |
Victordang | 3:214db5f41e07 | 139 | strcat(webbuff, "background-image:-webkit-linear-gradient(top,#FD9A3C,#8A7E1A);"); |
Victordang | 3:214db5f41e07 | 140 | strcat(webbuff, "background-image:linear-gradient(to bottom,#FD9A3C,#8A7E1A);"); |
Victordang | 2:3704512ed2d4 | 141 | strcat(webbuff, "text-decoration:none;\"></form></div></span>"); |
Victordang | 2:3704512ed2d4 | 142 | strcat(webbuff, "</body></html>"); |
Victordang | 2:3704512ed2d4 | 143 | // end of WEB page data |
Victordang | 2:3704512ed2d4 | 144 | bufl = strlen(webbuff); // get total page buffer length |
Victordang | 2:3704512ed2d4 | 145 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length and store all into cmdbuff |
Victordang | 2:3704512ed2d4 | 146 | timeout=200; |
Victordang | 2:3704512ed2d4 | 147 | getcount=7; |
Victordang | 2:3704512ed2d4 | 148 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 149 | getreply(); |
Victordang | 2:3704512ed2d4 | 150 | SendWEB(); // send web page |
Victordang | 2:3704512ed2d4 | 151 | memset(webbuff, '\0', sizeof(webbuff)); |
Victordang | 2:3704512ed2d4 | 152 | sendcheck(); |
Victordang | 2:3704512ed2d4 | 153 | } |
Victordang | 2:3704512ed2d4 | 154 | |
Victordang | 2:3704512ed2d4 | 155 | // wait for ESP-WROOM "SEND OK" reply, then close IP to load web page |
Victordang | 2:3704512ed2d4 | 156 | void sendcheck() |
Victordang | 2:3704512ed2d4 | 157 | { |
Victordang | 2:3704512ed2d4 | 158 | weberror=1; |
Victordang | 2:3704512ed2d4 | 159 | timeout=500; |
Victordang | 2:3704512ed2d4 | 160 | getcount=24; |
Victordang | 2:3704512ed2d4 | 161 | t2.reset(); |
Victordang | 2:3704512ed2d4 | 162 | t2.start(); |
Victordang | 2:3704512ed2d4 | 163 | while(weberror==1 && t2.read() <5) { |
Victordang | 0:c265e9b31b1d | 164 | getreply(); |
Victordang | 2:3704512ed2d4 | 165 | if (strstr(replybuff, "SEND OK") != NULL) { |
Victordang | 2:3704512ed2d4 | 166 | weberror=0; // wait for valid SEND OK |
Victordang | 2:3704512ed2d4 | 167 | } |
Victordang | 2:3704512ed2d4 | 168 | } |
Victordang | 2:3704512ed2d4 | 169 | if(weberror==1) { // restart connection |
Victordang | 2:3704512ed2d4 | 170 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
Victordang | 2:3704512ed2d4 | 171 | timeout=500; |
Victordang | 2:3704512ed2d4 | 172 | getcount=10; |
Victordang | 0:c265e9b31b1d | 173 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 174 | getreply(); |
Victordang | 2:3704512ed2d4 | 175 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 176 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
Victordang | 2:3704512ed2d4 | 177 | timeout=500; |
Victordang | 2:3704512ed2d4 | 178 | getcount=10; |
Victordang | 2:3704512ed2d4 | 179 | SendCMD(); |
Victordang | 0:c265e9b31b1d | 180 | getreply(); |
Victordang | 2:3704512ed2d4 | 181 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 182 | } else { |
Victordang | 2:3704512ed2d4 | 183 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
Victordang | 2:3704512ed2d4 | 184 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 185 | getreply(); |
Victordang | 2:3704512ed2d4 | 186 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 187 | } |
Victordang | 2:3704512ed2d4 | 188 | t2.reset(); |
Victordang | 0:c265e9b31b1d | 189 | } |
Victordang | 0:c265e9b31b1d | 190 | |
Victordang | 2:3704512ed2d4 | 191 | // Reads and processes GET and POST web data |
Victordang | 2:3704512ed2d4 | 192 | void ReadWebData() |
Victordang | 0:c265e9b31b1d | 193 | { |
Victordang | 2:3704512ed2d4 | 194 | wait_ms(200); |
Victordang | 2:3704512ed2d4 | 195 | wroom.attach(NULL); |
Victordang | 2:3704512ed2d4 | 196 | counter=0; |
Victordang | 2:3704512ed2d4 | 197 | DataRX=0; |
Victordang | 2:3704512ed2d4 | 198 | weberror=0; |
Victordang | 2:3704512ed2d4 | 199 | memset(webdata, '\0', sizeof(webdata)); |
Victordang | 2:3704512ed2d4 | 200 | int x = strcspn (webbuff,"+"); |
Victordang | 2:3704512ed2d4 | 201 | if(x) { |
Victordang | 2:3704512ed2d4 | 202 | strcpy(webdata, webbuff + x); |
Victordang | 2:3704512ed2d4 | 203 | weberror=0; |
Victordang | 2:3704512ed2d4 | 204 | //int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); // read linkID, ipdLen, type from webdata |
Victordang | 2:3704512ed2d4 | 205 | sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); // read linkID, ipdLen, type from webdata |
Victordang | 2:3704512ed2d4 | 206 | if( strstr(webdata, "led1=1") != NULL ) { |
Victordang | 2:3704512ed2d4 | 207 | led1=1; |
Victordang | 2:3704512ed2d4 | 208 | } |
Victordang | 2:3704512ed2d4 | 209 | if( strstr(webdata, "led1=0") != NULL ) { |
Victordang | 2:3704512ed2d4 | 210 | led1=0; |
Victordang | 2:3704512ed2d4 | 211 | } |
Victordang | 2:3704512ed2d4 | 212 | if( strstr(webdata, "led2=1") != NULL ) { |
Victordang | 2:3704512ed2d4 | 213 | led2=1; |
Victordang | 2:3704512ed2d4 | 214 | } |
Victordang | 2:3704512ed2d4 | 215 | if( strstr(webdata, "led2=0") != NULL ) { |
Victordang | 2:3704512ed2d4 | 216 | led2=0; |
Victordang | 2:3704512ed2d4 | 217 | } |
Victordang | 2:3704512ed2d4 | 218 | if( strstr(webdata, "led3=1") != NULL ) { |
Victordang | 2:3704512ed2d4 | 219 | led3=1; |
Victordang | 2:3704512ed2d4 | 220 | } |
Victordang | 2:3704512ed2d4 | 221 | if( strstr(webdata, "led3=0") != NULL ) { |
Victordang | 2:3704512ed2d4 | 222 | led3=0; |
Victordang | 2:3704512ed2d4 | 223 | } |
Victordang | 2:3704512ed2d4 | 224 | |
Victordang | 2:3704512ed2d4 | 225 | sprintf(channel, "%d",linkID); |
Victordang | 2:3704512ed2d4 | 226 | if (strstr(webdata, "GET") != NULL) { |
Victordang | 2:3704512ed2d4 | 227 | servreq=1; |
Victordang | 2:3704512ed2d4 | 228 | } |
Victordang | 2:3704512ed2d4 | 229 | if (strstr(webdata, "POST") != NULL) { |
Victordang | 2:3704512ed2d4 | 230 | servreq=1; |
Victordang | 2:3704512ed2d4 | 231 | } |
Victordang | 2:3704512ed2d4 | 232 | //webcounter++; |
Victordang | 2:3704512ed2d4 | 233 | //sprintf(webcount, "%d",webcounter); |
Victordang | 2:3704512ed2d4 | 234 | } else { |
Victordang | 2:3704512ed2d4 | 235 | memset(webbuff, '\0', sizeof(webbuff)); |
Victordang | 2:3704512ed2d4 | 236 | wroom.attach(&callback); |
Victordang | 2:3704512ed2d4 | 237 | weberror=1; |
Victordang | 2:3704512ed2d4 | 238 | } |
Victordang | 0:c265e9b31b1d | 239 | } |
Victordang | 2:3704512ed2d4 | 240 | // Starts and restarts webserver if errors detected. |
Victordang | 2:3704512ed2d4 | 241 | void startserver() |
Victordang | 0:c265e9b31b1d | 242 | { |
Victordang | 2:3704512ed2d4 | 243 | |
Victordang | 2:3704512ed2d4 | 244 | if (strstr(replybuff, "OK") != NULL) { |
Victordang | 2:3704512ed2d4 | 245 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
Victordang | 2:3704512ed2d4 | 246 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
Victordang | 2:3704512ed2d4 | 247 | timeout=500; |
Victordang | 2:3704512ed2d4 | 248 | getcount=20; |
Victordang | 2:3704512ed2d4 | 249 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 250 | getreply(); |
Victordang | 2:3704512ed2d4 | 251 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 252 | wait(1); |
Victordang | 2:3704512ed2d4 | 253 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
Victordang | 2:3704512ed2d4 | 254 | timeout=500; |
Victordang | 2:3704512ed2d4 | 255 | getcount=50; |
Victordang | 2:3704512ed2d4 | 256 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 257 | getreply(); |
Victordang | 2:3704512ed2d4 | 258 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 259 | wait(5); |
Victordang | 2:3704512ed2d4 | 260 | pc.printf("\n Getting Server IP \r\n"); |
Victordang | 2:3704512ed2d4 | 261 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
Victordang | 2:3704512ed2d4 | 262 | timeout=2500; |
Victordang | 2:3704512ed2d4 | 263 | getcount=200; |
Victordang | 2:3704512ed2d4 | 264 | while(weberror==0) { |
Victordang | 2:3704512ed2d4 | 265 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 266 | getreply(); |
Victordang | 2:3704512ed2d4 | 267 | if (strstr(replybuff, "0.0.0.0") == NULL) { |
Victordang | 2:3704512ed2d4 | 268 | weberror=1; // wait for valid IP |
Victordang | 2:3704512ed2d4 | 269 | } |
Victordang | 2:3704512ed2d4 | 270 | } |
Victordang | 2:3704512ed2d4 | 271 | pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n"); |
Victordang | 2:3704512ed2d4 | 272 | pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n"); |
Victordang | 2:3704512ed2d4 | 273 | replybuff[strlen(replybuff)-1] = '\0'; |
Victordang | 2:3704512ed2d4 | 274 | sprintf(webdata,"%s", replybuff); |
Victordang | 2:3704512ed2d4 | 275 | pc.printf(webdata); |
Victordang | 2:3704512ed2d4 | 276 | led2=0; |
Victordang | 2:3704512ed2d4 | 277 | bufflen=200; |
Victordang | 2:3704512ed2d4 | 278 | counter=0; |
Victordang | 2:3704512ed2d4 | 279 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
Victordang | 2:3704512ed2d4 | 280 | wroom.attach(&callback); |
Victordang | 2:3704512ed2d4 | 281 | } else { |
Victordang | 2:3704512ed2d4 | 282 | pc.printf("\n++++++++++ ESP-WROOM-02 error, check power/connections ++++++++++\r\n"); |
Victordang | 2:3704512ed2d4 | 283 | while(1) {} |
Victordang | 2:3704512ed2d4 | 284 | } |
Victordang | 2:3704512ed2d4 | 285 | t2.reset(); |
Victordang | 2:3704512ed2d4 | 286 | t2.start(); |
Victordang | 2:3704512ed2d4 | 287 | |
Victordang | 2:3704512ed2d4 | 288 | } |
Victordang | 2:3704512ed2d4 | 289 | // ESP-WROOM-02 Command data send |
Victordang | 2:3704512ed2d4 | 290 | void SendCMD() |
Victordang | 2:3704512ed2d4 | 291 | { |
Victordang | 2:3704512ed2d4 | 292 | wroom.printf("%s", cmdbuff); |
Victordang | 2:3704512ed2d4 | 293 | } |
Victordang | 2:3704512ed2d4 | 294 | // Large WEB buffer data send |
Victordang | 2:3704512ed2d4 | 295 | void SendWEB() |
Victordang | 2:3704512ed2d4 | 296 | { |
Victordang | 2:3704512ed2d4 | 297 | int i=0; |
Victordang | 2:3704512ed2d4 | 298 | if(wroom.writeable()) { |
Victordang | 2:3704512ed2d4 | 299 | while(webbuff[i]!='\0') { |
Victordang | 2:3704512ed2d4 | 300 | wroom.putc(webbuff[i]); |
Victordang | 2:3704512ed2d4 | 301 | i++; |
Victordang | 2:3704512ed2d4 | 302 | } |
Victordang | 2:3704512ed2d4 | 303 | } |
Victordang | 2:3704512ed2d4 | 304 | } |
Victordang | 2:3704512ed2d4 | 305 | // Get Command and ESP-WROOM-02 status replies |
Victordang | 2:3704512ed2d4 | 306 | void getreply() |
Victordang | 2:3704512ed2d4 | 307 | { |
Victordang | 2:3704512ed2d4 | 308 | memset(replybuff, '\0', sizeof(replybuff)); |
Victordang | 2:3704512ed2d4 | 309 | t1.reset(); |
Victordang | 2:3704512ed2d4 | 310 | t1.start(); |
Victordang | 2:3704512ed2d4 | 311 | replycount=0; |
Victordang | 2:3704512ed2d4 | 312 | while(t1.read_ms()< timeout && replycount < getcount) { |
Victordang | 2:3704512ed2d4 | 313 | if(wroom.readable()) { |
Victordang | 2:3704512ed2d4 | 314 | replybuff[replycount] = wroom.getc(); |
Victordang | 2:3704512ed2d4 | 315 | replycount++; |
Victordang | 2:3704512ed2d4 | 316 | } |
Victordang | 2:3704512ed2d4 | 317 | } |
Victordang | 2:3704512ed2d4 | 318 | t1.stop(); |
Victordang | 2:3704512ed2d4 | 319 | } |
Victordang | 2:3704512ed2d4 | 320 | |
Victordang | 2:3704512ed2d4 | 321 | void WROOMconfig() |
Victordang | 2:3704512ed2d4 | 322 | { |
Victordang | 2:3704512ed2d4 | 323 | |
Victordang | 0:c265e9b31b1d | 324 | wait(5); |
Victordang | 2:3704512ed2d4 | 325 | strcpy(cmdbuff,"AT\r\n"); |
Victordang | 0:c265e9b31b1d | 326 | SendCMD(); |
Victordang | 0:c265e9b31b1d | 327 | wait(1); |
Victordang | 2:3704512ed2d4 | 328 | strcpy(cmdbuff,"AT\r\n"); |
Victordang | 0:c265e9b31b1d | 329 | SendCMD(); |
Victordang | 0:c265e9b31b1d | 330 | wait(1); |
Victordang | 2:3704512ed2d4 | 331 | |
Victordang | 2:3704512ed2d4 | 332 | strcpy(cmdbuff,"AT\r\n"); |
Victordang | 0:c265e9b31b1d | 333 | SendCMD(); |
Victordang | 0:c265e9b31b1d | 334 | timeout=1; |
Victordang | 0:c265e9b31b1d | 335 | getreply(); |
Victordang | 0:c265e9b31b1d | 336 | wait(1); |
Victordang | 2:3704512ed2d4 | 337 | pc.printf("\f---------- Starting ESP-WROOM-02 Config ----------\r\n\n"); |
Victordang | 0:c265e9b31b1d | 338 | |
Victordang | 2:3704512ed2d4 | 339 | |
Victordang | 0:c265e9b31b1d | 340 | wait(3); |
Victordang | 0:c265e9b31b1d | 341 | |
Victordang | 0:c265e9b31b1d | 342 | // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
Victordang | 0:c265e9b31b1d | 343 | pc.printf("\n---------- Setting Mode ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 344 | strcpy(cmdbuff, "AT+CWMODE=1\r\n"); |
Victordang | 0:c265e9b31b1d | 345 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 346 | timeout=100; |
Victordang | 0:c265e9b31b1d | 347 | getreply(); |
Victordang | 2:3704512ed2d4 | 348 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 349 | |
Victordang | 0:c265e9b31b1d | 350 | wait(2); |
Victordang | 0:c265e9b31b1d | 351 | |
Victordang | 0:c265e9b31b1d | 352 | // set CIPMUX to 0=Single,1=Multi |
Victordang | 0:c265e9b31b1d | 353 | pc.printf("\n---------- Setting Connection Mode ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 354 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
Victordang | 0:c265e9b31b1d | 355 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 356 | timeout=400; |
Victordang | 0:c265e9b31b1d | 357 | getreply(); |
Victordang | 2:3704512ed2d4 | 358 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 359 | |
Victordang | 0:c265e9b31b1d | 360 | wait(2); |
Victordang | 0:c265e9b31b1d | 361 | |
Victordang | 0:c265e9b31b1d | 362 | pc.printf("\n---------- Listing Access Points ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 363 | strcpy(cmdbuff, "AT+CWLAP\r\n"); |
Victordang | 0:c265e9b31b1d | 364 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 365 | timeout=9000; |
Victordang | 2:3704512ed2d4 | 366 | getcount=5000; |
Victordang | 0:c265e9b31b1d | 367 | getreply(); |
Victordang | 2:3704512ed2d4 | 368 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 369 | |
Victordang | 0:c265e9b31b1d | 370 | wait(2); |
Victordang | 0:c265e9b31b1d | 371 | |
Victordang | 0:c265e9b31b1d | 372 | pc.printf("\n---------- Connecting to AP ----------\r\n"); |
Victordang | 0:c265e9b31b1d | 373 | pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); |
Victordang | 2:3704512ed2d4 | 374 | strcpy(cmdbuff, "AT+CWJAP=\""); |
Victordang | 2:3704512ed2d4 | 375 | strcat(cmdbuff, ssid); |
Victordang | 2:3704512ed2d4 | 376 | strcat(cmdbuff, "\",\""); |
Victordang | 2:3704512ed2d4 | 377 | strcat(cmdbuff, pwd); |
Victordang | 2:3704512ed2d4 | 378 | strcat(cmdbuff, "\"\r\n"); |
Victordang | 0:c265e9b31b1d | 379 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 380 | timeout=9000; |
Victordang | 2:3704512ed2d4 | 381 | getcount=5000; |
Victordang | 0:c265e9b31b1d | 382 | getreply(); |
Victordang | 2:3704512ed2d4 | 383 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 384 | |
Victordang | 0:c265e9b31b1d | 385 | wait(5); |
Victordang | 0:c265e9b31b1d | 386 | |
Victordang | 0:c265e9b31b1d | 387 | pc.printf("\n---------- Get IP's ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 388 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
Victordang | 0:c265e9b31b1d | 389 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 390 | timeout=400; |
Victordang | 0:c265e9b31b1d | 391 | getreply(); |
Victordang | 2:3704512ed2d4 | 392 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 393 | |
Victordang | 0:c265e9b31b1d | 394 | wait(1); |
Victordang | 0:c265e9b31b1d | 395 | |
Victordang | 0:c265e9b31b1d | 396 | pc.printf("\n---------- Get Connection Status ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 397 | strcpy(cmdbuff, "AT+CIPSTATUS\r\n"); |
Victordang | 0:c265e9b31b1d | 398 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 399 | timeout=500; |
Victordang | 0:c265e9b31b1d | 400 | getreply(); |
Victordang | 2:3704512ed2d4 | 401 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 402 | pc.printf("ESP-WROOM saves the SSID and password settings internally\r\n"); |
Victordang | 0:c265e9b31b1d | 403 | wait(10); |
Victordang | 0:c265e9b31b1d | 404 | } |