Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

Prevent having to wait for handle to close

MisterAMisterA Belgium
edited October 2017 in Mongoose OS

Hello,

I have two esp32's, and I try to toggle a pin from the other board using the boot button and an RPC call:

GPIO.set_mode(0, GPIO.MODE_INPUT);
GPIO.set_button_handler(0, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) {
  print('Button press, pin: ', x);
  RPC.call('ws://192.168.1.19/rpc', 'GPIO.Toggle', {pin: 2}, function (resp, ud) {
  print('Response:', JSON.stringify(resp));}, null);
}, null);

When I press the button one time I can see the built in led light up right away on the other board. And I see the following output on the board where the button was pressed:

[Oct 17 21:49:27.029] Button press, pin:  0 
[Oct 17 21:49:27.031] mg_rpc_cha68.1.19/rpc, SSL? 0
[Oct 17 21:49:27.359] mg_rpc_ws_handler    0x3ffb5528 WS HANDSHAKE DONE
[Oct 17 21:49:27.378] Response: {"value":1} 

And then after a few seconds:

[Oct 17 21:49:54.136] mg_rpc_ws_out_handle 0x3ffb53ec CLOSING (idle)
[Oct 17 21:49:54.157] mg_rpc_ws_handler    0x3ffb53ec CLOSED

The problem is, when I press the button again before the handle is closing (before the second output appears), it doesn't work. Always need to wait until the handle is closed before pressing the button works and toggles the pin on the second board.
The output is then:

[Oct 17 21:55:14.090] Button press, pin:  0 
[Oct 17 21:55:14.117] mg_rpc_channel_ws_ou 0x3ffb5784 Connecting to ws://192.168.1.19/rpc, SSL? 0
[Oct 17 21:55:14.157] mg_rpc_ws_handler    0x3ffb5784 WS HANDSHAKE DONE
[Oct 17 21:55:14.185] Response: {"value":1}   **<=The led lights up**
[Oct 17 21:55:15.122] Button press, pin:  0   **<=The Second button press does not toggle the led**
[Oct 17 21:55:15.164] mg_rpc_ws_handler    0x3ffb5784 CLOSED
[Oct 17 21:55:16.178] mg_rpc_channel_ws_ou 0x3ffb5784 Connecting to ws://192mg_rpc_ws_handler    0x3ffb5784 WS HANDSHAKE DONE
[Oct 17 21:55:31.420] mg_rpc_ws_out_handle 0x3ffb5784 CLOSING (idle)
[Oct 17 21:55:31.426] mg_rpc_ws_handler    0x3ffb5784 CLOSED

Is there a way to prevent this kind of behaviour?

Best regards,
Jan

Comments

  • SergeySergey Dublin, Ireland
    edited October 2017

    To add to the context, switching to HTTP gives this:

    RPC.call('http://192.168.1.19/rpc', 'GPIO.Toggle', {pin: 2}, function (resp, ud) {
      print('Response:', JSON.stringify(resp));}, null);
    }, null);
    
    [Oct 18 19:49:21.374] Button press, pin: 0
    [Oct 18 19:49:21.380] mg_rpc_get_channel_i Unsupported connection scheme in http://192.168.1.19/rpc
    
  • To further add to the context (not sure if the HTTP.query syntax I used is correct though)

    GPIO.set_mode(0, GPIO.MODE_INPUT);
    GPIO.set_button_handler(0, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) {
      print('Button press, pin: ', x);
      HTTP.query({
      url: 'http://192.168.1.19/rpc',
      data: {'GPIO.Toggle': 2},     
      success: function(body, full_http_msg) { print(body); },
      error: function(err) { print(err); },
    });
    }, null);
    

    results in:

    [Oct 18 20:49:06.576] Button press, pin:  0 
    [Oct 18 20:49:06.576]   at init.js:24
    [Oct 18 20:49:06.582] MJS callback error: [HTTP] is not defined
    
  • bravokeylbravokeyl Seoul, South Korea (Nomadist)

    @MisterA Did you load http first, if not please do that.

  • Hi,

    yes I (eventually) added api_http.js to be loaded. There was an error in my query as well. I was able to make it work by using:

        GPIO.set_mode(0, GPIO.MODE_INPUT);
        GPIO.set_button_handler(0, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 300, function(x) {
          print('Button press, pin: ', x);
          HTTP.query({  url: 'http://192.168.1.19/rpc/GPIO.Toggle',  data: {pin: 2},success: function(body, full_http_msg) { print(body); },  error: function(err) {      print(err);},});
        }, null);
    

    I still wonder what the problem was using websockets (a bug?), as I see the method above as a workaround.

    Best regards,
    Jan

  • SergeySergey Dublin, Ireland

    yes, there was a bug which is already fixed.
    You can revert to the original method and try again.

  • rojerrojer Dublin, Ireland
    Thanked by 1rohit_2787
  • I suppose I need to compile mongoose-os from source in order to test this?

  • rojerrojer Dublin, Ireland

    yes, or wait for next push

  • Ok, I think I will wait for the next push. Lots of other stuff to figure out in the mean time, like touchswitches and trying to get a SPI touchscreen to work. :-)

  • Could you please share your code?

Sign In or Register to comment.