thingsboard-client-sdk icon indicating copy to clipboard operation
thingsboard-client-sdk copied to clipboard

Error with RPC response for setting GPIO

Open ahmed-1a opened this issue 5 years ago • 2 comments

Hi,

The following code is from ESP32 Example https://thingsboard.io/docs/samples/esp32/gpio-control-pico-kit-dht22-sensor/

RPC_Response processSetGpioState(const RPC_Data &data)
{
  Serial.println("Received the set GPIO RPC method");

  int pin = data["pin"];
  bool enabled = data["enabled"];

  if (pin < COUNT_OF(leds_control)) {
    Serial.print("Setting LED ");
    Serial.print(pin);
    Serial.print(" to state ");
    Serial.println(enabled);

    digitalWrite(leds_control[pin], enabled);
  }

  return RPC_Response(data["pin"], (bool)data["enabled"]);
}

When the GPIO control widget is used on the dashboard, it send the setGpioStatus RPC call, but the response contains the value for the pin switched only. This response cannot be interpreted by the widget and the pin control on the widget stays at the default position.

How to send the proper response which includes the status of all pins using ThingsBoard library? This can help in implementing a getGpioStatus callback as well.

The following example for ESP8266 works perfectly because the response to the RPC call contains all the pins, however, this example does not use the the ThingsBoard library. https://thingsboard.io/docs/samples/esp8266/gpio/

Thank you.

@forGGe @ferchinas

ahmed-1a avatar Feb 16 '20 02:02 ahmed-1a

I changed the RPC_Response type from Telemetry to String and added getGpioStatus Pls see : https://wokwi.com/projects/328082930673910354

image

image

lc2a avatar Apr 05 '22 15:04 lc2a

@forGGe @ferchinas

Looks like a bug either in ThingsBoard.h or ArduinoJson.h. Thingsboard is ignoring the pin data from data["pin"] which is supposed to be in const char type. An easy work around is to use array of const char and use pin data to select the item from array and pass to the RPC_Response.

const char  * pinarray[]={"0","1","2","3","4"}; // Numbers corresponds to GPIO pin number in ThingsBoard dashboard GPIO widget
RPC_Response processSetGpioState(const RPC_Data &data){
  int pin = data["pin"];
  bool enabled = data["enabled"];
  digitalWrite(leds_control[pin], enabled);
  return RPC_Response( pinarray[pin] ,(bool)data["enabled"]); //pinarray[pin] is a workaround to send const char key as telemetry
}

saifuhameed avatar Sep 11 '22 12:09 saifuhameed

Tôi đã thay đổi loại RPC_Response từ Từ xa thành Chuỗi và thêm getGpioStatus. Vui lòng xem: https://wokwi.com/projects/328082930673910354

hình ảnh

hình ảnh

wow i find that bug when i try run you code could not convert 'String(led_delay, 10)' from 'String' to 'RPC_Response {aka Telemetry}'

chipluci avatar Nov 21 '22 10:11 chipluci

@imbeacon Can be marked as enhancement as well refers to the same underlying feature request as #79

MathewHDYT avatar Mar 13 '23 13:03 MathewHDYT