AWS and Local Connection

Good day! I'm new to esp8266 and mongoose. I'm starting a project to control and esp8266 over AWS IoT and local network. The idea is to be able to send messages to esp through web server, and over wifi when internet is not available, or when user is connected to the same network that esp is.
I got AWS IoT working just fine (i can public messages to AWS mqtt client correctly) and I'm not really sure which way to go to send messages to esp over local network. I guess i should use something like this example:
https://github.com/cesanta/mongoose-os/tree/master/fw/examples/c_network

or maybe websocket?

I need esp8266 to be able to listen and respond to multiple devices in the same network, and through AWS.

Thanks in advance, and sorry for my little knowledge, I'm just starting!

BR

Comments

  • Fede_1988Fede_1988 Argentina

    Wow! That was fast. Thanks a lot, I'll try and let you know how it goes.

  • Fede_1988Fede_1988 Argentina

    Hello again!

    I managed to implement a very simple rpc function:

    // Load Mongoose OS API
    load('api_rpc.js');
    load('api_gpio.js');
    let led = 2;
    GPIO.set_mode(led, GPIO.MODE_OUTPUT);

    RPC.addHandler('Control', function(args){
    GPIO.write(led, args.state);
    return true;
    });

    It works fine when i send a command through Cygwin. Now i'm trying to do the same in an AndroidApp (I want to control the led from my phoen). I get a good connection:

    mongoose_ev_handler 0x3fff3574 HTTP connection from 192.168.1.6:63280

    But nothing happens when i send a command (through my app):

        String s="{\"method\": \"Control\", \"args\": {\"state\": 1}}";
        mWebSocketClient.send(s);
    

    maybe my JSON format is not good? In that case, shouldn't i see some error in ESP8266 console?

    Thanks a lot for any advice!

  • SergeySergey Dublin, Ireland

    Which URL do you use for your websocket client?

  • Fede_1988Fede_1988 Argentina

    I was uisng ws://192.168.1.7/
    but it should have been ws://192.168.1.7/rpc !!
    Now i'm getting

    INVALID FRAME (43): '{"method": "Control", "args: {"state": 1}}'

  • SergeySergey Dublin, Ireland

    Add "src": "dummy"

  • Fede_1988Fede_1988 Argentina

    Perfect! Works great. Thanks Sergey.

    If anyone needs it, this is the complete JSON formatted string i sent :

    "{\"jsonrpc\": \"2.0\", \"method\": \"Control\", \"args\": {\"state\": 1},\"src\": \"dummy\"}"

  • SergeySergey Dublin, Ireland
    edited March 5

    jsonrpc key is not required.
    We will also make src key not required, too. It is required at this moment.

  • Fede_1988Fede_1988 Argentina
    edited March 9

    Hello! Sorry to re-open (not sure i should make a new post).
    I'm trying to add a rpc handler, and i can't manage to pass arguments well. I mean,

    I'm adding a handler like this:

    mg_rpc_add_handler(c, "Control", "{State: %d}", Control, NULL);

    and this is the handler:

    static void Control(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) {

    mgos_gpio_write(LED_GPIO,"{State: %d}");
    (void)ri;
    (void)cb_arg;
    (void)fi;
    (void)args;}
    

    What i ment to do is that when i call (from command-line):

    $ mos --port ws://IP_ADRESS/rpc call Control '{"State": 1}'

    this should call Control handler and write a "1" to LED_GPIO pin. I know i'm not adding the hanlder ok, and i don't know how to acces arguments inside my Control handler.

    Thanks in advance for any help!

  • SergeySergey Dublin, Ireland

    You need to parse an incoming JSON in order to fetch the parameter's value.

    There is a video: https://mongoose-os.com/video-tutorials.html#video7
    And there is an example project: https://github.com/cesanta/mongoose-os/blob/master/fw/examples/c_rpc/src/main.c#L10-L31

    Does that make it clear?

  • Fede_1988Fede_1988 Argentina

    Yes, perfectly. Thank you!!

  • Fede_1988Fede_1988 Argentina

    Sorry to bother again,

    i wrote the C code based on video tutorial 7 and works ok when i send a JSON string in command line, but i can't manage to do the same from a java app .

    This is my handler:

    static void Control(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) {
        //ri = Request Info
        //cb_arg = CallBack Argumet
        //fi = frame info
        //args = JSON string
    int state=0;
    int scanf_result=0;
    scanf_result=json_scanf(args.p, args.len, ri->args_fmt, &state);
    if (scanf_result == SUCCESS) {  
        mgos_gpio_write(LED_GPIO,state);
        mg_rpc_send_responsef(ri, "true"); //Command applied
    }
    
    printf("Control received\n");
    printf("State = %d\n",state);
    printf("scanf_result = %d\n",scanf_result);
    
    (void)cb_arg;
    (void)fi;}
    

    This is how i added the handler

    struct mg_rpc *c = mgos_rpc_get_global();
    g_rpc_add_handler(c, "Control", "{State: %d}", Control, NULL);

    When i send from command line:

    $ mos --port ws://192.168.0.14/rpc call Control '{"State": 0}'
    "true"

    Serial print:
    mongoose_ev_handler 0x3fff1c0c HTTP connection from 192.168.0.12:57420
    Control received
    State = 0
    scanf_result = 1
    mg_rpc_ws_handler 0x3fff17ec CLOSED

    Now i'm tring to send the same command in an android app using java. This is what i'm sending:

    s="{\"method\": \"Control\", \"args\": {\"State\": 1},\"src\": \"dummy\"}";

    Serial print
    mongoose_ev_handler 0x3fff1c24 HTTP connection from 192.168.0.10:56561
    Control received
    State = 0
    scanf_result = 0

    It seems i'm not formatting well the sending JSON string in java, but i'm not sure why. Any ideas?

    Thank you!

  • Fede_1988Fede_1988 Argentina

    Never mind, it was some compilation problem in my java editor. The JSON string is ok.

    Thanks!

  • SergeySergey Dublin, Ireland

    Thanks for letting us know!

Sign In or Register to comment.