Victor Dang
/
ESP8266_Wifi
my repo
main.cpp@2:3704512ed2d4, 2019-03-20 (annotated)
- Committer:
- Victordang
- Date:
- Wed Mar 20 04:46:53 2019 +0000
- Revision:
- 2:3704512ed2d4
- Parent:
- 1:cd76b77e35a4
- Child:
- 3:214db5f41e07
submit
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, "<html><head><title>IISEC - Matsui Lab</title></head>"); |
Victordang | 2:3704512ed2d4 | 106 | strcat(webbuff, "<body>"); |
Victordang | 2:3704512ed2d4 | 107 | //strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP-WROOM-02 and LPC7168 Web Server</h1>"); |
Victordang | 2:3704512ed2d4 | 108 | strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>情報セキュリティ大学院 <br> 松井研 - 2019</h1>"); |
Victordang | 2:3704512ed2d4 | 109 | strcat(webbuff, "<br><h1>ESP-WROOM-02 and LPC7168 Webサーバー</h1>"); |
Victordang | 2:3704512ed2d4 | 110 | strcat(webbuff, "</div><br /><hr>\r\n"); |
Victordang | 2:3704512ed2d4 | 111 | |
Victordang | 2:3704512ed2d4 | 112 | strcat(webbuff, "<p><div align=\"center\"><form method=\"POST\"><strong>"); |
Victordang | 2:3704512ed2d4 | 113 | if(led1==0) { |
Victordang | 2:3704512ed2d4 | 114 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> LED 1 off"); |
Victordang | 2:3704512ed2d4 | 115 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > LED 1 on"); |
Victordang | 2:3704512ed2d4 | 116 | } else { |
Victordang | 2:3704512ed2d4 | 117 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > LED 1 off"); |
Victordang | 2:3704512ed2d4 | 118 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> LED 1 on"); |
Victordang | 2:3704512ed2d4 | 119 | } |
Victordang | 2:3704512ed2d4 | 120 | if(led2==0) { |
Victordang | 2:3704512ed2d4 | 121 | strcat(webbuff, "<p><input type=\"radio\" name=\"led2\" value=\"0\" checked> LED 2 off"); |
Victordang | 2:3704512ed2d4 | 122 | strcat(webbuff, "<br><input type=\"radio\" name=\"led2\" value=\"1\" > LED 2 on"); |
Victordang | 2:3704512ed2d4 | 123 | } else { |
Victordang | 2:3704512ed2d4 | 124 | strcat(webbuff, "<p><input type=\"radio\" name=\"led2\" value=\"0\" > LED 2 off"); |
Victordang | 2:3704512ed2d4 | 125 | strcat(webbuff, "<br><input type=\"radio\" name=\"led2\" value=\"1\" checked> LED 2 on"); |
Victordang | 2:3704512ed2d4 | 126 | } |
Victordang | 2:3704512ed2d4 | 127 | if(led3==0) { |
Victordang | 2:3704512ed2d4 | 128 | strcat(webbuff, "<p><input type=\"radio\" name=\"led3\" value=\"0\" checked> LED 3 off"); |
Victordang | 2:3704512ed2d4 | 129 | strcat(webbuff, "<br><input type=\"radio\" name=\"led3\" value=\"1\" > LED 3 on"); |
Victordang | 2:3704512ed2d4 | 130 | } else { |
Victordang | 2:3704512ed2d4 | 131 | strcat(webbuff, "<p><input type=\"radio\" name=\"led3\" value=\"0\" > LED 3 off"); |
Victordang | 2:3704512ed2d4 | 132 | strcat(webbuff, "<br><input type=\"radio\" name=\"led3\" value=\"1\" checked> LED 3 on"); |
Victordang | 2:3704512ed2d4 | 133 | } |
Victordang | 2:3704512ed2d4 | 134 | |
Victordang | 2:3704512ed2d4 | 135 | strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send\" style=\"background: #3498db;"); |
Victordang | 2:3704512ed2d4 | 136 | strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);"); |
Victordang | 2:3704512ed2d4 | 137 | strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);"); |
Victordang | 2:3704512ed2d4 | 138 | strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:"); |
Victordang | 2:3704512ed2d4 | 139 | strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); |
Victordang | 2:3704512ed2d4 | 140 | strcat(webbuff, "background: #3cb0fd;"); |
Victordang | 2:3704512ed2d4 | 141 | strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); |
Victordang | 2:3704512ed2d4 | 142 | strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);"); |
Victordang | 2:3704512ed2d4 | 143 | strcat(webbuff, "text-decoration:none;\"></form></div></span>"); |
Victordang | 2:3704512ed2d4 | 144 | strcat(webbuff, "</body></html>"); |
Victordang | 2:3704512ed2d4 | 145 | // end of WEB page data |
Victordang | 2:3704512ed2d4 | 146 | bufl = strlen(webbuff); // get total page buffer length |
Victordang | 2:3704512ed2d4 | 147 | 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 | 148 | timeout=200; |
Victordang | 2:3704512ed2d4 | 149 | getcount=7; |
Victordang | 2:3704512ed2d4 | 150 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 151 | getreply(); |
Victordang | 2:3704512ed2d4 | 152 | SendWEB(); // send web page |
Victordang | 2:3704512ed2d4 | 153 | memset(webbuff, '\0', sizeof(webbuff)); |
Victordang | 2:3704512ed2d4 | 154 | sendcheck(); |
Victordang | 2:3704512ed2d4 | 155 | } |
Victordang | 2:3704512ed2d4 | 156 | |
Victordang | 2:3704512ed2d4 | 157 | // wait for ESP-WROOM "SEND OK" reply, then close IP to load web page |
Victordang | 2:3704512ed2d4 | 158 | void sendcheck() |
Victordang | 2:3704512ed2d4 | 159 | { |
Victordang | 2:3704512ed2d4 | 160 | weberror=1; |
Victordang | 2:3704512ed2d4 | 161 | timeout=500; |
Victordang | 2:3704512ed2d4 | 162 | getcount=24; |
Victordang | 2:3704512ed2d4 | 163 | t2.reset(); |
Victordang | 2:3704512ed2d4 | 164 | t2.start(); |
Victordang | 2:3704512ed2d4 | 165 | while(weberror==1 && t2.read() <5) { |
Victordang | 0:c265e9b31b1d | 166 | getreply(); |
Victordang | 2:3704512ed2d4 | 167 | if (strstr(replybuff, "SEND OK") != NULL) { |
Victordang | 2:3704512ed2d4 | 168 | weberror=0; // wait for valid SEND OK |
Victordang | 2:3704512ed2d4 | 169 | } |
Victordang | 2:3704512ed2d4 | 170 | } |
Victordang | 2:3704512ed2d4 | 171 | if(weberror==1) { // restart connection |
Victordang | 2:3704512ed2d4 | 172 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
Victordang | 2:3704512ed2d4 | 173 | timeout=500; |
Victordang | 2:3704512ed2d4 | 174 | getcount=10; |
Victordang | 0:c265e9b31b1d | 175 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 176 | getreply(); |
Victordang | 2:3704512ed2d4 | 177 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 178 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
Victordang | 2:3704512ed2d4 | 179 | timeout=500; |
Victordang | 2:3704512ed2d4 | 180 | getcount=10; |
Victordang | 2:3704512ed2d4 | 181 | SendCMD(); |
Victordang | 0:c265e9b31b1d | 182 | getreply(); |
Victordang | 2:3704512ed2d4 | 183 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 184 | } else { |
Victordang | 2:3704512ed2d4 | 185 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
Victordang | 2:3704512ed2d4 | 186 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 187 | getreply(); |
Victordang | 2:3704512ed2d4 | 188 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 189 | } |
Victordang | 2:3704512ed2d4 | 190 | t2.reset(); |
Victordang | 0:c265e9b31b1d | 191 | } |
Victordang | 0:c265e9b31b1d | 192 | |
Victordang | 2:3704512ed2d4 | 193 | // Reads and processes GET and POST web data |
Victordang | 2:3704512ed2d4 | 194 | void ReadWebData() |
Victordang | 0:c265e9b31b1d | 195 | { |
Victordang | 2:3704512ed2d4 | 196 | wait_ms(200); |
Victordang | 2:3704512ed2d4 | 197 | wroom.attach(NULL); |
Victordang | 2:3704512ed2d4 | 198 | counter=0; |
Victordang | 2:3704512ed2d4 | 199 | DataRX=0; |
Victordang | 2:3704512ed2d4 | 200 | weberror=0; |
Victordang | 2:3704512ed2d4 | 201 | memset(webdata, '\0', sizeof(webdata)); |
Victordang | 2:3704512ed2d4 | 202 | int x = strcspn (webbuff,"+"); |
Victordang | 2:3704512ed2d4 | 203 | if(x) { |
Victordang | 2:3704512ed2d4 | 204 | strcpy(webdata, webbuff + x); |
Victordang | 2:3704512ed2d4 | 205 | weberror=0; |
Victordang | 2:3704512ed2d4 | 206 | //int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); // read linkID, ipdLen, type from webdata |
Victordang | 2:3704512ed2d4 | 207 | sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); // read linkID, ipdLen, type from webdata |
Victordang | 2:3704512ed2d4 | 208 | if( strstr(webdata, "led1=1") != NULL ) { |
Victordang | 2:3704512ed2d4 | 209 | led1=1; |
Victordang | 2:3704512ed2d4 | 210 | } |
Victordang | 2:3704512ed2d4 | 211 | if( strstr(webdata, "led1=0") != NULL ) { |
Victordang | 2:3704512ed2d4 | 212 | led1=0; |
Victordang | 2:3704512ed2d4 | 213 | } |
Victordang | 2:3704512ed2d4 | 214 | if( strstr(webdata, "led2=1") != NULL ) { |
Victordang | 2:3704512ed2d4 | 215 | led2=1; |
Victordang | 2:3704512ed2d4 | 216 | } |
Victordang | 2:3704512ed2d4 | 217 | if( strstr(webdata, "led2=0") != NULL ) { |
Victordang | 2:3704512ed2d4 | 218 | led2=0; |
Victordang | 2:3704512ed2d4 | 219 | } |
Victordang | 2:3704512ed2d4 | 220 | if( strstr(webdata, "led3=1") != NULL ) { |
Victordang | 2:3704512ed2d4 | 221 | led3=1; |
Victordang | 2:3704512ed2d4 | 222 | } |
Victordang | 2:3704512ed2d4 | 223 | if( strstr(webdata, "led3=0") != NULL ) { |
Victordang | 2:3704512ed2d4 | 224 | led3=0; |
Victordang | 2:3704512ed2d4 | 225 | } |
Victordang | 2:3704512ed2d4 | 226 | |
Victordang | 2:3704512ed2d4 | 227 | sprintf(channel, "%d",linkID); |
Victordang | 2:3704512ed2d4 | 228 | if (strstr(webdata, "GET") != NULL) { |
Victordang | 2:3704512ed2d4 | 229 | servreq=1; |
Victordang | 2:3704512ed2d4 | 230 | } |
Victordang | 2:3704512ed2d4 | 231 | if (strstr(webdata, "POST") != NULL) { |
Victordang | 2:3704512ed2d4 | 232 | servreq=1; |
Victordang | 2:3704512ed2d4 | 233 | } |
Victordang | 2:3704512ed2d4 | 234 | //webcounter++; |
Victordang | 2:3704512ed2d4 | 235 | //sprintf(webcount, "%d",webcounter); |
Victordang | 2:3704512ed2d4 | 236 | } else { |
Victordang | 2:3704512ed2d4 | 237 | memset(webbuff, '\0', sizeof(webbuff)); |
Victordang | 2:3704512ed2d4 | 238 | wroom.attach(&callback); |
Victordang | 2:3704512ed2d4 | 239 | weberror=1; |
Victordang | 2:3704512ed2d4 | 240 | } |
Victordang | 0:c265e9b31b1d | 241 | } |
Victordang | 2:3704512ed2d4 | 242 | // Starts and restarts webserver if errors detected. |
Victordang | 2:3704512ed2d4 | 243 | void startserver() |
Victordang | 0:c265e9b31b1d | 244 | { |
Victordang | 2:3704512ed2d4 | 245 | |
Victordang | 2:3704512ed2d4 | 246 | if (strstr(replybuff, "OK") != NULL) { |
Victordang | 2:3704512ed2d4 | 247 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
Victordang | 2:3704512ed2d4 | 248 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections. |
Victordang | 2:3704512ed2d4 | 249 | timeout=500; |
Victordang | 2:3704512ed2d4 | 250 | getcount=20; |
Victordang | 2:3704512ed2d4 | 251 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 252 | getreply(); |
Victordang | 2:3704512ed2d4 | 253 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 254 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
Victordang | 2:3704512ed2d4 | 255 | timeout=500; |
Victordang | 2:3704512ed2d4 | 256 | getcount=20; |
Victordang | 2:3704512ed2d4 | 257 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 258 | getreply(); |
Victordang | 2:3704512ed2d4 | 259 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 260 | wait(1); |
Victordang | 2:3704512ed2d4 | 261 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
Victordang | 2:3704512ed2d4 | 262 | timeout=500; |
Victordang | 2:3704512ed2d4 | 263 | getcount=50; |
Victordang | 2:3704512ed2d4 | 264 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 265 | getreply(); |
Victordang | 2:3704512ed2d4 | 266 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 267 | wait(5); |
Victordang | 2:3704512ed2d4 | 268 | pc.printf("\n Getting Server IP \r\n"); |
Victordang | 2:3704512ed2d4 | 269 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
Victordang | 2:3704512ed2d4 | 270 | timeout=2500; |
Victordang | 2:3704512ed2d4 | 271 | getcount=200; |
Victordang | 2:3704512ed2d4 | 272 | while(weberror==0) { |
Victordang | 2:3704512ed2d4 | 273 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 274 | getreply(); |
Victordang | 2:3704512ed2d4 | 275 | if (strstr(replybuff, "0.0.0.0") == NULL) { |
Victordang | 2:3704512ed2d4 | 276 | weberror=1; // wait for valid IP |
Victordang | 2:3704512ed2d4 | 277 | } |
Victordang | 2:3704512ed2d4 | 278 | } |
Victordang | 2:3704512ed2d4 | 279 | pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n"); |
Victordang | 2:3704512ed2d4 | 280 | pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n"); |
Victordang | 2:3704512ed2d4 | 281 | replybuff[strlen(replybuff)-1] = '\0'; |
Victordang | 2:3704512ed2d4 | 282 | sprintf(webdata,"%s", replybuff); |
Victordang | 2:3704512ed2d4 | 283 | pc.printf(webdata); |
Victordang | 2:3704512ed2d4 | 284 | led2=0; |
Victordang | 2:3704512ed2d4 | 285 | bufflen=200; |
Victordang | 2:3704512ed2d4 | 286 | counter=0; |
Victordang | 2:3704512ed2d4 | 287 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
Victordang | 2:3704512ed2d4 | 288 | wroom.attach(&callback); |
Victordang | 2:3704512ed2d4 | 289 | } else { |
Victordang | 2:3704512ed2d4 | 290 | pc.printf("\n++++++++++ ESP-WROOM-02 error, check power/connections ++++++++++\r\n"); |
Victordang | 2:3704512ed2d4 | 291 | while(1) {} |
Victordang | 2:3704512ed2d4 | 292 | } |
Victordang | 2:3704512ed2d4 | 293 | t2.reset(); |
Victordang | 2:3704512ed2d4 | 294 | t2.start(); |
Victordang | 2:3704512ed2d4 | 295 | |
Victordang | 2:3704512ed2d4 | 296 | } |
Victordang | 2:3704512ed2d4 | 297 | // ESP-WROOM-02 Command data send |
Victordang | 2:3704512ed2d4 | 298 | void SendCMD() |
Victordang | 2:3704512ed2d4 | 299 | { |
Victordang | 2:3704512ed2d4 | 300 | wroom.printf("%s", cmdbuff); |
Victordang | 2:3704512ed2d4 | 301 | } |
Victordang | 2:3704512ed2d4 | 302 | // Large WEB buffer data send |
Victordang | 2:3704512ed2d4 | 303 | void SendWEB() |
Victordang | 2:3704512ed2d4 | 304 | { |
Victordang | 2:3704512ed2d4 | 305 | int i=0; |
Victordang | 2:3704512ed2d4 | 306 | if(wroom.writeable()) { |
Victordang | 2:3704512ed2d4 | 307 | while(webbuff[i]!='\0') { |
Victordang | 2:3704512ed2d4 | 308 | wroom.putc(webbuff[i]); |
Victordang | 2:3704512ed2d4 | 309 | i++; |
Victordang | 2:3704512ed2d4 | 310 | } |
Victordang | 2:3704512ed2d4 | 311 | } |
Victordang | 2:3704512ed2d4 | 312 | } |
Victordang | 2:3704512ed2d4 | 313 | // Get Command and ESP-WROOM-02 status replies |
Victordang | 2:3704512ed2d4 | 314 | void getreply() |
Victordang | 2:3704512ed2d4 | 315 | { |
Victordang | 2:3704512ed2d4 | 316 | memset(replybuff, '\0', sizeof(replybuff)); |
Victordang | 2:3704512ed2d4 | 317 | t1.reset(); |
Victordang | 2:3704512ed2d4 | 318 | t1.start(); |
Victordang | 2:3704512ed2d4 | 319 | replycount=0; |
Victordang | 2:3704512ed2d4 | 320 | while(t1.read_ms()< timeout && replycount < getcount) { |
Victordang | 2:3704512ed2d4 | 321 | if(wroom.readable()) { |
Victordang | 2:3704512ed2d4 | 322 | replybuff[replycount] = wroom.getc(); |
Victordang | 2:3704512ed2d4 | 323 | replycount++; |
Victordang | 2:3704512ed2d4 | 324 | } |
Victordang | 2:3704512ed2d4 | 325 | } |
Victordang | 2:3704512ed2d4 | 326 | t1.stop(); |
Victordang | 2:3704512ed2d4 | 327 | } |
Victordang | 2:3704512ed2d4 | 328 | |
Victordang | 2:3704512ed2d4 | 329 | void WROOMconfig() |
Victordang | 2:3704512ed2d4 | 330 | { |
Victordang | 2:3704512ed2d4 | 331 | |
Victordang | 0:c265e9b31b1d | 332 | wait(5); |
Victordang | 2:3704512ed2d4 | 333 | strcpy(cmdbuff,"AT\r\n"); |
Victordang | 0:c265e9b31b1d | 334 | SendCMD(); |
Victordang | 0:c265e9b31b1d | 335 | wait(1); |
Victordang | 2:3704512ed2d4 | 336 | strcpy(cmdbuff,"AT\r\n"); |
Victordang | 0:c265e9b31b1d | 337 | SendCMD(); |
Victordang | 0:c265e9b31b1d | 338 | wait(1); |
Victordang | 2:3704512ed2d4 | 339 | |
Victordang | 2:3704512ed2d4 | 340 | strcpy(cmdbuff,"AT\r\n"); |
Victordang | 0:c265e9b31b1d | 341 | SendCMD(); |
Victordang | 0:c265e9b31b1d | 342 | timeout=1; |
Victordang | 0:c265e9b31b1d | 343 | getreply(); |
Victordang | 0:c265e9b31b1d | 344 | wait(1); |
Victordang | 2:3704512ed2d4 | 345 | pc.printf("\f---------- Starting ESP-WROOM-02 Config ----------\r\n\n"); |
Victordang | 0:c265e9b31b1d | 346 | |
Victordang | 2:3704512ed2d4 | 347 | |
Victordang | 0:c265e9b31b1d | 348 | wait(3); |
Victordang | 0:c265e9b31b1d | 349 | |
Victordang | 0:c265e9b31b1d | 350 | // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
Victordang | 0:c265e9b31b1d | 351 | pc.printf("\n---------- Setting Mode ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 352 | strcpy(cmdbuff, "AT+CWMODE=1\r\n"); |
Victordang | 0:c265e9b31b1d | 353 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 354 | timeout=100; |
Victordang | 0:c265e9b31b1d | 355 | getreply(); |
Victordang | 2:3704512ed2d4 | 356 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 357 | |
Victordang | 0:c265e9b31b1d | 358 | wait(2); |
Victordang | 0:c265e9b31b1d | 359 | |
Victordang | 0:c265e9b31b1d | 360 | // set CIPMUX to 0=Single,1=Multi |
Victordang | 0:c265e9b31b1d | 361 | pc.printf("\n---------- Setting Connection Mode ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 362 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
Victordang | 0:c265e9b31b1d | 363 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 364 | timeout=400; |
Victordang | 0:c265e9b31b1d | 365 | getreply(); |
Victordang | 2:3704512ed2d4 | 366 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 367 | |
Victordang | 0:c265e9b31b1d | 368 | wait(2); |
Victordang | 0:c265e9b31b1d | 369 | |
Victordang | 0:c265e9b31b1d | 370 | pc.printf("\n---------- Listing Access Points ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 371 | strcpy(cmdbuff, "AT+CWLAP\r\n"); |
Victordang | 0:c265e9b31b1d | 372 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 373 | timeout=9000; |
Victordang | 2:3704512ed2d4 | 374 | getcount=5000; |
Victordang | 0:c265e9b31b1d | 375 | getreply(); |
Victordang | 2:3704512ed2d4 | 376 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 377 | |
Victordang | 0:c265e9b31b1d | 378 | wait(2); |
Victordang | 0:c265e9b31b1d | 379 | |
Victordang | 0:c265e9b31b1d | 380 | pc.printf("\n---------- Connecting to AP ----------\r\n"); |
Victordang | 0:c265e9b31b1d | 381 | pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); |
Victordang | 2:3704512ed2d4 | 382 | strcpy(cmdbuff, "AT+CWJAP=\""); |
Victordang | 2:3704512ed2d4 | 383 | strcat(cmdbuff, ssid); |
Victordang | 2:3704512ed2d4 | 384 | strcat(cmdbuff, "\",\""); |
Victordang | 2:3704512ed2d4 | 385 | strcat(cmdbuff, pwd); |
Victordang | 2:3704512ed2d4 | 386 | strcat(cmdbuff, "\"\r\n"); |
Victordang | 0:c265e9b31b1d | 387 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 388 | timeout=9000; |
Victordang | 2:3704512ed2d4 | 389 | getcount=5000; |
Victordang | 0:c265e9b31b1d | 390 | getreply(); |
Victordang | 2:3704512ed2d4 | 391 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 392 | |
Victordang | 0:c265e9b31b1d | 393 | wait(5); |
Victordang | 0:c265e9b31b1d | 394 | |
Victordang | 0:c265e9b31b1d | 395 | pc.printf("\n---------- Get IP's ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 396 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
Victordang | 0:c265e9b31b1d | 397 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 398 | timeout=400; |
Victordang | 0:c265e9b31b1d | 399 | getreply(); |
Victordang | 2:3704512ed2d4 | 400 | pc.printf(replybuff); |
Victordang | 0:c265e9b31b1d | 401 | |
Victordang | 0:c265e9b31b1d | 402 | wait(1); |
Victordang | 0:c265e9b31b1d | 403 | |
Victordang | 0:c265e9b31b1d | 404 | pc.printf("\n---------- Get Connection Status ----------\r\n"); |
Victordang | 2:3704512ed2d4 | 405 | strcpy(cmdbuff, "AT+CIPSTATUS\r\n"); |
Victordang | 0:c265e9b31b1d | 406 | SendCMD(); |
Victordang | 2:3704512ed2d4 | 407 | timeout=500; |
Victordang | 0:c265e9b31b1d | 408 | getreply(); |
Victordang | 2:3704512ed2d4 | 409 | pc.printf(replybuff); |
Victordang | 2:3704512ed2d4 | 410 | pc.printf("ESP-WROOM saves the SSID and password settings internally\r\n"); |
Victordang | 0:c265e9b31b1d | 411 | wait(10); |
Victordang | 0:c265e9b31b1d | 412 | } |