Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

RPC over MQTT message format?

holla2040holla2040 Montrose, CO

Folks,
Is RPC over MQTT possible? What the format of the message?

I've tried the following from linux command line
mosquitto_pub -h broker.hivemq.com -p 1883 -t holla2040/rpc -m '{"method":"Sys.GetInfo"}'
mosquitto_pub -h broker.hivemq.com -p 1883 -t holla2040/rpc -m '{"call":"Sys.GetInfo"}'
mosquitto_pub -h broker.hivemq.com -p 1883 -t holla2040/rpc -m '{"name":"Sys.GetInfo"}'

results in console errors
mg_rpc_ev_handler 0x3ffefd0c INVALID FRAME (24): '{"method":"Sys.GetInfo"}'
mg_rpc_ev_handler 0x3ffefd0c INVALID FRAME (22): '{"call":"Sys.GetInfo"}'
mg_rpc_ev_handler 0x3ffefd0c INVALID FRAME (22): '{"name":"Sys.GetInfo"}'

What's the message format? if this did work, which topic gets the response?

Thanks,
Craig

Comments

  • SergeySergey Dublin, Ireland
    edited June 2017
  • holla2040holla2040 Montrose, CO

    Sergey,

    Thanks, I read that but couldn't figure out how to send commands over mqtt

    You might want to add this example
    mos --port mqtt://test.mosquitto.org:1883/esp8266_E27B63 ls

    to https://mongoose-os.com/docs/#/rpc/frame.md/ just above "RPC frame format"

    in fact every place in docs where you run mos examples to talk over serial and ws, mqtt could be added.

    I figured out mqtt commands from reading this
    https://mongoose-os.com/blog/secure-remote-device-management-with-mongoose-os-and-aws-iot-for-esp32-esp8266-ti-cc3200-stm32/

    Craig

  • alanm101alanm101 Cape Town

    Hi Craig. I stumbled across your post. I checked the link you referenced but I'm still not coming right. Please share a command line that works for you. Here's one variation of many that I've tried:

    mosquitto_pub -h iot.eclipse.org -p 1883 -t esp32_06FBBF/rpc -m "call Led '{\"state\": 0}'"

    I hace registered "Led" and no matter what I try, the same message: mg_rpc_ev_handler 0x3ffd1ebc INVALID FRAME

    Thanks,
    Alan.

  • That format is not correct. That would be the syntax if you are passing the args to the "mos" executable for the "call" command, not over an rpc directly which is basically in the form of json string -- https://mongoose-os.com/docs/libraries/core/rpc.html#mg-rpc-frame-format

    mos --port mqtt://<ip>/<device_id> call my-rpc "{extra:state}"
    
    mos --port mqtt://<ip>/<device_id> ls
    mos --port mqtt://<ip>/<device_id> call FS.List '{"extra":"state"}'
    
    mos --port mqtt://192.168.1.5:1883/esp8266_A6F79A ls
    [
      "sys_ro_vars_schema.json",
      "sys_config_schema.json",
     ...
    ]
    

    If you want to see examples just fire up mosquito_sub, or any client, to watch for all messages over the rpc topic (and response topics), or just get everything using "#":

    esp8266_A6F79A/rpc {"src":"mos-1497649175","id":2088006592695,"method":"FS.Get","args":{...}}
    mos-1497649175/rpc {"id":2088006592695,"src":"esp8266_A6F79A","dst":"mos-1497649175","result":{"data": ...
    

    This assumes you have connected your webui (mos ui) over mqtt, so all rpc traffic will go over mqtt. To do this, run:

    mos --port mqtt://<ip>/<device_id> ui
    

    Once the webui comes up you can look at the services on the left navigation to make sure your service is listed, and works from the web-ui. If you are watching all mqtt traffic then you will see a request and response for your call.

    And to finish things off, in case you are still unsure how to pub from an arbitrary mqtt client (like mosquitto_pub), here is one last example:

    mosquitto_sub -h 192.168.1.5 -p 1883 -t out-topic/# &
    
    mosquitto_pub -h 192.168.1.5 -p 1883 -t esp8266_A6F79A/rpc -m '{"src":"out-topic","id":1,"method":"FS.List","args":{}}'
    

    HTH,
    Scott

    Thanked by 1Sergey
  • rojerrojer Dublin, Ireland

    @scotthernandez thank you for providing a detailed answer!

  • holla2040holla2040 Montrose, CO
  • alanm101alanm101 Cape Town

    Thanks Scott. That sorted me out.

  • mtraxmtrax Canberra

    Scott it would be great if you doc could be added to the official documentation for other people to understand, I eventually found my way here after a google.

  • mtraxmtrax Canberra
    edited June 23

    is there something I'm doing wrong

    I execute the rpc command:
    mos --port mqtt://mythbox:1883/esp8266_1BC722 call boost '{"relaystate":"on"}'
    {
    "relaypin": 12,
    "relaystate": 0,
    "relayon": "true",
    "type": "set"
    }

    and I get the json output as expected but console is showing the following error 3 times:

    [Jun 23 15:02:53.539] mg_rpc_channel_mqtt_ Cannot reply to RPC over MQTT, no dst: [{"id":1,"src":"esp8266_1BC722","result":{"relaypin":12,"relaystate":0,"relayon":"true","type":"set"}}]

  • nliviunliviu Romania

    The command looks ok.
    Maybe you have some issues in your rpc handler.
    What is the output of mos --port ws://<device_ip>/rpc call boost '{"relaystate":"on"}'?

  • mtraxmtrax Canberra

    hmmm.. I haven't changed anything other than a cold boot and now its not showing that message anymore..

  • edited July 11

    How about if you want to make an RPC call over AWS? What's the format? I am trying this:
    {"call":"FS.List"}
    And I'm getting this response from Mongoose OS (mos console and also on my AWS console):

    {
      "error": {
        "code": -1,
        "message": "unknown command"
      }
    }
    
  • rojerrojer Dublin, Ireland

    it's method, not call. "method": "FS.List"

  • edited July 12

    When I try

    {
      "method": "FS.List"
    }
    

    I get the same error message; "unknown command". :(.

    EDIT: I tried my own method that I implemented with mgos_mqtt_add_global_handler(), and that can work.

    {
     "command": "set_led_level", 
     "level": 5
    }
    

    Sets the LED level on my device :). That's all I was really after in the end anyway!

Sign In or Register to comment.