Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

RPC.addHandler and RPC.Call

Hello. I have read about how to add rpc handler in JS. I wonder how to use RPC.call in js. For example i want to call my rpc handler:

function switchCallback(){
  let isSwitchOn = GPIO.read(pinButton);
  GPIO.write(pinLed, isSwitchOn);
  print("read(", pinButton, ") --> The statement 'The switch is on' currently is " , isSwitchOn === 1 ? "TRUE" : "FALSE");   
  return GPIO.read(pinButton) === 1 ? true : false;
}

RPC.addHandler('call.rpc_remote', switchCallback);

But i want to call it when i press the button:

GPIO.set_int_handler(pinButton, GPIO.INT_EDGE_ANY, RPC.call(...), null);

So what i need to do to call 'call.rpc_remote' by the button press and how to call it on second esp module?
thx.

Comments

  • dwarq7dwarq7 Kiev
    edited June 11

    yep, i have done it but:

    load('api_rpc.js');
    load('api_config.js');
    load('api_events.js');
    load('api_gpio.js');
    
    let pinButton = Cfg.get('pins.btn_switch');
    GPIO.set_mode(pinButton, GPIO.MODE_INPUT);
    GPIO.set_pull(pinButton, GPIO.PULL_DOWN); 
    
    let pinLed = Cfg.get('pins.led');
    GPIO.set_mode(pinLed, GPIO.MODE_OUTPUT); 
    
    function switchCallback(){
      let isSwitchOn = GPIO.read(pinButton);
      GPIO.write(pinLed, isSwitchOn);
      print("read(", pinButton, ") --> The statement 'The switch is on' currently is " , isSwitchOn === 1 ? "TRUE" : "FALSE");   
      return GPIO.read(pinButton) === 1 ? true : false;
    }
    
    RPC.addHandler('call.rpc_remote', switchCallback);
    
    switchCallback();
    
    let StateResponse = {
      callback: switchCallback,
      rpc_call: RPC.call('ws://192.168.0.109', 'call.rpc_remote', {}, function (resp, ud) { //.109 into YUROBOT
          print('Response:', JSON.stringify(resp));
        }, null) 
    };
    
    GPIO.set_int_handler(pinButton, GPIO.INT_EDGE_ANY, StateResponse.rpc_call, null);
    GPIO.enable_int(pinButton);
    

    This code doesnt call's second esp(they are connected to my wifi network)

  • nliviunliviu Romania

    ws://192.168.0.109 -> ws://192.168.0.109/rpc where 192.168.0.109 is the remote device.

Sign In or Register to comment.