esp8266

Dependencies:   DS18B20_1wire mbed

Fork of ESP8266-WEB-Mbed-Controller by Paul Staron

Files at this revision

API Documentation at this revision

Comitter:
star297
Date:
Tue Feb 24 23:58:24 2015 +0000
Parent:
2:d4c6bc0f2dc4
Commit message:
improved web lock out

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r d4c6bc0f2dc4 -r f7febfa77784 main.cpp
--- a/main.cpp	Thu Feb 19 23:47:54 2015 +0000
+++ b/main.cpp	Tue Feb 24 23:58:24 2015 +0000
@@ -30,7 +30,7 @@
 
 struct tm t;
 
-int buflen, DataRX, count, getcount, ended, servreq, timeout;
+int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
 int bufl, ipdLen, linkID, weberror, webcounter;
 float temperature, AdcIn, Ht; 
 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
@@ -38,156 +38,163 @@
 char Temp[10];
 char temp[10];
 char webcount[8];
-char lasthit[60];
+char lasthit[30];
 char timebuf[30];
 char type[16];
 char type1[16];
 char channel[2];
-char snd[256];
+char cmdbuff[32];
+char replybuff[512];
 char webdata[1024]; // This may need to be bigger depending on WEB browser used
-char buf[4096];     // Currently using 1407 characters, Increase this if more web page data added
+char webbuff[4096];     // Currently using 1986 characters, Increase this if more web page data added
 
 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
 void gettime(),gettemp(),getbattery(),setRTC(),beep();
 
 // manual set RTC values
-int minute      =13;    // 0-59
-int hour        =15;    // 2-23
-int dayofmonth  =19;    // 1-31
+int minute      =20;    // 0-59
+int hour        =9;    // 2-23
+int dayofmonth  =20;    // 1-31
 int month       =2;     // 1-12
 int year        =15;    // last 2 digits
 
