wifi only 基準コード

Dependencies:   RemoteIR TextLCD

Revision:
2:1bf376317a43
Parent:
1:a45cf46c1adc
Child:
3:4c5c556c97a3
--- a/main.cpp	Thu Aug 06 01:50:55 2020 +0000
+++ b/main.cpp	Tue Aug 25 00:10:20 2020 +0000
@@ -36,11 +36,12 @@
 char webbuff[4096*4];     // Currently using 1986 characters, Increase this if more web page data added
 
 
-void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(),touchuan();
+void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),sendpage2(),SendWEB(),sendcheck(),touchuan();
 void wifi(void const *argument);
 
 Thread wifi_thread(wifi, NULL, osPriorityNormal); // decodeIRをスレッド化 :+3
-
+int count_test=1;        //test
+char battery_ch[8];
 int port        =80;  // set server port
 int SERVtimeout =5;    // set server timeout in seconds in case link breaks.
 
@@ -165,11 +166,35 @@
 strcat(webbuff, "htmlacs(url);");
 strcat(webbuff, "console.log(url);");
 strcat(webbuff, "}");
+
 strcat(webbuff, "function htmlacs(url) {");
 strcat(webbuff, "var xhr = new XMLHttpRequest();");
-strcat(webbuff, "xhr.open(\"GET\", url);");
-strcat(webbuff, "xhr.send(\"\");");
+strcat(webbuff, "xhr.open(\"GET\", url ,true);");
+strcat(webbuff, "xhr.send();");
 strcat(webbuff, "}");
+
+
+//0824 battery update auto
+strcat(webbuff, "function battery_update() {");
+strcat(webbuff, "var url1 =  \"http://\" + window.location.hostname+ \"/cargo?a=responseBattery\";");
+strcat(webbuff, "var xhr1 = new XMLHttpRequest();");
+strcat(webbuff, "xhr1.open(\"GET\", url1);");
+//0820
+strcat(webbuff, "xhr1.onreadystatechange = function(){");
+            //strcat(webbuff, "console.log(\"onready function is being reload!\");");
+            strcat(webbuff, "if(this.readyState == 4 && this.status == 200){");
+            //strcat(webbuff, "console.log(\"state is being reload!\");");
+                strcat(webbuff, "var res1 = xhr1.responseText;");
+                strcat(webbuff, "document.getElementById('leftms').value=res1;}};");
+                //strcat(webbuff, "console.log(res);}};");
+//0820
+
+strcat(webbuff, "xhr1.send();");
+strcat(webbuff, "}");
+strcat(webbuff, "setInterval(battery_update,10000);");
+
+//0824 battery update auto
+/*
 strcat(webbuff, "function send_mes_spe(btnmes,btnval){");
 strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
 strcat(webbuff, "htmlacs(url);");
@@ -184,6 +209,7 @@
 strcat(webbuff, "document.getElementById(\"funprint\").innerHTML=btnval;");
 strcat(webbuff, "}");
 strcat(webbuff, "}");
+*/
 strcat(webbuff, "</script>");
 // end of WEB page data
     bufl = strlen(webbuff); // get total page buffer length
@@ -207,75 +233,43 @@
     sendcheck();
 }
 
-// Large WEB buffer data send
-void SendWEB()
+
+void sendpage2()
 {
-    int i=0;
-    if(esp.writeable()) {
-        while(webbuff[i]!='\0') {
-            esp.putc(webbuff[i]);
-                 
-          //****
-          //output at command when 2000 
-            if(((i%2047)==0) && (i>0)){    
-                    //wait_ms(10);
-                    sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048)); // send IPD link channel and buffer character length.
-                    pc.printf("\r\n++++++++++ AT+CIPSENDBUF=%d,%d ++++++++++\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048));
-                    timeout=600;
-                    getcount=50;
-                    SendCMD();
-                    getreply();
-                    pc.printf(replybuff);
-                    pc.printf("\r\n+++++++++++++++++++\r\n");
-               }    
-    //****  
-            i++;     
-            pc.printf("%c",webbuff[i]);
-        }
-    }
-     pc.printf("\n++++++++++ send web i= %dinfo ++++++++++\r\n",i);
+// WEB page data
+
+//strcpy(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
+//strcat(webbuff, "document.getElementById('leftms').value=\"100\";");
+count_test++;
+sprintf(battery_ch,"%d",count_test);
+strcpy(webbuff, battery_ch);
+
+//strcpy(webbuff, "document.getElementById('leftms').value=\"100\";");
+
+//strcat(webbuff, "</script>");
+// end of WEB page data
+    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.
+    
+    sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl>2048?2048:bufl)); // send IPD link channel and buffer character length.
+    timeout=500;
+    getcount=40;
+    SendCMD();
+    getreply();
+    pc.printf(replybuff);
+    pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl));
+    
+    pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl);
+    
+    //pastthrough mode
+    SendWEB();  // send web page
+    pc.printf("\n++++++++++ webbuff clear  ++++++++++\r\n");
+  
+    memset(webbuff, '\0', sizeof(webbuff));
+    sendcheck();
 }
 
 
