real time wifi

Dependencies:   RemoteIR TextLCD

Revision:
4:a365d6acec8f
Parent:
3:4c5c556c97a3
Child:
5:4026062a6fdf
diff -r 4c5c556c97a3 -r a365d6acec8f main.cpp
--- a/main.cpp	Wed Sep 02 00:46:11 2020 +0000
+++ b/main.cpp	Thu Sep 03 00:30:32 2020 +0000
@@ -13,6 +13,8 @@
 DigitalOut  led2(LED2);    
 DigitalOut  led3(LED3);
 DigitalOut  led4(LED4);     
+DigitalOut  pred(p16);
+DigitalOut  pblue(p19);
 
 Timer t1;
 Timer t2;
@@ -31,7 +33,7 @@
 char channel[2];
 char cmdbuff[32];
 char replybuff[1024];
-
+bool click_log = false; 
 char webdata[1024]; // This may need to be bigger depending on WEB browser used
 char webbuff[4096*4];     // Currently using 1986 characters, Increase this if more web page data added
 
@@ -49,11 +51,14 @@
 void callback()
 {
     //pc.printf("\n\r------------ callback is being called --------------\n\r");
-    led3=1;
+    led3=1;    
+        
     while (esp.readable()) {
+        
         webbuff[count_wifi] = esp.getc();
         count_wifi++;
     }
+        
     if(strlen(webbuff)>bufflen) {
         pc.printf("\f\n\r------------ webbuff over bufflen --------------\n\r");
         DataRX=1;
@@ -76,19 +81,22 @@
     while(1) {
         if(DataRX==1) {
             pc.printf("\f\n\r------------ main while > if --------------\n\r");
-            click_flag = 1;
+           // click_flag = 1;
+            click_log=false;
             ReadWebData();
-            pc.printf("\f\n\r------------ click_flag=%d --------------\n\r",click_flag); 
+           // pc.printf("\f\n\r------------ click_flag=%d --------------\n\r",click_flag); 
             //if ((servreq == 1 && weberror == 0) && click_flag == 1) {
-            if (servreq == 1 && weberror == 0) {
+            if (servreq == 1 && weberror == 0 && click_log == false) {
                 pc.printf("\f\n\r------------ befor send page --------------\n\r");        
                 sendpage();
             }
-            pc.printf("\f\n\r------------ send_check begin --------------\n\r"); 
+           // pc.printf("\f\n\r------------ send_check begin --------------\n\r"); 
             
             //sendcheck();
-            pc.printf("\f\n\r------------ ssend_check end--------------\n\r"); 
-            
+            pc.printf("\f\n\r------------ befor attach callback--------------\n\r"); 
+            // this is neccessary 0902
+                memset(webbuff, '\0', sizeof(webbuff));
+
             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);
@@ -162,6 +170,10 @@
 strcat(webbuff, "</html>");
 strcat(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
 strcat(webbuff, "function send_mes(btnmes,btnval){");
+  
+  strcat(webbuff, "try {xhr.abort();console.log(1);}");
+  strcat(webbuff, "catch(err) {console.log(0);}");
+
 strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
 strcat(webbuff, "htmlacs(url);");
 strcat(webbuff, "console.log(url);");
@@ -173,7 +185,8 @@
 strcat(webbuff, "xhr.send();");
 strcat(webbuff, "}");
 
-
+//0902 beging
+/*
 //0824 battery update auto
 strcat(webbuff, "function battery_update() {");
 strcat(webbuff, "var url1 =  \"http://\" + window.location.hostname+ \"/cargo?a=responseBattery\";");
@@ -192,7 +205,8 @@
 strcat(webbuff, "xhr1.send();");
 strcat(webbuff, "}");
 strcat(webbuff, "setInterval(battery_update,10000);");
-
+*/
+//0902 end
 //0824 battery update auto
 /*
 strcat(webbuff, "function send_mes_spe(btnmes,btnval){");
@@ -237,7 +251,8 @@
 void sendpage2()
 {
 // WEB page data
-
+pred=!pred;
+/*
 //strcpy(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
 //strcat(webbuff, "document.getElementById('leftms').value=\"100\";");
 count_test++;
@@ -267,14 +282,19 @@
   
     memset(webbuff, '\0', sizeof(webbuff));
     sendcheck();
+    
+    */
 }
 
 
 // Reads and processes GET and POST web data
 void ReadWebData()
 {
-    pc.printf("+++++++++++++++++Read Web Data+++++++++++++++++++++\r\n");
-    wait_ms(200);
+    pc.printf("+++++++++++++++++Read Web Data begin+++++++++++++++++++++\r\n");
+ 
+//0902 is this wait neccessary 
+//    wait_ms(200);
+   
     esp.attach(NULL);
     count_wifi=0;
     DataRX=0;
@@ -286,9 +306,15 @@
         weberror=0;
         int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
         //int i=0;
-        pc.printf("+++++++++++++++++succed rec begin+++++++++++++++++++++\r\n");
+        pc.printf("\r\n+++++++++++++++++succed rec begin+++++++++++++++++++++\r\n");
         pc.printf("%s",webdata);
-        pc.printf("+++++++++++++++++succed rec end+++++++++++++++++++++\r\n");
+        pc.printf("\r\n+++++++++++++++++succed rec end+++++++++++++++++++++\r\n");
+       // click or login
+       if( strstr(webdata, "cargo") != NULL ) {
+            click_log=true;
+            pc.printf("\r\n++this is click++\r\n");
+                        
+        }
        if( strstr(webdata, "responseBattery") != NULL ) {
              click_flag = 0;
              led4=!led4;
@@ -321,19 +347,19 @@
                         
         }
         if( strstr(webdata, "LEFT") != NULL ) {
+            sendpage2();
             led4=!led4;
-            pc.printf("mode = RIGHT\r\n");
+           // pc.printf("mode = RIGHT\r\n");
                         
         }
-       /*  
+        
         if( strstr(webdata, "RIGHT") != NULL ) {
+       sendpage2();
             led4=!led4;
-            pc.printf("mode = RIGHT\r\n");
-                        mode = RIGHT;       // 右折モード
-                        run = RIGHT;        // 右折
-                        display();          // ディスプレイ表示
+           
         }
         
+        /*
         if( strstr(webdata, "GO") != NULL ) {
             led4=!led4;
             pc.printf("mode = ADVANCE\r\n");
@@ -405,6 +431,8 @@
         esp.attach(&callback);
         weberror=1;
     }
+    
+    pc.printf("+++++++++++++++++Read Web Data end+++++++++++++++++++++\r\n");
 }
 // Large WEB buffer data send
 void SendWEB()