thingsboard / thingsboard-client-sdk

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

Error with RPC response for setting GPIO #32

Closed ahmed-1a closed 1 year ago

ahmed-1a commented 4 years ago

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

lc2a commented 2 years ago

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

image

image

saifuhameed commented 2 years ago

@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
}
chipluci commented 1 year ago

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}'

MathewHDYT commented 1 year ago

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