1 Reply Latest reply on Jul 27, 2015 1:35 PM by JPMontero_Intel

    Arduino call c++ function from HTML Javascript

    CyberSurfer

      Hi,

       

           I am working on a simple demo project which I'd like to show off in the next few days (sorry for the short notice, but I'm new to this IoT stuff so I've spent alot of time learning the ropes and getting the functions I'll use working). Anyways, we have an HTML page with buttons (generated via PrintHTML() ) that calls a Javascript function when pressed. What I would like to do is find some way to have that Javascript function call a function elsewhere in my C++ class. I would appreciate any lightweight answer you could provide. Please provide as much detail or links as possible so I can quickly try and implement any suggestions.

       

      Thanks

      Cyber

       

      Arduino Project

      Setup
      {}
      
      Loop
      {
          PrintHTML();
      }
      
       void PrintHTML(WiFiClient clientVar)
      {
                WiFiClient client = clientVar;
                // send a standard http response header
                client.println("HTTP/1.1 200 OK");
                client.println("Content-Type: text/html");
                client.println("Connection: close");  // the connection will be closed after completion of the response
                client.println("Refresh: 5");  // refresh the page automatically every 5 sec
                client.println();
                client.println("<!DOCTYPE HTML>");
                client.println("<html>");
      
                 //Write javascript functions
                 client.println("<script>");  
                 client.println("var myStatusVar = \"Off\";");
                 client.println("function myJsFunction() {");
                 client.println("if(myStatusVar == \"Off\")");
                 client.println("myStatusVar = \"On\";");
                 client.println("else");
                 client.println("myStatusVar = \"Off\"");
                 client.println("document.getElementById(\"myStatus\").innerHTML = myStatusVar");
      //CALL MYTESTFUNCTION();
                 client.println("}");
                 client.println("</script>");           
                
                //Let add a button to kick off something
                 client.println("<br>");  
                 client.println("<button type=\"button\" onclick=\"myJsFunction()\">Do Something</button><br><br>");
                 client.println("<br>");                 
                }
                
                client.println("</html>");
      }
      
      Void MyTestFunction()
      { //MY C++ CODE
      }