Copyright ©

Mongoose OS Forum


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:

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!



  • 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
    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

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

        String s="{\"method\": \"Control\", \"args\": {\"state\": 1}}";

    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://
    but it should have been ws:// !!
    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 2017

    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 2017

    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}");

    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:
    And there is an example project:

    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) {  
        mg_rpc_send_responsef(ri, "true"); //Command applied
    printf("Control received\n");
    printf("State = %d\n",state);
    printf("scanf_result = %d\n",scanf_result);

    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:// call Control '{"State": 0}'

    Serial print:
    mongoose_ev_handler 0x3fff1c0c HTTP connection from
    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
    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.


  • SergeySergey Dublin, Ireland

    Thanks for letting us know!

Sign In or Register to comment.