1

I wold like to make me a REST API with string values and more string Keys.

like: ~http://url.tld/foo/bar/xy to get "bar" and do more...

Can somebody tell me how to get the second word out of the URL? In this case "bar".

The tutorial show's how to use /arduino/digital/13/1 as to set pin 13 High.I would like to get a different syntax. Something like this: arduino/device1/switch/on

By now i searched quite long and tested different things as you can see below, but i can't get more than one word caught to trigger from the client object.

is boils down to the question why my client object changes the payload.

this part:

void process(BridgeClient client) {
  String url0 = client.readString();
  Console.print("process url0: ");      // --> is "bar/xy"
  Console.println(url0);
  String command = client.readStringUntil('/');
  String url1 = client.readString();
  Console.print("process url1: ");
  Console.println(url1);               // --> is "" aka:empty!

i made a MWE to show what i already did:

// Libraries:
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
#include <Console.h>

// Constants:
const bool on = HIGH;
const bool off = LOW;

// Objects:
BridgeServer server;

void setup() {
  pinMode(6, INPUT_PULLUP);
  Bridge.begin();
  server.begin();
  Console.begin();
  while (!Console);
  Console.println("setup done..");
}

void loop() {
  BridgeClient client = server.accept();
  if (client) {
    process(client);
    client.stop();
  }
}

void process(BridgeClient client) {
    String url0 = client.readString();
    Console.print("process url0: ");   // --> is "bar/xy"
    Console.println(url0);
    String command = client.readStringUntil('/');
    String url1 = client.readString();
    Console.print("process url1: ");
    Console.println(url1);             // --> is "" aka:empty!
  if (command == "device1") {
    device1(client);
  }
  if (command == "status") {
    Console.print("process: command == status  | command: ");
    Console.println(command);
    statusCommand(client);
  }
}

void device1(BridgeClient client) {
  String URL = client.readString();                              
  Console.println("URL:");
  Console.println(URL);                               // ---> is "on"
  String mode = client.readStringUntil('\r');
  Console.println("mode:");
  Console.println(mode);                            // ---> is ""
  String value = getStringPartByNr(URL, '/', 1);
  Console.println("value:");
  Console.println(value);                            // ---> is ""
  power(on);
  if ( URL == "on"){
    client.print(F("found /on"));
    power(on);
  }
  else if ( URL == "off"){
    client.print(F("found /off"));
    power(off);
  }
}

void power(bool on_off){
  bool checkpin = digitalRead(6);                          // machine state
  if (checkpin == LOW && on_off == HIGH) {        // means power is off and on is called
    digitalWrite(13, HIGH);
    Console.print(" checkpin == LOW && on_off == HIGH [1] ");
  }
  else if (checkpin == HIGH && on_off == LOW) { // means power is on and off is called
    digitalWrite(13, LOW);
    Console.print(" checkpin == HIGH && on_off == LOW [2] ");
  }
}

void statusCommand(BridgeClient client) {
}

// splitting a string and return the part nr index split by separator
String getStringPartByNr(String data, char separator, int index) {
    int stringData = 0;        //variable to count data part nr 
    String dataPart = "";      //variable to hold the return text
    for(int i = 0; i<data.length()-1; i++) { //Walk through the text one letter at a time
        if(data[i]==separator) { //Count the number of times separator character appears 
            stringData++;
        } else if(stringData==index) { //get the text when separator is the rignt one
            dataPart.concat(data[i]);
        } else if(stringData>index) { //return text and stop if the next separator appears 
            return dataPart;
            break; // - to save CPU-time
        }
    }
    return dataPart; //return text if this is the last part
}

enter image description here

5
  • maybe you should not create a copy of the Cllient object when passing it into a function. use reference parameter BridgeClient &client Commented Mar 31, 2018 at 9:36
  • i added it as screenshoot. its not clear visible because the console is white as well but underneeth the console output you see that im calling [myarduino].local/device1/on in a browser and the output of url0 [String url0 = client.readString();] is the full string but then after one time calling client.readStringUntil('/') the client seams to be manipulated and url1 [String url1 = client.readString(); ] is empty. Commented Mar 31, 2018 at 13:45
  • same for 'BridgeClient &client'. Commented Mar 31, 2018 at 13:49
  • so you read all the input into ur10 with readString() and the expect to read what from it with readStringUntil()? Commented Mar 31, 2018 at 18:28
  • i expect url1 to be same as url0 because im reading the same object. why is it not the same? Commented Apr 1, 2018 at 7:23

1 Answer 1

1

You misunderstand the networking Client class concept. It is a stream like the Serial object. Bytes are stored in the Client object's buffer only until you read them.

12
  • ok, so how can i pass it thorugh a function without to loose the information about the "xy" string (of myarduino/foo/bar/xy)? Commented Apr 1, 2018 at 8:51
  • hackingmajenkoblog.wordpress.com/2016/02/01/… or forum.arduino.cc/index.php?topic=396450.0 Commented Apr 1, 2018 at 10:51
  • or read it to / and then to / etc. Commented Apr 1, 2018 at 10:56
  • Do you mean to read to device1/foo/bar/ and then to device1/foo/ ? thats not possible because readStringUntil('/'); finds the first and cuts away the rest of the string... Commented Apr 1, 2018 at 19:26
  • not cuts away, stores into output Commented Apr 2, 2018 at 5:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.