thingsboard / thingsboard-client-sdk

Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
MIT License
162 stars 125 forks source link

RPC request from device, null response #222

Closed jmarimonf closed 3 weeks ago

jmarimonf commented 1 month ago

I've used the example 0013-esp8266_esp32_request_rpc.ino, in order to do a RPC request from an ESP32 to Thingsboard server. The request is sent correctly and the rule-chain process and send the response of the time. But the ESP32 shows null as a response.

The code used is:

RPC_Request_Callback timeRequestCallback("getCurrentTime", &processTime);

void processTime(const JsonVariantConst &data) { serializeJsonPretty(data, Serial); }

I use the Thingsboard 0.13.0 and ArduinoJson 6.21.4 library versions.

I've also tried from wokwi simulator, and the problem is the same.

MathewHDYT commented 1 month ago

So serializeJsonPretty into the Serial prints null?

Could you add #define THINGSBOARD_ENABLE_DEBUG 1 on top of your main.cpp file and retry it should print some more debug messages which might help fix the issue.

jmarimonf commented 1 month ago

These are the messages: [TB] Sending data to server over topic (v1/devices/me/rpc/request/1) with data ({"method":"getCurrentTime","params":"{}"}) [TB] Received data from server over topic (v1/devices/me/rpc/response/1) [TB] Calling subscribed callback for request with response id (1) 1 Info rebuda de ThingsBoard: null

jmarimonf commented 1 month ago

The data send by Rule-chain node RPC call reply is: 1726562107145 The metadata: { "deviceName": "Nodemcu-T01-Controlador", "deviceType": "default", "requestId": "10", "serviceId": "tb-mqtt-transport2", "sessionId": "f80c86e9-7c1b-42fe-bef8-91efab7dc2de" }

MathewHDYT commented 1 month ago

Ah I might have an idea, it attempts to serialize the json in the background but simpyl the number 1726562107145 is not a valid json because it isn't a key value pair. Therefore the serialization results in null.

If it would be possible could you return {"time":1726562107145} from the Rule Chain instead, that might fix the aforementioned issue altogether.

jmarimonf commented 1 month ago

Thanks, now is running!

I was using the script on the link: https://thingsboard.io/docs/user-guide/rpc/

var rpcResponse; if (msg.method === "getCurrentTime"){ rpcResponse = new Date().getTime(); } else { rpcResponse = "Unknown RPC request method: " + msg.method;
} return {msg: rpcResponse, metadata: metadata, msgType: msgType};

And I've change it for:

rpcResponse = {"time":new Date().getTime()};

The results now are:

[TB] Sending data to server over topic (v1/devices/me/rpc/request/9) with data ({"method":"getCurrentTime","params":"{}"}) [TB] Received data from server over topic (v1/devices/me/rpc/response/9) [TB] Calling subscribed callback for request with response id (9) 9 Info rebuda de ThingsBoard: { "time": 1726565878777 } 1726565878777

For the code:

serializeJsonPretty(data, Serial);
Serial.println();
Serial.println(data["time"].as<String>());
Serial.println();
MathewHDYT commented 1 month ago

Yeah I probably either need to workaround this or add a comment, because we currently always assume that the response is a fully serializable json key-value pair and not just simply a value. But for now changing it to the expected type will solve the issue :D.