Arduino Yún 桥:简单命令失败

Arduino Yún Bridge: simple command failing

本文关键字:简单 命令 失败 Arduino      更新时间:2023-10-16

我创建了以下草图,几乎完全基于 arduino.cc 提供的 Bridge 教程。

我不知道为什么示例 Bridge 脚本对我有用(通过卷曲 URI arduino.local/arduino/digital/13/1 之类的 URI 来切换引脚 13 上的 LED),但是当我卷曲时,这个更简单的草图会响应我的失败字符串"无法识别的命令:你好"arduino.local/arduino/hello/

我错过了什么?

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer server;
void setup() {
  Serial.begin(9600);
  // Bridge startup
  pinMode(13,OUTPUT);
  digitalWrite(13, HIGH);
  Bridge.begin();
  digitalWrite(13, LOW);
  server.begin();
}
void loop() {
  // Get clients coming from server
  YunClient client = server.accept();
  // There is a new client?
  if (client) {
    // Process request
    process(client);
    // Close connection and free resources.
    client.stop();
  }
  delay(50); // Poll every 50ms
}
void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');
  if (command == "hello") {
    client.println(F("I will do your bidding"));
    return;
  }
  client.print(F("Unrecognized command: "));
  client.println(command);
}

最终,我想使用一个更长的随机字符串作为键 - 代替"hello" - 允许我从存储密钥的设备激活连接的组件(例如,将URI存储为主屏幕上的按钮的智能手机)。

我在示例中遗漏的是这些 Stream 函数的确切行为:

String command = client.readStringUntil('/');
if (command == "hello") { ... }

仅当"hello"不是 URI 的最后一段时,该条件才成立。 让我失望的是 Bridge 示例代码中的 mode 命令。 它解析了最后一个段(期望"输入"或"输出"),如下所示:

String mode = client.readStringUntil('r');

这很令人困惑,因为我没有想到云服务器会在我卷曲时剥离最后的'/'

$ curl "arduino.local/arduino/digital/hello/" -v

博士:

使用 readStringUntil('r') 分析 URI 的最后一段。