Biswajit Padhi
/
ESP8266-WEB-Mbed-LPC1768-Controller
wui0iuytdsaertyuio
Fork of ESP8266-WEB-Mbed-LPC1768-Controller by
main.cpp@4:40dd020463ea, 2015-08-28 (annotated)
- Committer:
- 4180_1
- Date:
- Fri Aug 28 01:37:50 2015 +0000
- Revision:
- 4:40dd020463ea
- Parent:
- 3:f7febfa77784
- Child:
- 5:3d29774cc3e2
ver 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:e2a155f50119 | 1 | // ESP8266 Static page WEB server to control Mbed |
star297 | 0:e2a155f50119 | 2 | |
star297 | 0:e2a155f50119 | 3 | #include "mbed.h" |
4180_1 | 4:40dd020463ea | 4 | //#include "DS18B20.h" |
star297 | 0:e2a155f50119 | 5 | |
star297 | 0:e2a155f50119 | 6 | Serial pc(USBTX, USBRX); |
4180_1 | 4:40dd020463ea | 7 | Serial esp(p28, p27); // tx, rx |
star297 | 0:e2a155f50119 | 8 | |
4180_1 | 4:40dd020463ea | 9 | //DS18B20 thermom(A0, DS18B20::RES_12_BIT); |
star297 | 2:d4c6bc0f2dc4 | 10 | |
star297 | 0:e2a155f50119 | 11 | // Standard Mbed LED definitions |
4180_1 | 4:40dd020463ea | 12 | DigitalOut led1(LED1); // (PTB18) |
4180_1 | 4:40dd020463ea | 13 | DigitalOut led2(LED2); // (PTB19) |
4180_1 | 4:40dd020463ea | 14 | DigitalOut led3(LED3); // (PTD1) |
star297 | 0:e2a155f50119 | 15 | |
star297 | 0:e2a155f50119 | 16 | // Digital Out and In pins, can be configured to any suitable pin depending on Platform |
4180_1 | 4:40dd020463ea | 17 | DigitalOut Out1(p6); |
4180_1 | 4:40dd020463ea | 18 | DigitalOut Out2(p7); |
4180_1 | 4:40dd020463ea | 19 | DigitalOut Out3(p8); |
4180_1 | 4:40dd020463ea | 20 | DigitalOut reset(p26); |
star297 | 0:e2a155f50119 | 21 | |
4180_1 | 4:40dd020463ea | 22 | DigitalIn In1(p9); |
4180_1 | 4:40dd020463ea | 23 | DigitalIn In2(p10); |
4180_1 | 4:40dd020463ea | 24 | DigitalIn In3(p11); |
star297 | 0:e2a155f50119 | 25 | |
4180_1 | 4:40dd020463ea | 26 | PwmOut speaker(p21); |
4180_1 | 4:40dd020463ea | 27 | AnalogIn Ain1(p18); |
4180_1 | 4:40dd020463ea | 28 | AnalogIn Ain2(p19); |
star297 | 0:e2a155f50119 | 29 | |
star297 | 0:e2a155f50119 | 30 | Timer t1; |
star297 | 1:71ed1afbf344 | 31 | Timer t2; |
star297 | 1:71ed1afbf344 | 32 | |
star297 | 1:71ed1afbf344 | 33 | struct tm t; |
star297 | 0:e2a155f50119 | 34 | |
star297 | 3:f7febfa77784 | 35 | int bufflen, DataRX, count, getcount, replycount, servreq, timeout; |
star297 | 2:d4c6bc0f2dc4 | 36 | int bufl, ipdLen, linkID, weberror, webcounter; |
4180_1 | 4:40dd020463ea | 37 | float temperature, AdcIn, Ht; |
star297 | 0:e2a155f50119 | 38 | float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage |
star297 | 0:e2a155f50119 | 39 | char Vcc[10]; |
star297 | 0:e2a155f50119 | 40 | char Temp[10]; |
star297 | 0:e2a155f50119 | 41 | char temp[10]; |
star297 | 2:d4c6bc0f2dc4 | 42 | char webcount[8]; |
star297 | 3:f7febfa77784 | 43 | char lasthit[30]; |
star297 | 0:e2a155f50119 | 44 | char timebuf[30]; |
star297 | 0:e2a155f50119 | 45 | char type[16]; |
star297 | 0:e2a155f50119 | 46 | char type1[16]; |
star297 | 0:e2a155f50119 | 47 | char channel[2]; |
star297 | 3:f7febfa77784 | 48 | char cmdbuff[32]; |
4180_1 | 4:40dd020463ea | 49 | char replybuff[1024]; |
star297 | 0:e2a155f50119 | 50 | char webdata[1024]; // This may need to be bigger depending on WEB browser used |
star297 | 3:f7febfa77784 | 51 | char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added |
star297 | 0:e2a155f50119 | 52 | |
star297 | 0:e2a155f50119 | 53 | void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); |
star297 | 2:d4c6bc0f2dc4 | 54 | void gettime(),gettemp(),getbattery(),setRTC(),beep(); |
star297 | 0:e2a155f50119 | 55 | |
star297 | 1:71ed1afbf344 | 56 | // manual set RTC values |
4180_1 | 4:40dd020463ea | 57 | int minute =00; // 0-59 |
4180_1 | 4:40dd020463ea | 58 | int hour =12; // 2-23 |
4180_1 | 4:40dd020463ea | 59 | int dayofmonth =26; // 1-31 |
4180_1 | 4:40dd020463ea | 60 | int month =8; // 1-12 |
star297 | 1:71ed1afbf344 | 61 | int year =15; // last 2 digits |
star297 | 2:d4c6bc0f2dc4 | 62 | |
4180_1 | 4:40dd020463ea | 63 | int port =80; // set server port |
4180_1 | 4:40dd020463ea | 64 | int SERVtimeout =5; // set server timeout in seconds in case link breaks. |
4180_1 | 4:40dd020463ea | 65 | |
star297 | 0:e2a155f50119 | 66 | // Serial Interrupt read ESP data |
4180_1 | 4:40dd020463ea | 67 | void callback() |
4180_1 | 4:40dd020463ea | 68 | { |
4180_1 | 4:40dd020463ea | 69 | led3=1; |
4180_1 | 4:40dd020463ea | 70 | while (esp.readable()) { |
4180_1 | 4:40dd020463ea | 71 | webbuff[count] = esp.getc(); |
4180_1 | 4:40dd020463ea | 72 | count++; |
4180_1 | 4:40dd020463ea | 73 | } |
4180_1 | 4:40dd020463ea | 74 | if(strlen(webbuff)>bufflen) { |
4180_1 | 4:40dd020463ea | 75 | DataRX=1; |
4180_1 | 4:40dd020463ea | 76 | led3=0; |
4180_1 | 4:40dd020463ea | 77 | } |
star297 | 0:e2a155f50119 | 78 | } |
star297 | 0:e2a155f50119 | 79 | |
4180_1 | 4:40dd020463ea | 80 | int main() |
4180_1 | 4:40dd020463ea | 81 | { |
4180_1 | 4:40dd020463ea | 82 | reset=0; |
star297 | 0:e2a155f50119 | 83 | pc.baud(115200); |
4180_1 | 4:40dd020463ea | 84 | |
4180_1 | 4:40dd020463ea | 85 | pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r"); |
4180_1 | 4:40dd020463ea | 86 | wait(0.5); |
4180_1 | 4:40dd020463ea | 87 | reset=1; |
4180_1 | 4:40dd020463ea | 88 | led1=1,led2=0,led3=0; |
4180_1 | 4:40dd020463ea | 89 | timeout=6000; |
4180_1 | 4:40dd020463ea | 90 | getcount=500; |
4180_1 | 4:40dd020463ea | 91 | getreply(); |
star297 | 3:f7febfa77784 | 92 | esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F |
4180_1 | 4:40dd020463ea | 93 | if (time(NULL) < 1420070400) { |
4180_1 | 4:40dd020463ea | 94 | setRTC(); |
4180_1 | 4:40dd020463ea | 95 | } |
star297 | 2:d4c6bc0f2dc4 | 96 | beep(); |
star297 | 0:e2a155f50119 | 97 | startserver(); |
4180_1 | 4:40dd020463ea | 98 | |
4180_1 | 4:40dd020463ea | 99 | while(1) { |
4180_1 | 4:40dd020463ea | 100 | if(DataRX==1) { |
star297 | 0:e2a155f50119 | 101 | ReadWebData(); |
star297 | 2:d4c6bc0f2dc4 | 102 | beep(); |
4180_1 | 4:40dd020463ea | 103 | if (servreq == 1 && weberror == 0) { |
4180_1 | 4:40dd020463ea | 104 | sendpage(); |
4180_1 | 4:40dd020463ea | 105 | } |
4180_1 | 4:40dd020463ea | 106 | esp.attach(&callback); |
4180_1 | 4:40dd020463ea | 107 | 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); |
4180_1 | 4:40dd020463ea | 108 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
star297 | 0:e2a155f50119 | 109 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
star297 | 0:e2a155f50119 | 110 | pc.printf(" -------------------------------------\n\n"); |
star297 | 2:d4c6bc0f2dc4 | 111 | strcpy(lasthit, timebuf); |
4180_1 | 4:40dd020463ea | 112 | servreq=0; |
4180_1 | 4:40dd020463ea | 113 | } |
star297 | 0:e2a155f50119 | 114 | } |
4180_1 | 4:40dd020463ea | 115 | } |
4180_1 | 4:40dd020463ea | 116 | // Static WEB page |
star297 | 0:e2a155f50119 | 117 | void sendpage() |
4180_1 | 4:40dd020463ea | 118 | { |
star297 | 3:f7febfa77784 | 119 | gettemp(); |
4180_1 | 4:40dd020463ea | 120 | getbattery(); |
4180_1 | 4:40dd020463ea | 121 | gettime(); |
4180_1 | 4:40dd020463ea | 122 | |
4180_1 | 4:40dd020463ea | 123 | // WEB page data |
star297 | 3:f7febfa77784 | 124 | strcpy(webbuff, "<!DOCTYPE html>"); |
4180_1 | 4:40dd020463ea | 125 | strcat(webbuff, "<html><head><title>ESP8266 Mbed LPC1768</title></head>"); |
4180_1 | 4:40dd020463ea | 126 | strcat(webbuff, "<body>"); |
4180_1 | 4:40dd020463ea | 127 | strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed IoT Web Controller</h1>"); |
star297 | 3:f7febfa77784 | 128 | strcat(webbuff, "Hit Count - "); |
star297 | 3:f7febfa77784 | 129 | strcat(webbuff, webcount); |
star297 | 3:f7febfa77784 | 130 | strcat(webbuff, "<br>Last Hit - "); |
4180_1 | 4:40dd020463ea | 131 | strcat(webbuff, lasthit); |
star297 | 3:f7febfa77784 | 132 | strcat(webbuff, "</div><br /><hr>"); |
4180_1 | 4:40dd020463ea | 133 | strcat(webbuff, "<h3>Mbed RTC Time -  "); |
star297 | 3:f7febfa77784 | 134 | strcat(webbuff, timebuf); |
star297 | 3:f7febfa77784 | 135 | strcat(webbuff, "</h3>\r\n"); |
4180_1 | 4:40dd020463ea | 136 | strcat(webbuff, "<p><form method=\"POST\"><strong> Analog 1:  <input type=\"text\" size=6 value=\""); |
star297 | 3:f7febfa77784 | 137 | strcat(webbuff, Temp); |
4180_1 | 4:40dd020463ea | 138 | strcat(webbuff, "\"> </sup>V <form method=\"POST\"> <strong>   Analog 2:  <input type=\"text\" size=4 value=\""); |
star297 | 3:f7febfa77784 | 139 | strcat(webbuff, Vcc); |
4180_1 | 4:40dd020463ea | 140 | strcat(webbuff, "\"> </sup>V"); |
4180_1 | 4:40dd020463ea | 141 | if(led1==0) { |
4180_1 | 4:40dd020463ea | 142 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> LED 1 off"); |
4180_1 | 4:40dd020463ea | 143 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > LED 1 on"); |
4180_1 | 4:40dd020463ea | 144 | } else { |
4180_1 | 4:40dd020463ea | 145 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > LED 1 off"); |
4180_1 | 4:40dd020463ea | 146 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> LED 1 on"); |
4180_1 | 4:40dd020463ea | 147 | } |
4180_1 | 4:40dd020463ea | 148 | if(Out1==0) { |
4180_1 | 4:40dd020463ea | 149 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked> Digital Out 1 off"); |
4180_1 | 4:40dd020463ea | 150 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" > Digital Out 1 on"); |
4180_1 | 4:40dd020463ea | 151 | } else { |
4180_1 | 4:40dd020463ea | 152 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" > Digital Out 1 off"); |
4180_1 | 4:40dd020463ea | 153 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked> Digital Out 1 on"); |
4180_1 | 4:40dd020463ea | 154 | } |
4180_1 | 4:40dd020463ea | 155 | if(Out2==0) { |
4180_1 | 4:40dd020463ea | 156 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked> Digital Out 2 off"); |
4180_1 | 4:40dd020463ea | 157 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" > Digital Out 2 on"); |
4180_1 | 4:40dd020463ea | 158 | } else { |
4180_1 | 4:40dd020463ea | 159 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" > Digital Out 2 off"); |
4180_1 | 4:40dd020463ea | 160 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked> Digital Out 2 on"); |
4180_1 | 4:40dd020463ea | 161 | } |
4180_1 | 4:40dd020463ea | 162 | if(Out3==0) { |
4180_1 | 4:40dd020463ea | 163 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked> Digital Out 3 off"); |
4180_1 | 4:40dd020463ea | 164 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" > Digital Out 3 on"); |
4180_1 | 4:40dd020463ea | 165 | } else { |
4180_1 | 4:40dd020463ea | 166 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" > Digital Out 3 off"); |
4180_1 | 4:40dd020463ea | 167 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked> Digital Out 3 on"); |
4180_1 | 4:40dd020463ea | 168 | } |
4180_1 | 4:40dd020463ea | 169 | if(In1==0) { |
4180_1 | 4:40dd020463ea | 170 | strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1"); |
4180_1 | 4:40dd020463ea | 171 | } else { |
4180_1 | 4:40dd020463ea | 172 | strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1"); |
4180_1 | 4:40dd020463ea | 173 | } |
4180_1 | 4:40dd020463ea | 174 | if(In2==0) { |
4180_1 | 4:40dd020463ea | 175 | strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"0\" > Digital In 2"); |
4180_1 | 4:40dd020463ea | 176 | } else { |
4180_1 | 4:40dd020463ea | 177 | strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked> Digital In 2"); |
4180_1 | 4:40dd020463ea | 178 | } |
4180_1 | 4:40dd020463ea | 179 | if(In3==0) { |
4180_1 | 4:40dd020463ea | 180 | strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"0\" > Digital In 3"); |
4180_1 | 4:40dd020463ea | 181 | } else { |
4180_1 | 4:40dd020463ea | 182 | strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked> Digital In 3"); |
4180_1 | 4:40dd020463ea | 183 | } |
4180_1 | 4:40dd020463ea | 184 | strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;"); |
4180_1 | 4:40dd020463ea | 185 | strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);"); |
star297 | 3:f7febfa77784 | 186 | strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);"); |
star297 | 3:f7febfa77784 | 187 | strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:"); |
4180_1 | 4:40dd020463ea | 188 | strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); |
star297 | 3:f7febfa77784 | 189 | strcat(webbuff, "background: #3cb0fd;"); |
4180_1 | 4:40dd020463ea | 190 | strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); |
star297 | 3:f7febfa77784 | 191 | strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);"); |
star297 | 3:f7febfa77784 | 192 | strcat(webbuff, "text-decoration:none;\"></form></span>"); |
star297 | 3:f7febfa77784 | 193 | strcat(webbuff, "<p/><h2>How to use:</h2><ul>"); |
star297 | 3:f7febfa77784 | 194 | strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>"); |
4180_1 | 4:40dd020463ea | 195 | strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>"); |
4180_1 | 4:40dd020463ea | 196 | strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>"); |
star297 | 3:f7febfa77784 | 197 | strcat(webbuff, "</ul>"); |
4180_1 | 4:40dd020463ea | 198 | strcat(webbuff, "</body></html>"); |
4180_1 | 4:40dd020463ea | 199 | // end of WEB page data |
4180_1 | 4:40dd020463ea | 200 | bufl = strlen(webbuff); // get total page buffer length |
star297 | 3:f7febfa77784 | 201 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
4180_1 | 4:40dd020463ea | 202 | timeout=200; |
4180_1 | 4:40dd020463ea | 203 | getcount=7; |
star297 | 3:f7febfa77784 | 204 | SendCMD(); |
4180_1 | 4:40dd020463ea | 205 | getreply(); |
star297 | 0:e2a155f50119 | 206 | SendWEB(); // send web page |
star297 | 3:f7febfa77784 | 207 | memset(webbuff, '\0', sizeof(webbuff)); |
4180_1 | 4:40dd020463ea | 208 | sendcheck(); |
star297 | 0:e2a155f50119 | 209 | } |
star297 | 0:e2a155f50119 | 210 | |
star297 | 3:f7febfa77784 | 211 | // wait for ESP "SEND OK" reply, then close IP to load web page |
star297 | 0:e2a155f50119 | 212 | void sendcheck() |
star297 | 0:e2a155f50119 | 213 | { |
4180_1 | 4:40dd020463ea | 214 | weberror=1; |
4180_1 | 4:40dd020463ea | 215 | timeout=500; |
4180_1 | 4:40dd020463ea | 216 | getcount=24; |
4180_1 | 4:40dd020463ea | 217 | t2.reset(); |
4180_1 | 4:40dd020463ea | 218 | t2.start(); |
4180_1 | 4:40dd020463ea | 219 | while(weberror==1 && t2.read() <5) { |
star297 | 0:e2a155f50119 | 220 | getreply(); |
4180_1 | 4:40dd020463ea | 221 | if (strstr(replybuff, "SEND OK") != NULL) { |
4180_1 | 4:40dd020463ea | 222 | weberror=0; // wait for valid SEND OK |
star297 | 0:e2a155f50119 | 223 | } |
4180_1 | 4:40dd020463ea | 224 | } |
4180_1 | 4:40dd020463ea | 225 | if(weberror==1) { // restart connection |
4180_1 | 4:40dd020463ea | 226 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
4180_1 | 4:40dd020463ea | 227 | timeout=500; |
4180_1 | 4:40dd020463ea | 228 | getcount=10; |
4180_1 | 4:40dd020463ea | 229 | SendCMD(); |
4180_1 | 4:40dd020463ea | 230 | getreply(); |
4180_1 | 4:40dd020463ea | 231 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 232 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
4180_1 | 4:40dd020463ea | 233 | timeout=500; |
4180_1 | 4:40dd020463ea | 234 | getcount=10; |
4180_1 | 4:40dd020463ea | 235 | SendCMD(); |
4180_1 | 4:40dd020463ea | 236 | getreply(); |
4180_1 | 4:40dd020463ea | 237 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 238 | } else { |
4180_1 | 4:40dd020463ea | 239 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
4180_1 | 4:40dd020463ea | 240 | SendCMD(); |
4180_1 | 4:40dd020463ea | 241 | getreply(); |
4180_1 | 4:40dd020463ea | 242 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 243 | } |
4180_1 | 4:40dd020463ea | 244 | t2.reset(); |
4180_1 | 4:40dd020463ea | 245 | } |
star297 | 0:e2a155f50119 | 246 | |
4180_1 | 4:40dd020463ea | 247 | // Reads and processes GET and POST web data |
star297 | 0:e2a155f50119 | 248 | void ReadWebData() |
4180_1 | 4:40dd020463ea | 249 | { |
4180_1 | 4:40dd020463ea | 250 | wait_ms(200); |
star297 | 3:f7febfa77784 | 251 | esp.attach(NULL); |
4180_1 | 4:40dd020463ea | 252 | count=0; |
4180_1 | 4:40dd020463ea | 253 | DataRX=0; |
4180_1 | 4:40dd020463ea | 254 | weberror=0; |
4180_1 | 4:40dd020463ea | 255 | memset(webdata, '\0', sizeof(webdata)); |
star297 | 3:f7febfa77784 | 256 | int x = strcspn (webbuff,"+"); |
4180_1 | 4:40dd020463ea | 257 | if(x) { |
4180_1 | 4:40dd020463ea | 258 | strcpy(webdata, webbuff + x); |
4180_1 | 4:40dd020463ea | 259 | weberror=0; |
4180_1 | 4:40dd020463ea | 260 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
4180_1 | 4:40dd020463ea | 261 | if( strstr(webdata, "led1=1") != NULL ) { |
4180_1 | 4:40dd020463ea | 262 | led1=1; |
4180_1 | 4:40dd020463ea | 263 | } |
4180_1 | 4:40dd020463ea | 264 | if( strstr(webdata, "led1=0") != NULL ) { |
4180_1 | 4:40dd020463ea | 265 | led1=0; |
4180_1 | 4:40dd020463ea | 266 | } |
4180_1 | 4:40dd020463ea | 267 | if( strstr(webdata, "Out1=1") != NULL ) { |
4180_1 | 4:40dd020463ea | 268 | Out1=1; |
4180_1 | 4:40dd020463ea | 269 | } |
4180_1 | 4:40dd020463ea | 270 | if( strstr(webdata, "Out1=0") != NULL ) { |
4180_1 | 4:40dd020463ea | 271 | Out1=0; |
4180_1 | 4:40dd020463ea | 272 | } |
4180_1 | 4:40dd020463ea | 273 | if( strstr(webdata, "Out2=1") != NULL ) { |
4180_1 | 4:40dd020463ea | 274 | Out2=1; |
4180_1 | 4:40dd020463ea | 275 | } |
4180_1 | 4:40dd020463ea | 276 | if( strstr(webdata, "Out2=0") != NULL ) { |
4180_1 | 4:40dd020463ea | 277 | Out2=0; |
4180_1 | 4:40dd020463ea | 278 | } |
4180_1 | 4:40dd020463ea | 279 | if( strstr(webdata, "Out3=1") != NULL ) { |
4180_1 | 4:40dd020463ea | 280 | Out3=1; |
4180_1 | 4:40dd020463ea | 281 | } |
4180_1 | 4:40dd020463ea | 282 | if( strstr(webdata, "Out3=0") != NULL ) { |
4180_1 | 4:40dd020463ea | 283 | Out3=0; |
4180_1 | 4:40dd020463ea | 284 | } |
4180_1 | 4:40dd020463ea | 285 | sprintf(channel, "%d",linkID); |
4180_1 | 4:40dd020463ea | 286 | if (strstr(webdata, "GET") != NULL) { |
4180_1 | 4:40dd020463ea | 287 | servreq=1; |
4180_1 | 4:40dd020463ea | 288 | } |
4180_1 | 4:40dd020463ea | 289 | if (strstr(webdata, "POST") != NULL) { |
4180_1 | 4:40dd020463ea | 290 | servreq=1; |
4180_1 | 4:40dd020463ea | 291 | } |
star297 | 2:d4c6bc0f2dc4 | 292 | webcounter++; |
star297 | 2:d4c6bc0f2dc4 | 293 | sprintf(webcount, "%d",webcounter); |
4180_1 | 4:40dd020463ea | 294 | } else { |
4180_1 | 4:40dd020463ea | 295 | memset(webbuff, '\0', sizeof(webbuff)); |
4180_1 | 4:40dd020463ea | 296 | esp.attach(&callback); |
4180_1 | 4:40dd020463ea | 297 | weberror=1; |
4180_1 | 4:40dd020463ea | 298 | } |
star297 | 0:e2a155f50119 | 299 | } |
star297 | 0:e2a155f50119 | 300 | // Starts and restarts webserver if errors detected. |
star297 | 0:e2a155f50119 | 301 | void startserver() |
star297 | 0:e2a155f50119 | 302 | { |
4180_1 | 4:40dd020463ea | 303 | gettemp(); |
4180_1 | 4:40dd020463ea | 304 | gettime(); |
star297 | 1:71ed1afbf344 | 305 | pc.printf("\n\n RTC time %s\r\n\n",timebuf); |
4180_1 | 4:40dd020463ea | 306 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
star297 | 3:f7febfa77784 | 307 | strcpy(cmdbuff,"AT+RST\r\n"); |
4180_1 | 4:40dd020463ea | 308 | timeout=8000; |
4180_1 | 4:40dd020463ea | 309 | getcount=1000; |
star297 | 0:e2a155f50119 | 310 | SendCMD(); |
star297 | 1:71ed1afbf344 | 311 | getreply(); |
star297 | 3:f7febfa77784 | 312 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 313 | pc.printf("%d",count); |
star297 | 3:f7febfa77784 | 314 | if (strstr(replybuff, "OK") != NULL) { |
star297 | 1:71ed1afbf344 | 315 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
4180_1 | 4:40dd020463ea | 316 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections. |
4180_1 | 4:40dd020463ea | 317 | timeout=500; |
4180_1 | 4:40dd020463ea | 318 | getcount=20; |
star297 | 1:71ed1afbf344 | 319 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 320 | getreply(); |
4180_1 | 4:40dd020463ea | 321 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 322 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
4180_1 | 4:40dd020463ea | 323 | timeout=500; |
4180_1 | 4:40dd020463ea | 324 | getcount=20; |
star297 | 1:71ed1afbf344 | 325 | SendCMD(); |
star297 | 3:f7febfa77784 | 326 | getreply(); |
4180_1 | 4:40dd020463ea | 327 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 328 | wait(1); |
star297 | 3:f7febfa77784 | 329 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
4180_1 | 4:40dd020463ea | 330 | timeout=500; |
4180_1 | 4:40dd020463ea | 331 | getcount=50; |
star297 | 3:f7febfa77784 | 332 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 333 | getreply(); |
4180_1 | 4:40dd020463ea | 334 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 335 | wait(5); |
4180_1 | 4:40dd020463ea | 336 | pc.printf("\n Getting Server IP \r\n"); |
star297 | 3:f7febfa77784 | 337 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
4180_1 | 4:40dd020463ea | 338 | timeout=2500; |
4180_1 | 4:40dd020463ea | 339 | getcount=200; |
4180_1 | 4:40dd020463ea | 340 | while(weberror==0) { |
4180_1 | 4:40dd020463ea | 341 | SendCMD(); |
4180_1 | 4:40dd020463ea | 342 | getreply(); |
4180_1 | 4:40dd020463ea | 343 | if (strstr(replybuff, "0.0.0.0") == NULL) { |
4180_1 | 4:40dd020463ea | 344 | weberror=1; // wait for valid IP |
star297 | 1:71ed1afbf344 | 345 | } |
4180_1 | 4:40dd020463ea | 346 | } |
4180_1 | 4:40dd020463ea | 347 | pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n"); |
4180_1 | 4:40dd020463ea | 348 | pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n"); |
4180_1 | 4:40dd020463ea | 349 | replybuff[strlen(replybuff)-1] = '\0'; |
4180_1 | 4:40dd020463ea | 350 | //char* IP = replybuff + 5; |
4180_1 | 4:40dd020463ea | 351 | sprintf(webdata,"%s", replybuff); |
4180_1 | 4:40dd020463ea | 352 | pc.printf(webdata); |
4180_1 | 4:40dd020463ea | 353 | led2=1; |
4180_1 | 4:40dd020463ea | 354 | bufflen=200; |
4180_1 | 4:40dd020463ea | 355 | count=0; |
star297 | 1:71ed1afbf344 | 356 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
star297 | 1:71ed1afbf344 | 357 | esp.attach(&callback); |
4180_1 | 4:40dd020463ea | 358 | } else { |
4180_1 | 4:40dd020463ea | 359 | pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); |
4180_1 | 4:40dd020463ea | 360 | while(1) {} |
4180_1 | 4:40dd020463ea | 361 | } |
4180_1 | 4:40dd020463ea | 362 | t2.reset(); |
4180_1 | 4:40dd020463ea | 363 | t2.start(); |
4180_1 | 4:40dd020463ea | 364 | beep(); |
4180_1 | 4:40dd020463ea | 365 | } |
star297 | 0:e2a155f50119 | 366 | // ESP Command data send |
star297 | 0:e2a155f50119 | 367 | void SendCMD() |
star297 | 0:e2a155f50119 | 368 | { |
4180_1 | 4:40dd020463ea | 369 | esp.printf("%s", cmdbuff); |
4180_1 | 4:40dd020463ea | 370 | } |
star297 | 0:e2a155f50119 | 371 | // Large WEB buffer data send |
star297 | 0:e2a155f50119 | 372 | void SendWEB() |
4180_1 | 4:40dd020463ea | 373 | { |
star297 | 0:e2a155f50119 | 374 | int i=0; |
star297 | 0:e2a155f50119 | 375 | if(esp.writeable()) { |
4180_1 | 4:40dd020463ea | 376 | while(webbuff[i]!='\0') { |
4180_1 | 4:40dd020463ea | 377 | esp.putc(webbuff[i]); |
4180_1 | 4:40dd020463ea | 378 | i++; |
4180_1 | 4:40dd020463ea | 379 | } |
4180_1 | 4:40dd020463ea | 380 | } |
4180_1 | 4:40dd020463ea | 381 | } |
4180_1 | 4:40dd020463ea | 382 | // Get Command and ESP status replies |
star297 | 0:e2a155f50119 | 383 | void getreply() |
4180_1 | 4:40dd020463ea | 384 | { |
star297 | 3:f7febfa77784 | 385 | memset(replybuff, '\0', sizeof(replybuff)); |
4180_1 | 4:40dd020463ea | 386 | t1.reset(); |
4180_1 | 4:40dd020463ea | 387 | t1.start(); |
4180_1 | 4:40dd020463ea | 388 | replycount=0; |
star297 | 3:f7febfa77784 | 389 | while(t1.read_ms()< timeout && replycount < getcount) { |
star297 | 0:e2a155f50119 | 390 | if(esp.readable()) { |
4180_1 | 4:40dd020463ea | 391 | replybuff[replycount] = esp.getc(); |
4180_1 | 4:40dd020463ea | 392 | replycount++; |
star297 | 2:d4c6bc0f2dc4 | 393 | } |
4180_1 | 4:40dd020463ea | 394 | } |
4180_1 | 4:40dd020463ea | 395 | t1.stop(); |
star297 | 0:e2a155f50119 | 396 | } |
star297 | 0:e2a155f50119 | 397 | // Analog in example |
star297 | 0:e2a155f50119 | 398 | void getbattery() |
star297 | 0:e2a155f50119 | 399 | { |
4180_1 | 4:40dd020463ea | 400 | AdcIn=Ain1.read(); |
4180_1 | 4:40dd020463ea | 401 | Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy |
4180_1 | 4:40dd020463ea | 402 | sprintf(Vcc,"%2.3f",Ht); |
star297 | 0:e2a155f50119 | 403 | } |
star297 | 0:e2a155f50119 | 404 | // Temperature example |
star297 | 0:e2a155f50119 | 405 | void gettemp() |
4180_1 | 4:40dd020463ea | 406 | { |
4180_1 | 4:40dd020463ea | 407 | |
4180_1 | 4:40dd020463ea | 408 | AdcIn=Ain2.read(); |
4180_1 | 4:40dd020463ea | 409 | Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy |
4180_1 | 4:40dd020463ea | 410 | sprintf(Temp,"%2.3f",Ht); |
star297 | 0:e2a155f50119 | 411 | } |
4180_1 | 4:40dd020463ea | 412 | // Get RTC time |
star297 | 0:e2a155f50119 | 413 | void gettime() |
star297 | 0:e2a155f50119 | 414 | { |
star297 | 0:e2a155f50119 | 415 | time_t seconds = time(NULL); |
4180_1 | 4:40dd020463ea | 416 | strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds)); |
star297 | 0:e2a155f50119 | 417 | } |
star297 | 0:e2a155f50119 | 418 | |
star297 | 2:d4c6bc0f2dc4 | 419 | void beep() |
4180_1 | 4:40dd020463ea | 420 | { |
star297 | 2:d4c6bc0f2dc4 | 421 | speaker.period(1.0/2000); // 2000hz period |
star297 | 2:d4c6bc0f2dc4 | 422 | speaker = 0.5; //50% duty cycle - max volume |
star297 | 2:d4c6bc0f2dc4 | 423 | wait_ms(60); |
star297 | 2:d4c6bc0f2dc4 | 424 | speaker=0.0; // turn off audio |
star297 | 2:d4c6bc0f2dc4 | 425 | } |
star297 | 2:d4c6bc0f2dc4 | 426 | |
star297 | 1:71ed1afbf344 | 427 | void setRTC() |
star297 | 1:71ed1afbf344 | 428 | { |
4180_1 | 4:40dd020463ea | 429 | t.tm_sec = (0); // 0-59 |
4180_1 | 4:40dd020463ea | 430 | t.tm_min = (minute); // 0-59 |
4180_1 | 4:40dd020463ea | 431 | t.tm_hour = (hour); // 0-23 |
4180_1 | 4:40dd020463ea | 432 | t.tm_mday = (dayofmonth); // 1-31 |
4180_1 | 4:40dd020463ea | 433 | t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format |
4180_1 | 4:40dd020463ea | 434 | t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year |
4180_1 | 4:40dd020463ea | 435 | set_time(mktime(&t)); // set RTC clock |
4180_1 | 4:40dd020463ea | 436 | } |