Announcement

Collapse
No announcement yet.

Connection problems

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Connection problems

    Hi,

    I have connected my PC to the xPico Wifi 192.168.0.1 accessing the WEB manager without problems.
    But when I created my first JS/html page to control GPIO CP1, the page do nothing waiting in the xmlhttp.open instruction:

    xmlhttp.open("POST","http://192.168.0.1/action/status",true)

    It looks like the page waits for the connection, unsuccessfully.
    Do I need to configure something in the xPico WebServer to allow access?
    Is the URL correctly specified?
    Something is missing?

    In the past I used the xPort, accessing it with TCP sockets and sending commands directly through port 0x7FF0 with perl or phyton. But it looks that it is not possible with the xPico Wifi.

    The only thing that I need for the moment is to control the CP1 pin.

    Many thanks for your help.

  • #2
    You need to connect and then send more information to the action status API.
    Set the CP3 as output:
    "group=CPM&optionalItem=CP&optionalItemInstance=3& action=Usage Output"
    Set the output value to 1
    "group=CPM&optionalItem=CP&optionalItemInstance=3& action=Value+1"

    Comment


    • #3
      The URL seems correct. You should also setRequestHeader to urlencoded, andn then do xmlhttp.send(content).

      See an example of how to do the Javascript in the JS library here:
      https://github.com/Lantronix/xpwJSli...ster/xpwLib.js

      Or you can just use the library itself. There's an example on how to toggle GPIOs.

      https://github.com/Lantronix/xpwJSlib

      Mariano

      Comment


      • #4
        Many thanks for your replies, but I did it.
        In fact my JS test is very simple and it set pin 1 to high:

        var gpioMsg = "group=CPM CPs&optionalItem=CP&optionalItemInstance=1&action= Value 1";

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
        replyPHP = xmlhttp.responseText;
        document.write(replyPHP);
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        replyPHP = xmlhttp.responseText;
        document.write(replyPHP);
        }
        }
        document.write("...before open...");
        xmlhttp.open("POST", "http://192.168.0.1/action/status", true);
        document.write("...before header...");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        document.write("...before send...");
        xmlhttp.send(gpioMsg);

        I inserted some writes to check were it hansgs, and the last message on screen is "...before open".
        That indicates me that is not a problem of commands or protocol and the page stays waiting for the
        open connection forever (it does not reach the setRequestHeader nor send).
        It looks like the xPico is not accepting the communication (configuration problem?).
        I should add that I am not using a router and the PC running Apache is contacting the XPico directly.

        Many thanks again, but this is frustating ... it was a lot easier with the xPort
        and maybe I am doing something wrong conceptually.

        Comment


        • #5
          The .open method initializes the request, it doesn't send anything to the server (xPico Wi-Fi) until the .send method is called.

          I'm not sure why the browser is not getting past that line. Can you open the browser's developer console and see if it has an error?

          One thing to try is changing the URL to "/action/status" and removing the IP address? That way the Javascript application will work regardless of the IP address of the xPico Wi-Fi (if for some example you want to access via wlan0 instead of ap0).

          Comment


          • #6
            Thank you Mariano.

            It looks something with JavaScript.

            Also, I tried without the IP address with the same result.
            Moreover, I add user and password parameter in the OPEN call too. Nothing.

            I tried with PHP by using CURL ant it works fine, but with JS is different.

            Firefox shows:
            InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

            Chrome shows:
            POST http://192.168.0.1/action/status 401 (Authorization Required)
            XMLHttpRequest cannot load http://192.168.0.1/action/status. No 'Access-Control-Allow-Origin'
            header is present on the requested resource. Origin 'null' is therefore not allowed access.
            The response had HTTP status code 401.

            Comment


            • #7
              Are you trying to open the HTML file hosted on your PC? If so, you are going to run into cross origin request issues.

              Can you host the file on the xPico Wi-Fi's web server instead?

              I just tested the following code in a test.html file which I placed in the filesystem of the xPico Wi-Fi in the html/ directory, and when I go to 192.168.0.1/test.html, it sets CP2 to Value 1 (after CP2 has been set as an output).

              Code:
              <html>
              <body>
              
              <script>
              var gpioMsg = "group=CPM CPs&optionalItem=CP&optionalItemInstance=2&action=Value 1";
              
              var xmlhttp = new XMLHttpRequest();
              xmlhttp.onreadystatechange = function() {
              replyPHP = xmlhttp.responseText;
              document.write(replyPHP);
              if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
              replyPHP = xmlhttp.responseText;
              document.write(replyPHP);
              } 
              }
              xmlhttp.open("POST", "http://192.168.0.1/action/status", true); 
              //xmlhttp.setRequestHeader("Authorization", "Basic " + btoa("admin:PASSWORD"));
              xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
              xmlhttp.send(gpioMsg);
              </script>
              
              </body>
              </html>

              Comment

              Working...
              X