Break/Stop loop with MQTT payload/message

[quote author=MrGlasspoole link=topic=234632.msg1689015#msg1689015 date=1398040799]
I want to stop a loop with MQTT but can't get it to work.

You have not really given us much to go on.
You would be more likely to get meaningful help if you post your entire code or a minimal, compilable example that shows the probem

Here's a thought ot two. My comments in the code.

int test = 0;   // Where is this declared? (where in the code)
void callback(char* topic, byte* payload, unsigned int length) {

  // place Serial.print() statements here, to show length, payload, and test
 
  if (length == 9 && strncmp((char*)payload,"volume-up",9)==0) {
    for (int i = 0; i < 1000; i++) {
      Serial.println("up");
      Serial.println(test);
      delay(50);
      if (test == 1) {
        break;
      }
    }
  }

  if (length == 11 && strncmp((char*)payload,"volume-stop",11)==0) {
    Serial.println("stop");
    Serial.println(test);
    test = 1;
  }
}

If i call "volume-up" and get my test prints and then call "volume-stop" nothing happens.

Insufficient information. What test prints do you get? What happens when you call "volume-stop". "Nothing happens" is meaningless. Does it keep printing "up"? Does it stop printing "up"? If it does stop, is it because of the timeout (ie. "up" is printed 1000 times)? Does it print "stop"?

If i call "volume-stop" first and then again i see that "test" has a value of "1".
Also "volume-up" is then printed just once.

Of course. Once test has been set to 1, you will break out of the for loop after the first time though it.

Why are you testing for length in the two if statements? You are doing strncmp() with the length of the desired compare string. Granted, it's slightly faster to check for length, and not bother with the compare if it's not the right length. Are you worried about speed? If you already know the length of the payload, you don't need strncmp(), and if you don't, your "if length --" may be preventing you from getting into the second if.