-    
-void sendcheck()
-{
-    weberror=1;
-    timeout=500;
-    getcount=24;
-    t2.reset();
-    t2.start();
-    
-    /*
-    while(weberror==1 && t2.read() <5) {
-        getreply();
-        if (strstr(replybuff, "SEND OK") != NULL) {
-            weberror=0;   // wait for valid SEND OK
-        }
-    }
-    */
-    if(weberror==1) { // restart connection
-        strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
-        timeout=500;
-        getcount=10;
-        SendCMD();
-        getreply();
-        pc.printf(replybuff);
-        sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
-        timeout=500;
-        getcount=10;
-        SendCMD();
-        getreply();
-        pc.printf(replybuff);
-    } else {
-        sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
-        SendCMD();
-        getreply();
-        pc.printf(replybuff);
-    }
-    t2.reset();
-}
-
 // Reads and processes GET and POST web data
 void ReadWebData()
 {
@@ -295,15 +289,38 @@
         pc.printf("+++++++++++++++++succed rec begin+++++++++++++++++++++\r\n");
         pc.printf("%s",webdata);
         pc.printf("+++++++++++++++++succed rec end+++++++++++++++++++++\r\n");
-       /* if( strstr(webdata, "LEFT") != NULL ) {
+       if( strstr(webdata, "responseBattery") != NULL ) {
              click_flag = 0;
              led4=!led4;
-             pc.printf("mode = LEFT\r\n");
-                        mode = LEFT;        // 左折モード
+             pc.printf("\r\n++++++++++++++mode = LEFT++++++++++++++\r\n");
+             sendpage2();
+/*
+            strcpy(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
+            strcat(webbuff, "document.getElementById('leftms').value=\"96\";");
+             strcat(webbuff, "let request = new XMLHttpRequest();");
+ strcat(webbuff, "request.open('GET', url);request.responseType = 'text';");
+ strcat(webbuff, "request.onload = function() {  poemDisplay.textContent = request.response;};");
+
+ strcat(webbuff, "request.send();");
+            strcat(webbuff, "</script>"); 
+            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=500;
+            getcount=40;
+            SendCMD();
+            getreply();
+            SendWEB(); 
+            memset(webbuff, '\0', sizeof(webbuff));
+    sendcheck();
+            pc.printf("\r\n++++++++++++++mode = LEFT++++++++++++++\r\n");
+*/
+     
+          /*              mode = LEFT;        // 左折モード
                         run = LEFT;         // 左折
-                        display();
+                        display(); */
+                        
         }
-        
+       /*  
         if( strstr(webdata, "RIGHT") != NULL ) {
             led4=!led4;
             pc.printf("mode = RIGHT\r\n");
@@ -384,6 +401,75 @@
         weberror=1;
     }
 }
+// Large WEB buffer data send
+void SendWEB()
+{
+    int i=0;
+    if(esp.writeable()) {
+        while(webbuff[i]!='\0') {
+            esp.putc(webbuff[i]);
+                 
+          //****
+          //output at command when 2000 
+            if(((i%2047)==0) && (i>0)){    
+                    //wait_ms(10);
+                    sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048)); // send IPD link channel and buffer character length.
+                    pc.printf("\r\n++++++++++ AT+CIPSENDBUF=%d,%d ++++++++++\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048));
+                    timeout=600;
+                    getcount=50;
+                    SendCMD();
+                    getreply();
+                    pc.printf(replybuff);
+                    pc.printf("\r\n+++++++++++++++++++\r\n");
+               }    
+    //****  
+            i++;     
+            pc.printf("%c",webbuff[i]);
+        }
+    }
+     pc.printf("\n++++++++++ send web i= %dinfo ++++++++++\r\n",i);
+}
+
+
+    
+void sendcheck()
+{
+    weberror=1;
+    timeout=500;
+    getcount=24;
+    t2.reset();
+    t2.start();
+    
+    /*
+    while(weberror==1 && t2.read() <5) {
+        getreply();
+        if (strstr(replybuff, "SEND OK") != NULL) {
+            weberror=0;   // wait for valid SEND OK
+        }
+    }
+    */
+    if(weberror==1) { // restart connection
+        strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
+        timeout=500;
+        getcount=10;
+        SendCMD();
+        getreply();
+        pc.printf(replybuff);
+        sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
+        timeout=500;
+        getcount=10;
+        SendCMD();
+        getreply();
+        pc.printf(replybuff);
+    } else {
+        sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
+        SendCMD();
+        getreply();
+        pc.printf(replybuff);
+    }
+    t2.reset();
+}
+
 // Starts and restarts webserver if errors detected.
 void startserver()
 {