Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Operating through index.html

I recently started using mongoose with my ESP8266. I want to modify the index.html file so that anyone in the network can connect to the device and control the led.Below is my code which is not working any suggestion to correct will be helpful.

<!DOCTYPE html>



function myjs(){ alert("hi"); var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var arr1=this.responseText; alert(arr1); } }; xmlhttp.open("POST", "/rpc/GPIO.Toggle", true); xmlhttp.send(); }

Comments

  • I use something like this:
    in jQyery
    $.post('/rpc/App.SetState', JSON.stringify({ "pin": 1 , "state": 0 }));

    in js on esp

    load('api_gpio.js');
    load('api_rpc.js');
    let led_pin = 8;
    GPIO.set_mode(led_pin, GPIO.MODE_OUTPUT);
    GPIO.write(led_pin, 0);
    RPC.addHandler('App.SetState', function(args){
    GPIO.write(led_pin, args.state);
    or
    GPIO.write(args.pin, args.state);
    return true;});
  • Hi z_X_z ,
    I modified my query as below and its still not working, can we please help. currently its not giving any error but the state of LED is not changing.

    <!DOCTYPE html>



    $(document).ready(function(){ $("button").click(function(){ $.post('/rpc/App.SetState', JSON.stringify({"state":1 })); }); });

  • Try this:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      </head>
    <body>
    <button id="button">Continue</button>               
    <script>
    $(document).ready(function(){ 
        $("#button").bind( "click", function(event, ui) {
            $.post('/rpc/App.SetState', JSON.stringify({"state": 1 })); 
        }); 
    });
    </script>
    </body>
    </html>

    load('api_gpio.js');
    load('api_rpc.js');
    let led_pin = 2;
    GPIO.set_mode(led_pin, GPIO.MODE_OUTPUT);
    GPIO.write(led_pin, 0);
    RPC.addHandler('App.SetState', function(args){
    GPIO.write(led_pin, args.state);
    return true;});
  • Hi z_X_z .
    Thanks a lot. the code is working as I was expecting.

  • SergeySergey Dublin, Ireland

    Just to let you know, there is a stock GPIO RPC library that has all these GPIO management services,

    listed at
    https://mongoose-os.com/libs.html

    The library is https://github.com/mongoose-os-libs/rpc-service-gpio

    That library is part of the default firmware, here is the example of my session with one of the devices over device management dashboard:

Sign In or Register to comment.