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
}

BridgeClient &client