从Thingsboard到Arduino的RPC调用问题(ESP32)

问题描述 投票:0回答:2

我创建了一个简单的Arduino程序,它从我的thingsboard仪表板上的KnobControl Widget接收位置值并更新伺服位置。该程序基于ThingsBoard网站上的ESP32 Pico Kit GPIO control and DHT22 sensor monitor示例,主要是工作。

到目前为止,我的代码能够连接到仪表板并从服务器接收“setPos”和“getPos”RPC命令,到目前为止,它已成功运行“setPos”调用的关联RPC_Response函数,并且可以移动伺服。

但是,当我刷新仪表板并向控制器发出“getPos”调用以获取当前伺服值时,我在串行输出中得到一条SDK消息,指示控制器收到命令,但关联的RPC_Response函数从不调用。我不确定我错过了什么,但这是我到目前为止写的完整代码示例:

#include <WiFi.h>
#include <ESP32Servo.h>
#include <ThingsBoard.h>

// Constants
#define SERVO_PIN               19      // Servo Output Pin
#define SERVO_UPDATE_INTERVAL   20      // Speed of servo position updates

// Helper macro to calculate array size
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

// WiFi Login Info
#define ssid                "IoT"
#define password            "password"

// MQTT Broker IP address:
#define THINGSBOARD_SERVER  "10.10.0.30"

// MQTT Client Info
#define ACCESS_TOKEN "ESP32_DEMO_TOKEN"

// Servo Variables
int minUs = 500;
int maxUs = 2400;
int SetPosition = 0; // ServoMotor Position Setpoint
int Position = 0;    // ServoMotor current position

// Control/Timing Variables
long lastServoTime = 0;       // keeps track of timestamp since the last servo update occured

// Objects
Servo ServoMotor;
WiFiClient espClient;
ThingsBoard client(espClient);

// RPC Callbacks
RPC_Callback callbacks[] = {
  { "setPos", setPosition },
  { "getPos", getPosition },
};

void setup() {
  Serial.begin(115200);

  // Initialize Servo
  ServoMotor.setPeriodHertz(50);                  // Standard 50hz servo
  ServoMotor.attach(SERVO_PIN, minUs, maxUs);

  // Initialize the WiFi and MQTT connections
  setup_wifi();
}

void loop() {
  // Update/refresh the Wifi/MQTT connection
  updateWirelessConnection();

  // Update Servo Positions
  updateServo();
}

void updateWirelessConnection()
{
  if (!client.connected()) {
    reconnect();
  }

  client.loop();
}

// Processes function for RPC call "getPos"
// RPC_Data is a JSON variant, that can be queried using operator[]
// See https://arduinojson.org/v5/api/jsonvariant/subscript/ for more details
RPC_Response getPosition(const RPC_Data &data)
{
  Serial.println("Received the get Position Method");
  return RPC_Response(NULL, SetPosition);
}

// Processes function for RPC call "setPos"
// RPC_Data is a JSON variant, that can be queried using operator[]
// See https://arduinojson.org/v5/api/jsonvariant/subscript/ for more details
RPC_Response setPosition(const RPC_Data &data)
{
  Serial.print("Received the Set Position method: ");
  SetPosition = data;
  Serial.println(SetPosition);
  return RPC_Response(NULL, SetPosition);
}

void updateServo()
{
  long currentTime = millis();

  if (currentTime - lastServoTime > SERVO_UPDATE_INTERVAL) {
    lastServoTime = currentTime;

    // Approach the Horizontal set point incrementally and update the servo if applicable
    if (Position != SetPosition) {
      Position = SetPosition;
      ServoMotor.write(Position);
    }
  }
}

void setup_wifi() {
  delay(10);

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");

    // Attempt to connect
    if (client.connect(THINGSBOARD_SERVER, ACCESS_TOKEN)) {
      Serial.println("connected");

      // Perform a subscription. All consequent data processing will happen in
      // callbacks as denoted by callbacks[] array.
      if (!client.RPC_Subscribe(callbacks, COUNT_OF(callbacks))) {
        Serial.println("Failed to subscribe for RPC");
        return;
      }

      Serial.println("Subscribe done");
    } else {
      Serial.println("Failed to connect. Trying again in 5 seconds...");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

此外,这是我刷新仪表板时在串行监视器中看到的响应:

{"method":"getPos"}
[SDK] received RPC getPos
[SDK] response {}

当我更新仪表板上旋钮控件的位置时,这是我在串行监视器中收到的内容:

{"method":"setPos","params":"135"}
[SDK] received RPC setPos
[SDK] calling RPC setPos
Received the Set Position method: 135
[SDK] response 135

注意:我对setPos调用没有任何问题,这会正确调用RPC函数。

最后一点,当我刷新仪表板时,旋钮控件小部件顶部会出现一条错误消息“无法解析响应:[object Object]”。

所以主要的问题是没有调用正确的RPC函数。您认为这里的问题是什么?

c++ arduino rpc dashboard thingsboard
2个回答
0
投票

好吧,我在ThingsBoard包装器库中探索,试图更好地理解从服务器处理传入的RPC文本字符串的代码发生了什么。在sendDataArray函数里面,我在for循环中发现了这段好奇的代码,它扫描回调数组并将其与嵌入式RPC字符串相匹配:

// Do not inform client, if parameter field is missing for some reason
if (!data.containsKey("params")) {
    continue;
}

如果调用的RPC方法不包含params字段,则完全忽略方法调用。不幸的是,这是getPos RPC调用的情况。所以为了解决这个问题,我只是注释掉了上面的代码,现在一切正常。

@thingsboard团队,这段代码的原始理由是什么?如何将getValue RPC调用传递给客户端进行处理?


0
投票

我试图在另一篇文章中解决这个问题,没有意识到你已经发布了这个。回复没有帮助。我的查询链接如下。

https://github.com/thingsboard/ThingsBoard-Arduino-MQTT-SDK/issues/10#issuecomment-474368259

我还注意到,当旋钮控件没有发出参数键时,thingsboard.h包装器基本上会使草图短路。由于旋钮控件在执行getPos方法时不会发出任何“params”键,因此草图代码永远不会有机会响应最新值。

我玩了新的调试终端小部件,并通过发出带有参数值的getPos方法来测试旋钮控件小部件RPC调用。如果在New debug terminal提示符下输入'getPos',则会在调试终端中将空括号作为响应,并在串行输出中获得{“method”:“getPos”}。就像草图一样。

如果在提示符处输入“getPos 1”,则应在终端窗口中将SetPosition值作为响应,并在串行输出中使用“{”方法“:”getValue“,”params“:1}'。值1并不重要,因为有某种参数可以触发包装器。

请注意,如果在调试窗口中输入“setPost 12”,则会将SetPosition变量值更新为12。

我的结论是旋钮控制小部件被削弱了。如果要使用包装器,它需要发出'params'密钥对。

另一个问题是,当您在调试终端中使用getPos时,旋钮控件中显示的数值不会更新。不确定这是否是最好的测试。使用仪表板刷新浏览器窗口会产生类似的结果。我认为getMethod应该这样做。

Thingsboard团队:这里发生了什么,可以改进吗?

© www.soinside.com 2019 - 2024. All rights reserved.