Arduino Yún Bridge: simple command failing

1.1k views Asked by At

I created the following sketch, based almost entirely on the Bridge tutorial provided on arduino.cc.

I can't figure out why the example Bridge script has worked for me (toggling the LED on pin 13 by curling URIs like arduino.local/arduino/digital/13/1), but this much simpler sketch responds with my failure string, "Unrecognized command: hello" when I curl arduino.local/arduino/hello/.

What am I missing?

#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);
}

Ultimately, I'd like to use a longer, random string as a key--in place of "hello"--allowing me to activate a connected component from a device that has stored the secret (e.g. an smart phone with the URI stored as a button on the home screen).

1

There are 1 answers

0
Eric On BEST ANSWER

What I missed in the example was the exact behavior of these Stream functions:

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

That condition will only be true if "hello" is not the final segment of the URI. What tipped me off was the mode command in the Bridge example code. It parsed the final segment (expecting "input" or "output") like this:

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

This was perplexing because I hadn't assumed the Yun server would strip the final '/' when I curled:

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

tl;dr:

Use readStringUntil('\r') for parsing the final segment of a URI.