Yun readStringUntil and readString in same time in loop

Hi all,
I have a litlte problem. I have a termometer and RGB LED conected to my Yun.
To send from web page setups for RGB LED i use:
function sendVal() { $.get('/arduino/rgb/' + r + '.' + g + '.' + b, function(){}); }

To receive information from termometer i yse link: myYunIp/arduino/temp

To do this I am using this code in the loop:

void loop() {
  YunClient client = server.accept();

  if (client) {   

  String command = client.readStringUntil('/');
  
    if (command == "rgb") { 
      analogWrite(r, client.parseInt());
      analogWrite(g, client.parseInt());
      analogWrite(b, client.parseInt());
    } 
String command2 = client.readString();
 command2.trim();        //kill whitespace
  
  if (command2 == "temp") {
     double temp = analogRead(0);
     temp = temp * 0.48828125;
     client.print(temp);
    }
  
  client.stop();
  }
  //delay(50);
}

But it doesn't work :frowning: only work without String comand2 and with if (command == "temp") when i use for read temperature link: myYunIp/arduino/temp/some number

How to do this to use link for read remperature without number and for RGB led with this Java script function

Thanks!!

ignis77:

  String command = client.readStringUntil('/');

Serial.println(command); // <<== ADD THIS LINE

if (command == “rgb”) {
     analogWrite(r, client.parseInt());
     analogWrite(g, client.parseInt());
     analogWrite(b, client.parseInt());
   }

ignis77,
you fix. Add code, SEE ABOVE. Tell use what it says.
Jesse

It sounds like you want to implement two different commands:

  • rgb - which accepts three numbers and sets an output color
  • temp - which reads, scales, and returns a temperature

Is this correct?

If so, the problem is that you have it coded as a single command. The issue is that after reading a command string and comparing it to "rgb" you are reading a second command string. If you want to process two different commands, but one at a time, you want to read the command once and test it twice.

Try this:

void loop() {
void loop() {
  YunClient client = server.accept();

  if (client) {

    String command = client.readStringUntil('/');
    command.trim();

    if (command == "rgb") {
      analogWrite(r, client.parseInt());
      analogWrite(g, client.parseInt());
      analogWrite(b, client.parseInt());
    }
    else if (command == "temp") {
      double temp = analogRead(0);
      temp = temp * 0.48828125;
      client.print(temp);
    }

    client.stop();
  }
  //delay(50);
}

The changes I made are to only read a single command, and always trim it. (trim() will do nothing for the rgb command, but will remove the trailing newline that would be present on the temp command.) Then I put the test for the temp command as an else clause for the rgb command.

This new code should be able to handle two commands:

  • arduino.local/rgb/1.2.3 or arduino.local/rgb/1/2/3
  • arduino.local/temp

Note that for the rgb command, the delimiters between numbers can be anything that isn't a digit or a '+' or '-'. So slashes, periods, commas, dashes, even letters should work.

Now, if you really are trying to combine both functions into a single command, please give us an example of what that command might look like.

Hi

Jesse unfortunately does not work...
way to solve of ShapeShifter works :slight_smile:

Thanks a lot for help and hints!
I wish you a happy new year :slight_smile:

the command.trim(); helps me a lot, and i don't know why every time i use String command = client.readStringUntil('/'); there will be a whitespace at the end of the command

can anyone explain it?

HSpaigu:
the command.trim(); helps me a lot, and i don't know why every time i use String command = client.readStringUntil('/'); there will be a whitespace at the end of the command

You are trying to read until the next '/' character. But if that is the last token in the string, you will get the newline end of line delimiter before you get another '/' character. It's that trailing newline that gets into the string, the one that marks the end of the line.