-int port        =90;  // set server port
+int port        =8266;  // set server port
+int SERVtimeout =5;    // set server timeout in seconds incase link breaks.
  
 // Serial Interrupt read ESP data
 void callback() {    
-    while (esp.readable()) {buf[count] = esp.getc();count++;}
-    if(strlen(buf)>buflen){DataRX=1;}  
+    while (esp.readable()) {webbuff[count] = esp.getc();count++;}
+    if(strlen(webbuff)>bufflen){DataRX=1;}  
 }
 
 int main() {
     led1=1,led2=1,led3=1;
     pc.baud(115200);
-    esp.baud(115200);   // ESP8266 baudrate.
+    esp.baud(115200);   // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
     if (time(NULL) < 1420070400) {setRTC();}    
     beep();
     startserver();
       
-    while(1){        
-        
+    while(1){     
         if(DataRX==1){
             ReadWebData();
             beep();
-            if (servreq == 1 && weberror == 0){sendpage();}    
+            if (servreq == 1 && weberror == 0){sendpage();}
+            esp.attach(&callback);     
             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);        
             pc.printf("\n\n  HTTP Packet: \n\n%s\n", webdata); 
             pc.printf("  Web Characters sent : %d\n\n", bufl);
             pc.printf("  -------------------------------------\n\n");
-            esp.attach(&callback);
             strcpy(lasthit, timebuf);
             servreq=0;               
-        }
-        if(t2.read()>900){startserver();} // restart server every 15 minutes after no activity in case of ESP lock out.                               
+        }                             
     }
 }    
 // Static WEB page    
 void sendpage()
 {          
-    gettemp();getbattery();gettime();   
-    memset(buf, '\0', sizeof(buf));
-    
+    gettemp();
+    getbattery();gettime();  
+        
 // WEB page data     
-    strcpy(buf, "<!DOCTYPE html>");
-    strcat(buf, "<html><head><title>ESP8266 Mbed</title></head>");
-    strcat(buf, "<body>");    
-    strcat(buf, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed Web Controller</h1>");   
-    strcat(buf, "Hit Count - ");
-    strcat(buf, webcount);
-    strcat(buf, "<br>Last Hit - ");
-    strcat(buf, lasthit);                     
-    strcat(buf, "</div><br /><hr>");
-    strcat(buf, "<h3>Mbed RTC Time -  ");             
-    strcat(buf, timebuf);
-    strcat(buf, "</h3>\r\n");
-    strcat(buf, "<p><form method=\"POST\"><strong> Temperature:  <input type=\"text\" size=6 value=\"");
-    strcat(buf, Temp);
-    strcat(buf, "\"> <sup>O</sup>C <form method=\"POST\"> <strong> Battery:  <input type=\"text\" size=4 value=\"");
-    strcat(buf, Vcc);
-    strcat(buf, "\"> </sup>V");    
-    if(led1==1){strcat(buf, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked>  Red LED off");
-                strcat(buf, "<br><input type=\"radio\" name=\"led1\" value=\"1\" >  Red LED on");} 
-        else{strcat(buf, "<p><input type=\"radio\" name=\"led1\" value=\"0\" >  Red LED off");
-                strcat(buf, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked>  Red LED on");}   
-    if(Out1==0){strcat(buf, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked>  Digital Out 1  off");
-                strcat(buf, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" >  Digital Out 1 on");}
-        else{strcat(buf, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" >  Digital Out 1 off");
-                strcat(buf, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked>  Digital Out 1 on");}   
-    if(Out2==0){strcat(buf, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked>  Digital Out 2 off");
-                strcat(buf, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" >  Digital Out 2 on");}
-        else{strcat(buf, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" >  Digital Out 2 off");
-                strcat(buf, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked>  Digital Out 2 on");}           
-    if(Out3==0){strcat(buf, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked>  Digital Out 3 off");
-                strcat(buf, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" >  Digital Out 3 on");}
-        else{strcat(buf, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" >  Digital Out 3 off");
-                strcat(buf, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked>  Digital Out 3 on");}    
-    if(In1==0){strcat(buf, "<p><input type=\"radio\" name=\"In1\" value=\"0\" >  Digital In 1");}
-        else{strcat(buf, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked>  Digital In 1");}   
-    if(In2==0){strcat(buf, "<br><input type=\"radio\" name=\"In2\" value=\"0\" >  Digital In 2");}
-        else{strcat(buf, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked>  Digital In 2");}           
-    if(In3==0){strcat(buf, "<br><input type=\"radio\" name=\"In3\" value=\"0\" >  Digital In 3");}
-        else{strcat(buf, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked>  Digital In 3");}                  
-    strcat(buf, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;");    
-    strcat(buf, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);");    
-    strcat(buf, "background-image:linear-gradient(to bottom, #3498db, #2980b9);");
-    strcat(buf, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:");
-    strcat(buf, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); 
-    strcat(buf, "background: #3cb0fd;");
-    strcat(buf, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); 
-    strcat(buf, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);");
-    strcat(buf, "text-decoration:none;\"></form></span>");
-    strcat(buf, "<p/><h2>How to use:</h2><ul>");
-    strcat(buf, "<li>Select the Radio buttons to control the digital out pins.</li>");
-    strcat(buf, "<li>Click 'Send-Refresh' to send.</li>");      
-    strcat(buf, "<li>Use the 'Send-Refresh' button to refresh the data.</li>"); 
-    strcat(buf, "</ul>");
-    strcat(buf, "</body></html>");    
+    strcpy(webbuff, "<!DOCTYPE html>");
+    strcat(webbuff, "<html><head><title>ESP8266 Mbed</title></head>");
+    strcat(webbuff, "<body>");    
+    strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed Web Controller</h1>");   
+    strcat(webbuff, "Hit Count - ");
+    strcat(webbuff, webcount);
+    strcat(webbuff, "<br>Last Hit - ");
+    strcat(webbuff, lasthit);                     
+    strcat(webbuff, "</div><br /><hr>");
+    strcat(webbuff, "<h3>Mbed RTC Time -&nbsp&nbsp");             
+    strcat(webbuff, timebuf);
+    strcat(webbuff, "</h3>\r\n");
+    strcat(webbuff, "<p><form method=\"POST\"><strong> Temperature:&nbsp&nbsp<input type=\"text\" size=6 value=\"");
+    strcat(webbuff, Temp);
+    strcat(webbuff, "\"> <sup>O</sup>C <form method=\"POST\"> <strong> &nbsp&nbspBattery:&nbsp&nbsp<input type=\"text\" size=4 value=\"");
+    strcat(webbuff, Vcc);
+    strcat(webbuff, "\"> </sup>V");    
+    if(led1==1){strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked>  Red LED off");
+                strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" >  Red LED on");} 
+        else{strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" >  Red LED off");
+                strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked>  Red LED on");}   
+    if(Out1==0){strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked>  Digital Out 1  off");
+                strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" >  Digital Out 1 on");}
+        else{strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" >  Digital Out 1 off");
+                strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked>  Digital Out 1 on");}   
+    if(Out2==0){strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked>  Digital Out 2 off");
+                strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" >  Digital Out 2 on");}
+        else{strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" >  Digital Out 2 off");
+                strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked>  Digital Out 2 on");}           
+    if(Out3==0){strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked>  Digital Out 3 off");
+                strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" >  Digital Out 3 on");}
+        else{strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" >  Digital Out 3 off");
+                strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked>  Digital Out 3 on");}    
+    if(In1==0){strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"0\" >  Digital In 1");}
+        else{strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked>  Digital In 1");}   
+    if(In2==0){strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"0\" >  Digital In 2");}
+        else{strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked>  Digital In 2");}           
+    if(In3==0){strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"0\" >  Digital In 3");}
+        else{strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked>  Digital In 3");}                  
+    strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;");    
+    strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);");    
+    strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);");
+    strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:");
+    strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); 
+    strcat(webbuff, "background: #3cb0fd;");
+    strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); 
+    strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);");
+    strcat(webbuff, "text-decoration:none;\"></form></span>");
+    strcat(webbuff, "<p/><h2>How to use:</h2><ul>");
+    strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>");
+    strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>");      
+    strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>"); 
+    strcat(webbuff, "</ul>");
+    strcat(webbuff, "</body></html>");    
 // end of WEB page data       
-    bufl = strlen(buf); // get total page buffer length            
-    sprintf(snd,"AT+CIPSEND=%s,%d\r\n", channel, bufl); // send IPD link channel and buffer character length.
-    SendCMD();                                   
-    wait_ms(100);                        
+    bufl = strlen(webbuff); // get total page buffer length            
+    sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
+    timeout=200;getcount=7;
+    SendCMD();
+    getreply();                                                               
     SendWEB();  // send web page
-    memset(buf, '\0', sizeof(buf));
+    memset(webbuff, '\0', sizeof(webbuff));
     sendcheck();              
 }
 
-//  wait for ESP "SEND OK" reply, then close IP to load web page, if lock out, system fully restarts
+//  wait for ESP "SEND OK" reply, then close IP to load web page
 void sendcheck()
 {
-    timeout=200;
-    weberror=1;t2.reset();t2.start();
-    while(weberror==1 && t2.read() <10){
+     weberror=1;timeout=500;getcount=24;
+    t2.reset();t2.start();
+    while(weberror==1 && t2.read() <5){        
         getreply();
-        if (strstr(buf, "SEND OK") != NULL) {weberror=0;}  // wait for valid SEND OK
+        if (strstr(replybuff, "SEND OK") != NULL) {weberror=0;}  // wait for valid SEND OK 
         }
-    t2.reset();
-    if(weberror==1){
-        pc.printf("\n\n\n ++++++++++ WEB Connect Error, restarting ...   ++++++++++\n\n");
-        NVIC_SystemReset();}     
-    strcpy(snd, "AT+CIPCLOSE=0\r\n");
-    SendCMD();    
+    if(weberror==1){ // restart connection
+        strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); 
+        timeout=500;getcount=10;
+        SendCMD();getreply(); 
+        sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
+        timeout=500;getcount=10;
+        SendCMD();getreply();
+        }
+        else{
+            sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
+            SendCMD();}       
+    t2.reset();      
 }    
 
 // Reads and processes GET and POST web data 
 void ReadWebData()
  {
-    wait_ms(200);
-    led3=0;    
-    count=0;DataRX=0;weberror=0;    
-    esp.attach(NULL);    
-    int x = strcspn (buf,"+");
+    wait_ms(200);        
+    esp.attach(NULL);
+    count=0;DataRX=0;weberror=0;
+    memset(webdata, '\0', sizeof(webdata));    
+    int x = strcspn (webbuff,"+");
     if(x){
-        strcpy(webdata, buf + x);weberror=0;                                   
+        strcpy(webdata, webbuff + x);weberror=0;                                   
         int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);    
         if( strstr(webdata, "led1=1") != NULL ) {led1=0;}
         if( strstr(webdata, "led1=0") != NULL ) {led1=1;}
@@ -203,48 +210,51 @@
         webcounter++;
         sprintf(webcount, "%d",webcounter);
         }
-            else {strcpy(webdata, buf);weberror=1;}         
-    led3=1;
+            else {
+                memset(webbuff, '\0', sizeof(webbuff));
+                esp.attach(&callback);weberror=1;
+                }         
 }
 // Starts and restarts webserver if errors detected.
 void startserver()
 {
-    esp.attach(NULL);
-    wait(1);
-    gettime();
+    gettemp();gettime();
     pc.printf("\n\n RTC time   %s\r\n\n",timebuf);
     pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");  
-    strcpy(snd,"AT+RST\r\n");
+    strcpy(cmdbuff,"AT+RST\r\n");
+    timeout=2000;getcount=600;
     SendCMD();
-    timeout=2000;getcount=336;
     getreply();
-    if (strstr(buf, "ready") != NULL) {
+    pc.printf(replybuff);
+    pc.printf("%d",count);
+    if (strstr(replybuff, "OK") != NULL) {
         pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
-        strcpy(snd, "AT+CIPMUX=1\r\n");  // set multiple connections. 
+        strcpy(cmdbuff, "AT+CIPMUX=1\r\n");  // set multiple connections. 
+        timeout=500;getcount=10;
         SendCMD();
-        timeout=500;getcount=10;
         getreply();
-        strcpy(snd, "AT+CIPSERVER=1,90\r\n");
-        //sprintf(snd,"AT+CIPSERVER=1,%d\r\n", port);
+        sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
+        timeout=500;getcount=10;
         SendCMD();
-        timeout=500;getcount=10;
+        getreply();
+        sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
+        timeout=500;getcount=50;
+        SendCMD();
         getreply();
         pc.printf("\n Getting Server IP \r\n"); 
-        strcpy(snd, "AT+CIFSR\r\n");
+        strcpy(cmdbuff, "AT+CIFSR\r\n");
         timeout=1000;getcount=50;
         while(weberror==0){
             SendCMD();getreply();
-            if (strstr(buf, "0.0.0.0") == NULL) {weberror=1;}  // wait for valid IP
+            if (strstr(replybuff, "0.0.0.0") == NULL) {weberror=1;}  // wait for valid IP
             }
         pc.printf("\n Enter WEB address in your browser \r\n\n");
-        buf[strlen(buf) - 8] = '\0';                
-        char* IP = buf + 5; 
-        sprintf(snd,"   http://%s:%d", IP, port);
-        pc.printf(snd); 
-        while (esp.readable()){esp.getc();}    
-        buflen=200;count=0;
-        memset(buf, '\0', sizeof(buf));    
+        replybuff[strlen(replybuff) - 8] = '\0';                
+        char* IP = replybuff + 5;
+        sprintf(webdata,"   http://%s:%d", IP, port);
+        pc.printf(webdata);       
         led2=0;wait(2);led2=1;
+        bufflen=200;count=0;
         pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
         esp.attach(&callback);
         }
@@ -257,25 +267,24 @@
 // ESP Command data send
 void SendCMD()
 {
-    esp.printf("%s", snd);      
+    esp.printf("%s", cmdbuff);      
 }    
 // Large WEB buffer data send
 void SendWEB()
 {    
     int i=0;
     if(esp.writeable()) {
-        while(buf[i]!='\0') {esp.putc(buf[i]);i++;}
+        while(webbuff[i]!='\0') {esp.putc(webbuff[i]);i++;}
         }     
 } 
 // Get Cammand and ESP status replies
 void getreply()
 {    
-    memset(buf, '\0', sizeof(buf));
-    t1.reset(); t1.start();
-    ended=0;count=0;
-    while(t1.read_ms()< timeout && count < getcount) {
+    memset(replybuff, '\0', sizeof(replybuff));
+    t1.reset(); t1.start();replycount=0;
+    while(t1.read_ms()< timeout && replycount < getcount) {
         if(esp.readable()) {
-            buf[count] = esp.getc();count++;
+            replybuff[replycount] = esp.getc();replycount++;
             }
         }
     t1.stop();