![](/media/cache/profiles/ibmfp2.jpg.50x50_q85.jpg)
For ESP8266 Wi Fi SOC from Sparkfun on the mbed LPC1768. Sets up a web page server that can control a few mbed pins. Nice IoT demo. Need to configure Wi Fi SSID and PASSWORD first with another program first (one time only). See https://developer.mbed.org/users/4180_1/notebook/using-the-esp8266-with-the-mbed-lpc1768/ for more info.
Fork of ESP8266-WEB-Mbed-Controller by
main.cpp@3:f7febfa77784, 2015-02-24 (annotated)
- Committer:
- star297
- Date:
- Tue Feb 24 23:58:24 2015 +0000
- Revision:
- 3:f7febfa77784
- Parent:
- 2:d4c6bc0f2dc4
- Child:
- 4:40dd020463ea
improved web lock out
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" |
star297 | 0:e2a155f50119 | 4 | #include "DS18B20.h" |
star297 | 0:e2a155f50119 | 5 | |
star297 | 0:e2a155f50119 | 6 | Serial pc(USBTX, USBRX); |
star297 | 0:e2a155f50119 | 7 | Serial esp(PTE0, PTE1); // tx, rx |
star297 | 0:e2a155f50119 | 8 | |
star297 | 2:d4c6bc0f2dc4 | 9 | DS18B20 thermom(A0, DS18B20::RES_12_BIT); |
star297 | 2:d4c6bc0f2dc4 | 10 | |
star297 | 0:e2a155f50119 | 11 | // Standard Mbed LED definitions |
star297 | 0:e2a155f50119 | 12 | DigitalOut led1(LED_RED); // (PTB18) |
star297 | 0:e2a155f50119 | 13 | DigitalOut led2(LED_GREEN); // (PTB19) |
star297 | 0:e2a155f50119 | 14 | DigitalOut led3(LED_BLUE); // (PTD1) |
star297 | 0:e2a155f50119 | 15 | |
star297 | 0:e2a155f50119 | 16 | // Digital Out and In pins, can be configured to any suitable pin depending on Platform |
star297 | 2:d4c6bc0f2dc4 | 17 | DigitalOut Out1(D7); |
star297 | 2:d4c6bc0f2dc4 | 18 | DigitalOut Out2(D8); |
star297 | 2:d4c6bc0f2dc4 | 19 | DigitalOut Out3(D4); |
star297 | 0:e2a155f50119 | 20 | |
star297 | 2:d4c6bc0f2dc4 | 21 | DigitalIn In1(A2); |
star297 | 2:d4c6bc0f2dc4 | 22 | DigitalIn In2(A3); |
star297 | 2:d4c6bc0f2dc4 | 23 | DigitalIn In3(A4); |
star297 | 0:e2a155f50119 | 24 | |
star297 | 2:d4c6bc0f2dc4 | 25 | PwmOut speaker(D5); |
star297 | 2:d4c6bc0f2dc4 | 26 | AnalogIn BATin(A1); |
star297 | 0:e2a155f50119 | 27 | |
star297 | 0:e2a155f50119 | 28 | Timer t1; |
star297 | 1:71ed1afbf344 | 29 | Timer t2; |
star297 | 1:71ed1afbf344 | 30 | |
star297 | 1:71ed1afbf344 | 31 | struct tm t; |
star297 | 0:e2a155f50119 | 32 | |
star297 | 3:f7febfa77784 | 33 | int bufflen, DataRX, count, getcount, replycount, servreq, timeout; |
star297 | 2:d4c6bc0f2dc4 | 34 | int bufl, ipdLen, linkID, weberror, webcounter; |
star297 | 0:e2a155f50119 | 35 | float temperature, AdcIn, Ht; |
star297 | 0:e2a155f50119 | 36 | float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage |
star297 | 0:e2a155f50119 | 37 | char Vcc[10]; |
star297 | 0:e2a155f50119 | 38 | char Temp[10]; |
star297 | 0:e2a155f50119 | 39 | char temp[10]; |
star297 | 2:d4c6bc0f2dc4 | 40 | char webcount[8]; |
star297 | 3:f7febfa77784 | 41 | char lasthit[30]; |
star297 | 0:e2a155f50119 | 42 | char timebuf[30]; |
star297 | 0:e2a155f50119 | 43 | char type[16]; |
star297 | 0:e2a155f50119 | 44 | char type1[16]; |
star297 | 0:e2a155f50119 | 45 | char channel[2]; |
star297 | 3:f7febfa77784 | 46 | char cmdbuff[32]; |
star297 | 3:f7febfa77784 | 47 | char replybuff[512]; |
star297 | 0:e2a155f50119 | 48 | char webdata[1024]; // This may need to be bigger depending on WEB browser used |
star297 | 3:f7febfa77784 | 49 | char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added |
star297 | 0:e2a155f50119 | 50 | |
star297 | 0:e2a155f50119 | 51 | void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); |
star297 | 2:d4c6bc0f2dc4 | 52 | void gettime(),gettemp(),getbattery(),setRTC(),beep(); |
star297 | 0:e2a155f50119 | 53 | |
star297 | 1:71ed1afbf344 | 54 | // manual set RTC values |
star297 | 3:f7febfa77784 | 55 | int minute =20; // 0-59 |
star297 | 3:f7febfa77784 | 56 | int hour =9; // 2-23 |
star297 | 3:f7febfa77784 | 57 | int dayofmonth =20; // 1-31 |
star297 | 1:71ed1afbf344 | 58 | int month =2; // 1-12 |
star297 | 1:71ed1afbf344 | 59 | int year =15; // last 2 digits |
star297 | 2:d4c6bc0f2dc4 | 60 | |
star297 | 3:f7febfa77784 | 61 | int port =8266; // set server port |
star297 | 3:f7febfa77784 | 62 | int SERVtimeout =5; // set server timeout in seconds incase link breaks. |
star297 | 1:71ed1afbf344 | 63 | |
star297 | 0:e2a155f50119 | 64 | // Serial Interrupt read ESP data |
star297 | 0:e2a155f50119 | 65 | void callback() { |
star297 | 3:f7febfa77784 | 66 | while (esp.readable()) {webbuff[count] = esp.getc();count++;} |
star297 | 3:f7febfa77784 | 67 | if(strlen(webbuff)>bufflen){DataRX=1;} |
star297 | 0:e2a155f50119 | 68 | } |
star297 | 0:e2a155f50119 | 69 | |
star297 | 0:e2a155f50119 | 70 | int main() { |
star297 | 0:e2a155f50119 | 71 | led1=1,led2=1,led3=1; |
star297 | 0:e2a155f50119 | 72 | pc.baud(115200); |
star297 | 3:f7febfa77784 | 73 | esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F |
star297 | 1:71ed1afbf344 | 74 | if (time(NULL) < 1420070400) {setRTC();} |
star297 | 2:d4c6bc0f2dc4 | 75 | beep(); |
star297 | 0:e2a155f50119 | 76 | startserver(); |
star297 | 0:e2a155f50119 | 77 | |
star297 | 3:f7febfa77784 | 78 | while(1){ |
star297 | 0:e2a155f50119 | 79 | if(DataRX==1){ |
star297 | 0:e2a155f50119 | 80 | ReadWebData(); |
star297 | 2:d4c6bc0f2dc4 | 81 | beep(); |
star297 | 3:f7febfa77784 | 82 | if (servreq == 1 && weberror == 0){sendpage();} |
star297 | 3:f7febfa77784 | 83 | esp.attach(&callback); |
star297 | 0:e2a155f50119 | 84 | 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); |
star297 | 0:e2a155f50119 | 85 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
star297 | 0:e2a155f50119 | 86 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
star297 | 0:e2a155f50119 | 87 | pc.printf(" -------------------------------------\n\n"); |
star297 | 2:d4c6bc0f2dc4 | 88 | strcpy(lasthit, timebuf); |
star297 | 0:e2a155f50119 | 89 | servreq=0; |
star297 | 3:f7febfa77784 | 90 | } |
star297 | 0:e2a155f50119 | 91 | } |
star297 | 0:e2a155f50119 | 92 | } |
star297 | 0:e2a155f50119 | 93 | // Static WEB page |
star297 | 0:e2a155f50119 | 94 | void sendpage() |
star297 | 0:e2a155f50119 | 95 | { |
star297 | 3:f7febfa77784 | 96 | gettemp(); |
star297 | 3:f7febfa77784 | 97 | getbattery();gettime(); |
star297 | 3:f7febfa77784 | 98 | |
star297 | 2:d4c6bc0f2dc4 | 99 | // WEB page data |
star297 | 3:f7febfa77784 | 100 | strcpy(webbuff, "<!DOCTYPE html>"); |
star297 | 3:f7febfa77784 | 101 | strcat(webbuff, "<html><head><title>ESP8266 Mbed</title></head>"); |
star297 | 3:f7febfa77784 | 102 | strcat(webbuff, "<body>"); |
star297 | 3:f7febfa77784 | 103 | strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed Web Controller</h1>"); |
star297 | 3:f7febfa77784 | 104 | strcat(webbuff, "Hit Count - "); |
star297 | 3:f7febfa77784 | 105 | strcat(webbuff, webcount); |
star297 | 3:f7febfa77784 | 106 | strcat(webbuff, "<br>Last Hit - "); |
star297 | 3:f7febfa77784 | 107 | strcat(webbuff, lasthit); |
star297 | 3:f7febfa77784 | 108 | strcat(webbuff, "</div><br /><hr>"); |
star297 | 3:f7febfa77784 | 109 | strcat(webbuff, "<h3>Mbed RTC Time -  "); |
star297 | 3:f7febfa77784 | 110 | strcat(webbuff, timebuf); |
star297 | 3:f7febfa77784 | 111 | strcat(webbuff, "</h3>\r\n"); |
star297 | 3:f7febfa77784 | 112 | strcat(webbuff, "<p><form method=\"POST\"><strong> Temperature:  <input type=\"text\" size=6 value=\""); |
star297 | 3:f7febfa77784 | 113 | strcat(webbuff, Temp); |
star297 | 3:f7febfa77784 | 114 | strcat(webbuff, "\"> <sup>O</sup>C <form method=\"POST\"> <strong>   Battery:  <input type=\"text\" size=4 value=\""); |
star297 | 3:f7febfa77784 | 115 | strcat(webbuff, Vcc); |
star297 | 3:f7febfa77784 | 116 | strcat(webbuff, "\"> </sup>V"); |
star297 | 3:f7febfa77784 | 117 | if(led1==1){strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> Red LED off"); |
star297 | 3:f7febfa77784 | 118 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > Red LED on");} |
star297 | 3:f7febfa77784 | 119 | else{strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > Red LED off"); |
star297 | 3:f7febfa77784 | 120 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> Red LED on");} |
star297 | 3:f7febfa77784 | 121 | if(Out1==0){strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked> Digital Out 1 off"); |
star297 | 3:f7febfa77784 | 122 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" > Digital Out 1 on");} |
star297 | 3:f7febfa77784 | 123 | else{strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" > Digital Out 1 off"); |
star297 | 3:f7febfa77784 | 124 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked> Digital Out 1 on");} |
star297 | 3:f7febfa77784 | 125 | if(Out2==0){strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked> Digital Out 2 off"); |
star297 | 3:f7febfa77784 | 126 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" > Digital Out 2 on");} |
star297 | 3:f7febfa77784 | 127 | else{strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" > Digital Out 2 off"); |
star297 | 3:f7febfa77784 | 128 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked> Digital Out 2 on");} |
star297 | 3:f7febfa77784 | 129 | if(Out3==0){strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked> Digital Out 3 off"); |
star297 | 3:f7febfa77784 | 130 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" > Digital Out 3 on");} |
star297 | 3:f7febfa77784 | 131 | else{strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" > Digital Out 3 off"); |
star297 | 3:f7febfa77784 | 132 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked> Digital Out 3 on");} |
star297 | 3:f7febfa77784 | 133 | if(In1==0){strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1");} |
star297 | 3:f7febfa77784 | 134 | else{strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1");} |
star297 | 3:f7febfa77784 | 135 | if(In2==0){strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"0\" > Digital In 2");} |
star297 | 3:f7febfa77784 | 136 | else{strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked> Digital In 2");} |
star297 | 3:f7febfa77784 | 137 | if(In3==0){strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"0\" > Digital In 3");} |
star297 | 3:f7febfa77784 | 138 | else{strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked> Digital In 3");} |
star297 | 3:f7febfa77784 | 139 | strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;"); |
star297 | 3:f7febfa77784 | 140 | strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);"); |
star297 | 3:f7febfa77784 | 141 | strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);"); |
star297 | 3:f7febfa77784 | 142 | strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:"); |
star297 | 3:f7febfa77784 | 143 | strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); |
star297 | 3:f7febfa77784 | 144 | strcat(webbuff, "background: #3cb0fd;"); |
star297 | 3:f7febfa77784 | 145 | strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); |
star297 | 3:f7febfa77784 | 146 | strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);"); |
star297 | 3:f7febfa77784 | 147 | strcat(webbuff, "text-decoration:none;\"></form></span>"); |
star297 | 3:f7febfa77784 | 148 | strcat(webbuff, "<p/><h2>How to use:</h2><ul>"); |
star297 | 3:f7febfa77784 | 149 | strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>"); |
star297 | 3:f7febfa77784 | 150 | strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>"); |
star297 | 3:f7febfa77784 | 151 | strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>"); |
star297 | 3:f7febfa77784 | 152 | strcat(webbuff, "</ul>"); |
star297 | 3:f7febfa77784 | 153 | strcat(webbuff, "</body></html>"); |
star297 | 0:e2a155f50119 | 154 | // end of WEB page data |
star297 | 3:f7febfa77784 | 155 | bufl = strlen(webbuff); // get total page buffer length |
star297 | 3:f7febfa77784 | 156 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
star297 | 3:f7febfa77784 | 157 | timeout=200;getcount=7; |
star297 | 3:f7febfa77784 | 158 | SendCMD(); |
star297 | 3:f7febfa77784 | 159 | getreply(); |
star297 | 0:e2a155f50119 | 160 | SendWEB(); // send web page |
star297 | 3:f7febfa77784 | 161 | memset(webbuff, '\0', sizeof(webbuff)); |
star297 | 0:e2a155f50119 | 162 | sendcheck(); |
star297 | 0:e2a155f50119 | 163 | } |
star297 | 0:e2a155f50119 | 164 | |
star297 | 3:f7febfa77784 | 165 | // wait for ESP "SEND OK" reply, then close IP to load web page |
star297 | 0:e2a155f50119 | 166 | void sendcheck() |
star297 | 0:e2a155f50119 | 167 | { |
star297 | 3:f7febfa77784 | 168 | weberror=1;timeout=500;getcount=24; |
star297 | 3:f7febfa77784 | 169 | t2.reset();t2.start(); |
star297 | 3:f7febfa77784 | 170 | while(weberror==1 && t2.read() <5){ |
star297 | 0:e2a155f50119 | 171 | getreply(); |
star297 | 3:f7febfa77784 | 172 | if (strstr(replybuff, "SEND OK") != NULL) {weberror=0;} // wait for valid SEND OK |
star297 | 0:e2a155f50119 | 173 | } |
star297 | 3:f7febfa77784 | 174 | if(weberror==1){ // restart connection |
star297 | 3:f7febfa77784 | 175 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
star297 | 3:f7febfa77784 | 176 | timeout=500;getcount=10; |
star297 | 3:f7febfa77784 | 177 | SendCMD();getreply(); |
star297 | 3:f7febfa77784 | 178 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
star297 | 3:f7febfa77784 | 179 | timeout=500;getcount=10; |
star297 | 3:f7febfa77784 | 180 | SendCMD();getreply(); |
star297 | 3:f7febfa77784 | 181 | } |
star297 | 3:f7febfa77784 | 182 | else{ |
star297 | 3:f7febfa77784 | 183 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
star297 | 3:f7febfa77784 | 184 | SendCMD();} |
star297 | 3:f7febfa77784 | 185 | t2.reset(); |
star297 | 0:e2a155f50119 | 186 | } |
star297 | 0:e2a155f50119 | 187 | |
star297 | 0:e2a155f50119 | 188 | // Reads and processes GET and POST web data |
star297 | 0:e2a155f50119 | 189 | void ReadWebData() |
star297 | 0:e2a155f50119 | 190 | { |
star297 | 3:f7febfa77784 | 191 | wait_ms(200); |
star297 | 3:f7febfa77784 | 192 | esp.attach(NULL); |
star297 | 3:f7febfa77784 | 193 | count=0;DataRX=0;weberror=0; |
star297 | 3:f7febfa77784 | 194 | memset(webdata, '\0', sizeof(webdata)); |
star297 | 3:f7febfa77784 | 195 | int x = strcspn (webbuff,"+"); |
star297 | 0:e2a155f50119 | 196 | if(x){ |
star297 | 3:f7febfa77784 | 197 | strcpy(webdata, webbuff + x);weberror=0; |
star297 | 0:e2a155f50119 | 198 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
star297 | 0:e2a155f50119 | 199 | if( strstr(webdata, "led1=1") != NULL ) {led1=0;} |
star297 | 0:e2a155f50119 | 200 | if( strstr(webdata, "led1=0") != NULL ) {led1=1;} |
star297 | 0:e2a155f50119 | 201 | if( strstr(webdata, "Out1=1") != NULL ) {Out1=1;} |
star297 | 0:e2a155f50119 | 202 | if( strstr(webdata, "Out1=0") != NULL ) {Out1=0;} |
star297 | 0:e2a155f50119 | 203 | if( strstr(webdata, "Out2=1") != NULL ) {Out2=1;} |
star297 | 0:e2a155f50119 | 204 | if( strstr(webdata, "Out2=0") != NULL ) {Out2=0;} |
star297 | 0:e2a155f50119 | 205 | if( strstr(webdata, "Out3=1") != NULL ) {Out3=1;} |
star297 | 0:e2a155f50119 | 206 | if( strstr(webdata, "Out3=0") != NULL ) {Out3=0;} |
star297 | 0:e2a155f50119 | 207 | sprintf(channel, "%d",linkID); |
star297 | 0:e2a155f50119 | 208 | if (strstr(webdata, "GET") != NULL) {servreq=1;} |
star297 | 0:e2a155f50119 | 209 | if (strstr(webdata, "POST") != NULL) {servreq=1;} |
star297 | 2:d4c6bc0f2dc4 | 210 | webcounter++; |
star297 | 2:d4c6bc0f2dc4 | 211 | sprintf(webcount, "%d",webcounter); |
star297 | 0:e2a155f50119 | 212 | } |
star297 | 3:f7febfa77784 | 213 | else { |
star297 | 3:f7febfa77784 | 214 | memset(webbuff, '\0', sizeof(webbuff)); |
star297 | 3:f7febfa77784 | 215 | esp.attach(&callback);weberror=1; |
star297 | 3:f7febfa77784 | 216 | } |
star297 | 0:e2a155f50119 | 217 | } |
star297 | 0:e2a155f50119 | 218 | // Starts and restarts webserver if errors detected. |
star297 | 0:e2a155f50119 | 219 | void startserver() |
star297 | 0:e2a155f50119 | 220 | { |
star297 | 3:f7febfa77784 | 221 | gettemp();gettime(); |
star297 | 1:71ed1afbf344 | 222 | pc.printf("\n\n RTC time %s\r\n\n",timebuf); |
star297 | 1:71ed1afbf344 | 223 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
star297 | 3:f7febfa77784 | 224 | strcpy(cmdbuff,"AT+RST\r\n"); |
star297 | 3:f7febfa77784 | 225 | timeout=2000;getcount=600; |
star297 | 0:e2a155f50119 | 226 | SendCMD(); |
star297 | 1:71ed1afbf344 | 227 | getreply(); |
star297 | 3:f7febfa77784 | 228 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 229 | pc.printf("%d",count); |
star297 | 3:f7febfa77784 | 230 | if (strstr(replybuff, "OK") != NULL) { |
star297 | 1:71ed1afbf344 | 231 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
star297 | 3:f7febfa77784 | 232 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections. |
star297 | 3:f7febfa77784 | 233 | timeout=500;getcount=10; |
star297 | 1:71ed1afbf344 | 234 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 235 | getreply(); |
star297 | 3:f7febfa77784 | 236 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
star297 | 3:f7febfa77784 | 237 | timeout=500;getcount=10; |
star297 | 1:71ed1afbf344 | 238 | SendCMD(); |
star297 | 3:f7febfa77784 | 239 | getreply(); |
star297 | 3:f7febfa77784 | 240 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
star297 | 3:f7febfa77784 | 241 | timeout=500;getcount=50; |
star297 | 3:f7febfa77784 | 242 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 243 | getreply(); |
star297 | 1:71ed1afbf344 | 244 | pc.printf("\n Getting Server IP \r\n"); |
star297 | 3:f7febfa77784 | 245 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
star297 | 2:d4c6bc0f2dc4 | 246 | timeout=1000;getcount=50; |
star297 | 1:71ed1afbf344 | 247 | while(weberror==0){ |
star297 | 1:71ed1afbf344 | 248 | SendCMD();getreply(); |
star297 | 3:f7febfa77784 | 249 | if (strstr(replybuff, "0.0.0.0") == NULL) {weberror=1;} // wait for valid IP |
star297 | 1:71ed1afbf344 | 250 | } |
star297 | 1:71ed1afbf344 | 251 | pc.printf("\n Enter WEB address in your browser \r\n\n"); |
star297 | 3:f7febfa77784 | 252 | replybuff[strlen(replybuff) - 8] = '\0'; |
star297 | 3:f7febfa77784 | 253 | char* IP = replybuff + 5; |
star297 | 3:f7febfa77784 | 254 | sprintf(webdata," http://%s:%d", IP, port); |
star297 | 3:f7febfa77784 | 255 | pc.printf(webdata); |
star297 | 1:71ed1afbf344 | 256 | led2=0;wait(2);led2=1; |
star297 | 3:f7febfa77784 | 257 | bufflen=200;count=0; |
star297 | 1:71ed1afbf344 | 258 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
star297 | 1:71ed1afbf344 | 259 | esp.attach(&callback); |
star297 | 0:e2a155f50119 | 260 | } |
star297 | 1:71ed1afbf344 | 261 | else{ |
star297 | 1:71ed1afbf344 | 262 | pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); |
star297 | 1:71ed1afbf344 | 263 | while(1){} |
star297 | 2:d4c6bc0f2dc4 | 264 | } |
star297 | 2:d4c6bc0f2dc4 | 265 | t2.reset();t2.start();beep(); |
star297 | 0:e2a155f50119 | 266 | } |
star297 | 0:e2a155f50119 | 267 | // ESP Command data send |
star297 | 0:e2a155f50119 | 268 | void SendCMD() |
star297 | 0:e2a155f50119 | 269 | { |
star297 | 3:f7febfa77784 | 270 | esp.printf("%s", cmdbuff); |
star297 | 0:e2a155f50119 | 271 | } |
star297 | 0:e2a155f50119 | 272 | // Large WEB buffer data send |
star297 | 0:e2a155f50119 | 273 | void SendWEB() |
star297 | 0:e2a155f50119 | 274 | { |
star297 | 0:e2a155f50119 | 275 | int i=0; |
star297 | 0:e2a155f50119 | 276 | if(esp.writeable()) { |
star297 | 3:f7febfa77784 | 277 | while(webbuff[i]!='\0') {esp.putc(webbuff[i]);i++;} |
star297 | 0:e2a155f50119 | 278 | } |
star297 | 0:e2a155f50119 | 279 | } |
star297 | 0:e2a155f50119 | 280 | // Get Cammand and ESP status replies |
star297 | 0:e2a155f50119 | 281 | void getreply() |
star297 | 0:e2a155f50119 | 282 | { |
star297 | 3:f7febfa77784 | 283 | memset(replybuff, '\0', sizeof(replybuff)); |
star297 | 3:f7febfa77784 | 284 | t1.reset(); t1.start();replycount=0; |
star297 | 3:f7febfa77784 | 285 | while(t1.read_ms()< timeout && replycount < getcount) { |
star297 | 0:e2a155f50119 | 286 | if(esp.readable()) { |
star297 | 3:f7febfa77784 | 287 | replybuff[replycount] = esp.getc();replycount++; |
star297 | 0:e2a155f50119 | 288 | } |
star297 | 2:d4c6bc0f2dc4 | 289 | } |
star297 | 2:d4c6bc0f2dc4 | 290 | t1.stop(); |
star297 | 0:e2a155f50119 | 291 | } |
star297 | 0:e2a155f50119 | 292 | // Analog in example |
star297 | 0:e2a155f50119 | 293 | void getbattery() |
star297 | 0:e2a155f50119 | 294 | { |
star297 | 0:e2a155f50119 | 295 | AdcIn=BATin.read(); |
star297 | 2:d4c6bc0f2dc4 | 296 | Ht = (AdcIn*3.328f*(R1+R2)/R2); // set the numeric to the exact MCU analog reference voltage for greater accuracy |
star297 | 0:e2a155f50119 | 297 | sprintf(Vcc,"%2.3f",Ht); |
star297 | 0:e2a155f50119 | 298 | } |
star297 | 0:e2a155f50119 | 299 | // Temperature example |
star297 | 0:e2a155f50119 | 300 | void gettemp() |
star297 | 2:d4c6bc0f2dc4 | 301 | { |
star297 | 0:e2a155f50119 | 302 | temperature=thermom.GetTemperature(); // comment this out if no sensor connected |
star297 | 0:e2a155f50119 | 303 | //temperature = 21.357; // include for dummy value |
star297 | 0:e2a155f50119 | 304 | sprintf(Temp,"%3.3f",temperature); |
star297 | 0:e2a155f50119 | 305 | } |
star297 | 0:e2a155f50119 | 306 | // Get RTC time |
star297 | 0:e2a155f50119 | 307 | void gettime() |
star297 | 0:e2a155f50119 | 308 | { |
star297 | 0:e2a155f50119 | 309 | time_t seconds = time(NULL); |
star297 | 0:e2a155f50119 | 310 | strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds)); |
star297 | 0:e2a155f50119 | 311 | } |
star297 | 0:e2a155f50119 | 312 | |
star297 | 2:d4c6bc0f2dc4 | 313 | void beep() |
star297 | 2:d4c6bc0f2dc4 | 314 | { |
star297 | 2:d4c6bc0f2dc4 | 315 | speaker.period(1.0/2000); // 2000hz period |
star297 | 2:d4c6bc0f2dc4 | 316 | speaker = 0.5; //50% duty cycle - max volume |
star297 | 2:d4c6bc0f2dc4 | 317 | wait_ms(60); |
star297 | 2:d4c6bc0f2dc4 | 318 | speaker=0.0; // turn off audio |
star297 | 2:d4c6bc0f2dc4 | 319 | } |
star297 | 2:d4c6bc0f2dc4 | 320 | |
star297 | 1:71ed1afbf344 | 321 | void setRTC() |
star297 | 1:71ed1afbf344 | 322 | { |
star297 | 1:71ed1afbf344 | 323 | t.tm_sec = (0); // 0-59 |
star297 | 1:71ed1afbf344 | 324 | t.tm_min = (minute); // 0-59 |
star297 | 1:71ed1afbf344 | 325 | t.tm_hour = (hour); // 0-23 |
star297 | 1:71ed1afbf344 | 326 | t.tm_mday = (dayofmonth); // 1-31 |
star297 | 1:71ed1afbf344 | 327 | t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format |
star297 | 1:71ed1afbf344 | 328 | t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year |
star297 | 1:71ed1afbf344 | 329 | set_time(mktime(&t)); // set RTC clock |
star297 | 1:71ed1afbf344 | 330 | } |