Copyright ©

Mongoose OS Forum


RPC over MQTT message format?

holla2040holla2040 Montrose, CO

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

I've tried the following from linux command line
mosquitto_pub -h -p 1883 -t holla2040/rpc -m '{"method":"Sys.GetInfo"}'
mosquitto_pub -h -p 1883 -t holla2040/rpc -m '{"call":"Sys.GetInfo"}'
mosquitto_pub -h -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?



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


    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:// ls

    to 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


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


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

    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:// ls

    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 -p 1883 -t out-topic/# &
    mosquitto_pub -h -p 1883 -t esp8266_A6F79A/rpc -m '{"src":"out-topic","id":1,"method":"FS.List","args":{}}'


    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 2018

    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 2018

    How about if you want to make an RPC call over AWS? What's the format? I am trying this:
    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 2018

    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.