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 separatorString 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}- maybe you should not create a copy of the Cllient object when passing it into a function. use reference parameter
BridgeClient &client2018-03-31 09:36:00 +00:00CommentedMar 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.novski– novski2018-03-31 13:45:07 +00:00CommentedMar 31, 2018 at 13:45
- same for 'BridgeClient &client'.novski– novski2018-03-31 13:49:07 +00:00CommentedMar 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()?2018-03-31 18:28:28 +00:00CommentedMar 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?novski– novski2018-04-01 07:23:38 +00:00CommentedApr 1, 2018 at 7:23
1 Answer1
You misunderstand thenetworking Client class concept. It is astream like the Serial object. Bytes are stored in the Client object's buffer only until you read them.
- ok, so how can i pass it thorugh a function without to loose the information about the "xy" string (ofmyarduino/foo/bar/xy)?novski– novski2018-04-01 08:51:54 +00:00CommentedApr 1, 2018 at 8:51
- hackingmajenkoblog.wordpress.com/2016/02/01/… orforum.arduino.cc/index.php?topic=396450.02018-04-01 10:51:05 +00:00CommentedApr 1, 2018 at 10:51
- or read it to / and then to / etc.2018-04-01 10:56:58 +00:00CommentedApr 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...novski– novski2018-04-01 19:26:37 +00:00CommentedApr 1, 2018 at 19:26
- not cuts away, stores into output2018-04-02 05:50:49 +00:00CommentedApr 2, 2018 at 5:50
Explore related questions
See similar questions with these tags.

