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@2:d4c6bc0f2dc4, 2015-02-19 (annotated)
- Committer:
- star297
- Date:
- Thu Feb 19 23:47:54 2015 +0000
- Revision:
- 2:d4c6bc0f2dc4
- Parent:
- 1:71ed1afbf344
- Child:
- 3:f7febfa77784
WEB page update, added pwm speaker
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 | 2:d4c6bc0f2dc4 | 33 | int buflen, DataRX, count, getcount, ended, 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 | 2:d4c6bc0f2dc4 | 41 | char lasthit[60]; |
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 | 0:e2a155f50119 | 46 | char snd[256]; |
star297 | 0:e2a155f50119 | 47 | char webdata[1024]; // This may need to be bigger depending on WEB browser used |
star297 | 2:d4c6bc0f2dc4 | 48 | char buf[4096]; // Currently using 1407 characters, Increase this if more web page data added |
star297 | 0:e2a155f50119 | 49 | |
star297 | 0:e2a155f50119 | 50 | void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); |
star297 | 2:d4c6bc0f2dc4 | 51 | void gettime(),gettemp(),getbattery(),setRTC(),beep(); |
star297 | 0:e2a155f50119 | 52 | |
star297 | 1:71ed1afbf344 | 53 | // manual set RTC values |
star297 | 2:d4c6bc0f2dc4 | 54 | int minute =13; // 0-59 |
star297 | 2:d4c6bc0f2dc4 | 55 | int hour =15; // 2-23 |
star297 | 2:d4c6bc0f2dc4 | 56 | int dayofmonth =19; // 1-31 |
star297 | 1:71ed1afbf344 | 57 | int month =2; // 1-12 |
star297 | 1:71ed1afbf344 | 58 | int year =15; // last 2 digits |
star297 | 2:d4c6bc0f2dc4 | 59 | |
star297 | 2:d4c6bc0f2dc4 | 60 | int port =90; // set server port |
star297 | 1:71ed1afbf344 | 61 | |
star297 | 0:e2a155f50119 | 62 | // Serial Interrupt read ESP data |
star297 | 0:e2a155f50119 | 63 | void callback() { |
star297 | 0:e2a155f50119 | 64 | while (esp.readable()) {buf[count] = esp.getc();count++;} |
star297 | 0:e2a155f50119 | 65 | if(strlen(buf)>buflen){DataRX=1;} |
star297 | 0:e2a155f50119 | 66 | } |
star297 | 0:e2a155f50119 | 67 | |
star297 | 0:e2a155f50119 | 68 | int main() { |
star297 | 0:e2a155f50119 | 69 | led1=1,led2=1,led3=1; |
star297 | 0:e2a155f50119 | 70 | pc.baud(115200); |
star297 | 0:e2a155f50119 | 71 | esp.baud(115200); // ESP8266 baudrate. |
star297 | 1:71ed1afbf344 | 72 | if (time(NULL) < 1420070400) {setRTC();} |
star297 | 2:d4c6bc0f2dc4 | 73 | beep(); |
star297 | 0:e2a155f50119 | 74 | startserver(); |
star297 | 0:e2a155f50119 | 75 | |
star297 | 0:e2a155f50119 | 76 | while(1){ |
star297 | 0:e2a155f50119 | 77 | |
star297 | 0:e2a155f50119 | 78 | if(DataRX==1){ |
star297 | 0:e2a155f50119 | 79 | ReadWebData(); |
star297 | 2:d4c6bc0f2dc4 | 80 | beep(); |
star297 | 0:e2a155f50119 | 81 | if (servreq == 1 && weberror == 0){sendpage();} |
star297 | 0:e2a155f50119 | 82 | 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 | 83 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
star297 | 0:e2a155f50119 | 84 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
star297 | 0:e2a155f50119 | 85 | pc.printf(" -------------------------------------\n\n"); |
star297 | 0:e2a155f50119 | 86 | esp.attach(&callback); |
star297 | 2:d4c6bc0f2dc4 | 87 | strcpy(lasthit, timebuf); |
star297 | 0:e2a155f50119 | 88 | servreq=0; |
star297 | 2:d4c6bc0f2dc4 | 89 | } |
star297 | 2:d4c6bc0f2dc4 | 90 | if(t2.read()>900){startserver();} // restart server every 15 minutes after no activity in case of ESP lock out. |
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 | 2:d4c6bc0f2dc4 | 96 | gettemp();getbattery();gettime(); |
star297 | 0:e2a155f50119 | 97 | memset(buf, '\0', sizeof(buf)); |
star297 | 2:d4c6bc0f2dc4 | 98 | |
star297 | 2:d4c6bc0f2dc4 | 99 | // WEB page data |
star297 | 2:d4c6bc0f2dc4 | 100 | strcpy(buf, "<!DOCTYPE html>"); |
star297 | 2:d4c6bc0f2dc4 | 101 | strcat(buf, "<html><head><title>ESP8266 Mbed</title></head>"); |
star297 | 2:d4c6bc0f2dc4 | 102 | strcat(buf, "<body>"); |
star297 | 2:d4c6bc0f2dc4 | 103 | strcat(buf, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed Web Controller</h1>"); |
star297 | 2:d4c6bc0f2dc4 | 104 | strcat(buf, "Hit Count - "); |
star297 | 2:d4c6bc0f2dc4 | 105 | strcat(buf, webcount); |
star297 | 2:d4c6bc0f2dc4 | 106 | strcat(buf, "<br>Last Hit - "); |
star297 | 2:d4c6bc0f2dc4 | 107 | strcat(buf, lasthit); |
star297 | 2:d4c6bc0f2dc4 | 108 | strcat(buf, "</div><br /><hr>"); |
star297 | 0:e2a155f50119 | 109 | strcat(buf, "<h3>Mbed RTC Time - "); |
star297 | 0:e2a155f50119 | 110 | strcat(buf, timebuf); |
star297 | 0:e2a155f50119 | 111 | strcat(buf, "</h3>\r\n"); |
star297 | 2:d4c6bc0f2dc4 | 112 | strcat(buf, "<p><form method=\"POST\"><strong> Temperature: <input type=\"text\" size=6 value=\""); |
star297 | 0:e2a155f50119 | 113 | strcat(buf, Temp); |
star297 | 0:e2a155f50119 | 114 | strcat(buf, "\"> <sup>O</sup>C <form method=\"POST\"> <strong> Battery: <input type=\"text\" size=4 value=\""); |
star297 | 0:e2a155f50119 | 115 | strcat(buf, Vcc); |
star297 | 0:e2a155f50119 | 116 | strcat(buf, "\"> </sup>V"); |
star297 | 0:e2a155f50119 | 117 | if(led1==1){strcat(buf, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> Red LED off"); |
star297 | 0:e2a155f50119 | 118 | strcat(buf, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > Red LED on");} |
star297 | 0:e2a155f50119 | 119 | else{strcat(buf, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > Red LED off"); |
star297 | 0:e2a155f50119 | 120 | strcat(buf, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> Red LED on");} |
star297 | 0:e2a155f50119 | 121 | if(Out1==0){strcat(buf, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked> Digital Out 1 off"); |
star297 | 0:e2a155f50119 | 122 | strcat(buf, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" > Digital Out 1 on");} |
star297 | 0:e2a155f50119 | 123 | else{strcat(buf, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" > Digital Out 1 off"); |
star297 | 0:e2a155f50119 | 124 | strcat(buf, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked> Digital Out 1 on");} |
star297 | 0:e2a155f50119 | 125 | if(Out2==0){strcat(buf, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked> Digital Out 2 off"); |
star297 | 0:e2a155f50119 | 126 | strcat(buf, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" > Digital Out 2 on");} |
star297 | 0:e2a155f50119 | 127 | else{strcat(buf, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" > Digital Out 2 off"); |
star297 | 0:e2a155f50119 | 128 | strcat(buf, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked> Digital Out 2 on");} |
star297 | 0:e2a155f50119 | 129 | if(Out3==0){strcat(buf, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked> Digital Out 3 off"); |
star297 | 0:e2a155f50119 | 130 | strcat(buf, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" > Digital Out 3 on");} |
star297 | 0:e2a155f50119 | 131 | else{strcat(buf, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" > Digital Out 3 off"); |
star297 | 0:e2a155f50119 | 132 | strcat(buf, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked> Digital Out 3 on");} |
star297 | 0:e2a155f50119 | 133 | if(In1==0){strcat(buf, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1");} |
star297 | 0:e2a155f50119 | 134 | else{strcat(buf, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1");} |
star297 | 0:e2a155f50119 | 135 | if(In2==0){strcat(buf, "<br><input type=\"radio\" name=\"In2\" value=\"0\" > Digital In 2");} |
star297 | 0:e2a155f50119 | 136 | else{strcat(buf, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked> Digital In 2");} |
star297 | 0:e2a155f50119 | 137 | if(In3==0){strcat(buf, "<br><input type=\"radio\" name=\"In3\" value=\"0\" > Digital In 3");} |
star297 | 2:d4c6bc0f2dc4 | 138 | else{strcat(buf, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked> Digital In 3");} |
star297 | 2:d4c6bc0f2dc4 | 139 | strcat(buf, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;"); |
star297 | 2:d4c6bc0f2dc4 | 140 | strcat(buf, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);"); |
star297 | 2:d4c6bc0f2dc4 | 141 | strcat(buf, "background-image:linear-gradient(to bottom, #3498db, #2980b9);"); |
star297 | 2:d4c6bc0f2dc4 | 142 | strcat(buf, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:"); |
star297 | 2:d4c6bc0f2dc4 | 143 | strcat(buf, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); |
star297 | 2:d4c6bc0f2dc4 | 144 | strcat(buf, "background: #3cb0fd;"); |
star297 | 2:d4c6bc0f2dc4 | 145 | strcat(buf, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); |
star297 | 2:d4c6bc0f2dc4 | 146 | strcat(buf, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);"); |
star297 | 2:d4c6bc0f2dc4 | 147 | strcat(buf, "text-decoration:none;\"></form></span>"); |
star297 | 0:e2a155f50119 | 148 | strcat(buf, "<p/><h2>How to use:</h2><ul>"); |
star297 | 0:e2a155f50119 | 149 | strcat(buf, "<li>Select the Radio buttons to control the digital out pins.</li>"); |
star297 | 2:d4c6bc0f2dc4 | 150 | strcat(buf, "<li>Click 'Send-Refresh' to send.</li>"); |
star297 | 2:d4c6bc0f2dc4 | 151 | strcat(buf, "<li>Use the 'Send-Refresh' button to refresh the data.</li>"); |
star297 | 0:e2a155f50119 | 152 | strcat(buf, "</ul>"); |
star297 | 2:d4c6bc0f2dc4 | 153 | strcat(buf, "</body></html>"); |
star297 | 0:e2a155f50119 | 154 | // end of WEB page data |
star297 | 0:e2a155f50119 | 155 | bufl = strlen(buf); // get total page buffer length |
star297 | 0:e2a155f50119 | 156 | sprintf(snd,"AT+CIPSEND=%s,%d\r\n", channel, bufl); // send IPD link channel and buffer character length. |
star297 | 0:e2a155f50119 | 157 | SendCMD(); |
star297 | 0:e2a155f50119 | 158 | wait_ms(100); |
star297 | 0:e2a155f50119 | 159 | SendWEB(); // send web page |
star297 | 2:d4c6bc0f2dc4 | 160 | memset(buf, '\0', sizeof(buf)); |
star297 | 0:e2a155f50119 | 161 | sendcheck(); |
star297 | 0:e2a155f50119 | 162 | } |
star297 | 0:e2a155f50119 | 163 | |
star297 | 0:e2a155f50119 | 164 | // wait for ESP "SEND OK" reply, then close IP to load web page, if lock out, system fully restarts |
star297 | 0:e2a155f50119 | 165 | void sendcheck() |
star297 | 0:e2a155f50119 | 166 | { |
star297 | 0:e2a155f50119 | 167 | timeout=200; |
star297 | 1:71ed1afbf344 | 168 | weberror=1;t2.reset();t2.start(); |
star297 | 1:71ed1afbf344 | 169 | while(weberror==1 && t2.read() <10){ |
star297 | 0:e2a155f50119 | 170 | getreply(); |
star297 | 0:e2a155f50119 | 171 | if (strstr(buf, "SEND OK") != NULL) {weberror=0;} // wait for valid SEND OK |
star297 | 0:e2a155f50119 | 172 | } |
star297 | 2:d4c6bc0f2dc4 | 173 | t2.reset(); |
star297 | 0:e2a155f50119 | 174 | if(weberror==1){ |
star297 | 0:e2a155f50119 | 175 | pc.printf("\n\n\n ++++++++++ WEB Connect Error, restarting ... ++++++++++\n\n"); |
star297 | 0:e2a155f50119 | 176 | NVIC_SystemReset();} |
star297 | 0:e2a155f50119 | 177 | strcpy(snd, "AT+CIPCLOSE=0\r\n"); |
star297 | 0:e2a155f50119 | 178 | SendCMD(); |
star297 | 0:e2a155f50119 | 179 | } |
star297 | 0:e2a155f50119 | 180 | |
star297 | 0:e2a155f50119 | 181 | // Reads and processes GET and POST web data |
star297 | 0:e2a155f50119 | 182 | void ReadWebData() |
star297 | 0:e2a155f50119 | 183 | { |
star297 | 0:e2a155f50119 | 184 | wait_ms(200); |
star297 | 0:e2a155f50119 | 185 | led3=0; |
star297 | 0:e2a155f50119 | 186 | count=0;DataRX=0;weberror=0; |
star297 | 0:e2a155f50119 | 187 | esp.attach(NULL); |
star297 | 0:e2a155f50119 | 188 | int x = strcspn (buf,"+"); |
star297 | 0:e2a155f50119 | 189 | if(x){ |
star297 | 0:e2a155f50119 | 190 | strcpy(webdata, buf + x);weberror=0; |
star297 | 0:e2a155f50119 | 191 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
star297 | 0:e2a155f50119 | 192 | if( strstr(webdata, "led1=1") != NULL ) {led1=0;} |
star297 | 0:e2a155f50119 | 193 | if( strstr(webdata, "led1=0") != NULL ) {led1=1;} |
star297 | 0:e2a155f50119 | 194 | if( strstr(webdata, "Out1=1") != NULL ) {Out1=1;} |
star297 | 0:e2a155f50119 | 195 | if( strstr(webdata, "Out1=0") != NULL ) {Out1=0;} |
star297 | 0:e2a155f50119 | 196 | if( strstr(webdata, "Out2=1") != NULL ) {Out2=1;} |
star297 | 0:e2a155f50119 | 197 | if( strstr(webdata, "Out2=0") != NULL ) {Out2=0;} |
star297 | 0:e2a155f50119 | 198 | if( strstr(webdata, "Out3=1") != NULL ) {Out3=1;} |
star297 | 0:e2a155f50119 | 199 | if( strstr(webdata, "Out3=0") != NULL ) {Out3=0;} |
star297 | 0:e2a155f50119 | 200 | sprintf(channel, "%d",linkID); |
star297 | 0:e2a155f50119 | 201 | if (strstr(webdata, "GET") != NULL) {servreq=1;} |
star297 | 0:e2a155f50119 | 202 | if (strstr(webdata, "POST") != NULL) {servreq=1;} |
star297 | 2:d4c6bc0f2dc4 | 203 | webcounter++; |
star297 | 2:d4c6bc0f2dc4 | 204 | sprintf(webcount, "%d",webcounter); |
star297 | 0:e2a155f50119 | 205 | } |
star297 | 2:d4c6bc0f2dc4 | 206 | else {strcpy(webdata, buf);weberror=1;} |
star297 | 0:e2a155f50119 | 207 | led3=1; |
star297 | 0:e2a155f50119 | 208 | } |
star297 | 0:e2a155f50119 | 209 | // Starts and restarts webserver if errors detected. |
star297 | 0:e2a155f50119 | 210 | void startserver() |
star297 | 0:e2a155f50119 | 211 | { |
star297 | 0:e2a155f50119 | 212 | esp.attach(NULL); |
star297 | 0:e2a155f50119 | 213 | wait(1); |
star297 | 1:71ed1afbf344 | 214 | gettime(); |
star297 | 1:71ed1afbf344 | 215 | pc.printf("\n\n RTC time %s\r\n\n",timebuf); |
star297 | 1:71ed1afbf344 | 216 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
star297 | 0:e2a155f50119 | 217 | strcpy(snd,"AT+RST\r\n"); |
star297 | 0:e2a155f50119 | 218 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 219 | timeout=2000;getcount=336; |
star297 | 1:71ed1afbf344 | 220 | getreply(); |
star297 | 1:71ed1afbf344 | 221 | if (strstr(buf, "ready") != NULL) { |
star297 | 1:71ed1afbf344 | 222 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
star297 | 1:71ed1afbf344 | 223 | strcpy(snd, "AT+CIPMUX=1\r\n"); // set multiple connections. |
star297 | 1:71ed1afbf344 | 224 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 225 | timeout=500;getcount=10; |
star297 | 2:d4c6bc0f2dc4 | 226 | getreply(); |
star297 | 2:d4c6bc0f2dc4 | 227 | strcpy(snd, "AT+CIPSERVER=1,90\r\n"); |
star297 | 2:d4c6bc0f2dc4 | 228 | //sprintf(snd,"AT+CIPSERVER=1,%d\r\n", port); |
star297 | 1:71ed1afbf344 | 229 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 230 | timeout=500;getcount=10; |
star297 | 2:d4c6bc0f2dc4 | 231 | getreply(); |
star297 | 1:71ed1afbf344 | 232 | pc.printf("\n Getting Server IP \r\n"); |
star297 | 1:71ed1afbf344 | 233 | strcpy(snd, "AT+CIFSR\r\n"); |
star297 | 2:d4c6bc0f2dc4 | 234 | timeout=1000;getcount=50; |
star297 | 1:71ed1afbf344 | 235 | while(weberror==0){ |
star297 | 1:71ed1afbf344 | 236 | SendCMD();getreply(); |
star297 | 1:71ed1afbf344 | 237 | if (strstr(buf, "0.0.0.0") == NULL) {weberror=1;} // wait for valid IP |
star297 | 1:71ed1afbf344 | 238 | } |
star297 | 1:71ed1afbf344 | 239 | pc.printf("\n Enter WEB address in your browser \r\n\n"); |
star297 | 1:71ed1afbf344 | 240 | buf[strlen(buf) - 8] = '\0'; |
star297 | 1:71ed1afbf344 | 241 | char* IP = buf + 5; |
star297 | 2:d4c6bc0f2dc4 | 242 | sprintf(snd," http://%s:%d", IP, port); |
star297 | 1:71ed1afbf344 | 243 | pc.printf(snd); |
star297 | 1:71ed1afbf344 | 244 | while (esp.readable()){esp.getc();} |
star297 | 1:71ed1afbf344 | 245 | buflen=200;count=0; |
star297 | 1:71ed1afbf344 | 246 | memset(buf, '\0', sizeof(buf)); |
star297 | 1:71ed1afbf344 | 247 | led2=0;wait(2);led2=1; |
star297 | 1:71ed1afbf344 | 248 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
star297 | 1:71ed1afbf344 | 249 | esp.attach(&callback); |
star297 | 0:e2a155f50119 | 250 | } |
star297 | 1:71ed1afbf344 | 251 | else{ |
star297 | 1:71ed1afbf344 | 252 | pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); |
star297 | 1:71ed1afbf344 | 253 | while(1){} |
star297 | 2:d4c6bc0f2dc4 | 254 | } |
star297 | 2:d4c6bc0f2dc4 | 255 | t2.reset();t2.start();beep(); |
star297 | 0:e2a155f50119 | 256 | } |
star297 | 0:e2a155f50119 | 257 | // ESP Command data send |
star297 | 0:e2a155f50119 | 258 | void SendCMD() |
star297 | 0:e2a155f50119 | 259 | { |
star297 | 0:e2a155f50119 | 260 | esp.printf("%s", snd); |
star297 | 0:e2a155f50119 | 261 | } |
star297 | 0:e2a155f50119 | 262 | // Large WEB buffer data send |
star297 | 0:e2a155f50119 | 263 | void SendWEB() |
star297 | 0:e2a155f50119 | 264 | { |
star297 | 0:e2a155f50119 | 265 | int i=0; |
star297 | 0:e2a155f50119 | 266 | if(esp.writeable()) { |
star297 | 0:e2a155f50119 | 267 | while(buf[i]!='\0') {esp.putc(buf[i]);i++;} |
star297 | 0:e2a155f50119 | 268 | } |
star297 | 0:e2a155f50119 | 269 | } |
star297 | 0:e2a155f50119 | 270 | // Get Cammand and ESP status replies |
star297 | 0:e2a155f50119 | 271 | void getreply() |
star297 | 0:e2a155f50119 | 272 | { |
star297 | 0:e2a155f50119 | 273 | memset(buf, '\0', sizeof(buf)); |
star297 | 2:d4c6bc0f2dc4 | 274 | t1.reset(); t1.start(); |
star297 | 0:e2a155f50119 | 275 | ended=0;count=0; |
star297 | 2:d4c6bc0f2dc4 | 276 | while(t1.read_ms()< timeout && count < getcount) { |
star297 | 0:e2a155f50119 | 277 | if(esp.readable()) { |
star297 | 0:e2a155f50119 | 278 | buf[count] = esp.getc();count++; |
star297 | 0:e2a155f50119 | 279 | } |
star297 | 2:d4c6bc0f2dc4 | 280 | } |
star297 | 2:d4c6bc0f2dc4 | 281 | t1.stop(); |
star297 | 0:e2a155f50119 | 282 | } |
star297 | 0:e2a155f50119 | 283 | // Analog in example |
star297 | 0:e2a155f50119 | 284 | void getbattery() |
star297 | 0:e2a155f50119 | 285 | { |
star297 | 0:e2a155f50119 | 286 | AdcIn=BATin.read(); |
star297 | 2:d4c6bc0f2dc4 | 287 | Ht = (AdcIn*3.328f*(R1+R2)/R2); // set the numeric to the exact MCU analog reference voltage for greater accuracy |
star297 | 0:e2a155f50119 | 288 | sprintf(Vcc,"%2.3f",Ht); |
star297 | 0:e2a155f50119 | 289 | } |
star297 | 0:e2a155f50119 | 290 | // Temperature example |
star297 | 0:e2a155f50119 | 291 | void gettemp() |
star297 | 2:d4c6bc0f2dc4 | 292 | { |
star297 | 0:e2a155f50119 | 293 | temperature=thermom.GetTemperature(); // comment this out if no sensor connected |
star297 | 0:e2a155f50119 | 294 | //temperature = 21.357; // include for dummy value |
star297 | 0:e2a155f50119 | 295 | sprintf(Temp,"%3.3f",temperature); |
star297 | 0:e2a155f50119 | 296 | } |
star297 | 0:e2a155f50119 | 297 | // Get RTC time |
star297 | 0:e2a155f50119 | 298 | void gettime() |
star297 | 0:e2a155f50119 | 299 | { |
star297 | 0:e2a155f50119 | 300 | time_t seconds = time(NULL); |
star297 | 0:e2a155f50119 | 301 | strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds)); |
star297 | 0:e2a155f50119 | 302 | } |
star297 | 0:e2a155f50119 | 303 | |
star297 | 2:d4c6bc0f2dc4 | 304 | void beep() |
star297 | 2:d4c6bc0f2dc4 | 305 | { |
star297 | 2:d4c6bc0f2dc4 | 306 | speaker.period(1.0/2000); // 2000hz period |
star297 | 2:d4c6bc0f2dc4 | 307 | speaker = 0.5; //50% duty cycle - max volume |
star297 | 2:d4c6bc0f2dc4 | 308 | wait_ms(60); |
star297 | 2:d4c6bc0f2dc4 | 309 | speaker=0.0; // turn off audio |
star297 | 2:d4c6bc0f2dc4 | 310 | } |
star297 | 2:d4c6bc0f2dc4 | 311 | |
star297 | 1:71ed1afbf344 | 312 | void setRTC() |
star297 | 1:71ed1afbf344 | 313 | { |
star297 | 1:71ed1afbf344 | 314 | t.tm_sec = (0); // 0-59 |
star297 | 1:71ed1afbf344 | 315 | t.tm_min = (minute); // 0-59 |
star297 | 1:71ed1afbf344 | 316 | t.tm_hour = (hour); // 0-23 |
star297 | 1:71ed1afbf344 | 317 | t.tm_mday = (dayofmonth); // 1-31 |
star297 | 1:71ed1afbf344 | 318 | t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format |
star297 | 1:71ed1afbf344 | 319 | t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year |
star297 | 1:71ed1afbf344 | 320 | set_time(mktime(&t)); // set RTC clock |
star297 | 1:71ed1afbf344 | 321 | } |