RPC call over MQTT

Can someone help me with the syntax for a RPC call over MQTT
eg if I have the following handler for ‘temperature’ how do I invoke that over MQTT?

eg

RPC.addHandler(‘temperature’, function(args) {
print(‘Temperature RPC’);
return JSON.parse(requestTemperature());
});

RPC call via MQTT.

You need to add the rpc-mqtt library in mos.yml.

I already have that library added.

the following doesn’t seem to work

mosquitto_pub -h myserver -t “esp32_871123/rpc” -m “call temperature”

No, it doesn’t work like that.
It works the way described in the link I provided in my previous post.

I assume mean something like this :

Published to esp32_871123/rpc` :

{ "id": 123, "src": "foo", "method": "Sys.GetInfo"}

not sure what ID # is ?
but would this translate to

mosquitto_pub -h myserver -t “esp32_871123/rpc” -m “{ "id": 123, "src": "foo", "method": "Sys.GetInfo"}”

id is the request identifier.

A combination of mosquitto_pub/mosquitto_sub works, but it’s more convenient to use

mos --port mqtt://my.mqtt.server:1883/esp32_6732ac call sys.getinfo

The mos tool will send the request and return the results.

strange when I execute I see the RPC is executed twice in the log output

mos --port mqtt://mythbox:1883/esp32_872911 call temperature

RPC.addHandler('temperature', function(args) {
  print('Temperature RPC');
  return JSON.parse(getTemperature());
});

console output

[Jul 20 17:12:17.763] Temperature RPC 
[Jul 20 17:12:18.127] 0 Sensor# 28ffe4e964150121 Temperature: 22.880000 *C 
[Jul 20 17:12:18.170] 1 Sensor# 28ffd7c96415019b Temperature: 22.880000 *C 
[Jul 20 17:12:18.243] devices: 2 msg: { "status" :"ok", "devices": [ {"Temperature":22.880000,"device":"28ffe4e964150121"},{"Temperature":22.880000,"device":"28ffd7c96415019b"} ] } 
[Jul 20 17:12:18.243] mg_rpc.c:309            temperature via MQTT 
[Jul 20 17:12:18.243] Temperature RPC 
[Jul 20 17:12:18.612] 0 Sensor# 28ffe4e964150121 Temperature: 22.880000 *C 
[Jul 20 17:12:18.681] 1 Sensor# 28ffd7c96415019b Temperature: 22.880000 *C

Yes, I’ve also noticed that.

Can you sniff the network somewhere to make sure you are not receiving your msg twice ? QoS 0 and 1 have no guarantee of msgs not being sent more than once (QoS0 --> best effort: fire and forget, should be once or nothing; QoS1 --> delivered at least once, can be sent several times on a noisy connection); or use QoS 2 where available to at least eliminate that possibility.

I just realised this bug seems to still exists…

It’s not a bug. I’ve just found out about this setting which by default is true. Make it false and only one topic will be subscribed.

1 Like