0

it keeps failing and failing and failing.

Arduino YUN i have this code. It has to go to a site and get a value "reboot" if the value "reboot" found it does the action, if "reboot" not then do nothing else.

Sometimes the reboot command is working, but its not always working, which is making me sad.

Does anyone know why is my code not working?

#include <Bridge.h>
#include <HttpClient.h>

String result = String("");
void setup() {
  delay(5000);
  pinMode(2, OUTPUT);
  Bridge.begin();
  digitalWrite(2, HIGH);
}

void loop() {
  HttpClient client;

  // PIN 2
  client.get("http://www.example.com/output.php?value=reboot");// output.php outputs the value reboot and the reset the value to empty string. after 10 second php puts the value to reboot and then put the value to empty. 
  result = "";
  while (client.available()) {
    char c = client.read();
    result = result + c;
  }

  if(result.indexOf("reboot") >= 0) {// this keeps failing, sometime working and sometime not working
    digitalWrite(2, LOW);
    delay(3000);
    digitalWrite(2, HIGH);
  }

  delay(7000);
}
  • 1
    What do you have when you log `client.read()` with every character? – Fredrick Gauss Mar 02 '16 at 06:39
  • 2
    And if you put `http://www.example.com/output.php?value=reboot` into a normal web-broser, does it work all the time, or does it still have the same problem? What *does* the web-server send back? – Some programmer dude Mar 02 '16 at 06:39
  • 1) client.read() is every characters 2) when i open the url on curl or wget or browsers i have always value correctly coming "reboot" –  Mar 02 '16 at 06:41
  • Which of the two parts sometimes fails? Remember, you are supposed to extract a minimal example demonstrating the issue. – Ulrich Eckhardt Mar 02 '16 at 06:41
  • This is failing: `if(result.indexOf("reboot") >= 0) {` even the result string had value of reboot. –  Mar 02 '16 at 06:42
  • Then remove all the web stuff to get closer to a minimal example. – Ulrich Eckhardt Mar 02 '16 at 06:43
  • 1
    Is the Arduino board connected to your computer in any other way than through a network? E.g. using serial communication? Then simply write every character you receive on using `client.read()` on the serial port, so you can actually see what it receives. – Some programmer dude Mar 02 '16 at 06:48
  • Sir, Arduino YUN has no serial board to debug, i still cant see what i am getting in Arduino YUN. Therefore its only over networking. It would be nice to see what exact value i am getting after the lines `while (client.available()) { char c = client.read(); result = result + c; }` . is it possible with Arduino YUN to output debug info via IP to a port? –  Mar 02 '16 at 06:56
  • Ok, then.. What does the PHP script do? I couldn't understand it from your description – frarugi87 Mar 02 '16 at 07:20
  • PHP script reads a value from MySQL table. "reboot" or "done" when it reads "reboot" it sets it back to "done". value from done to reboot is manually set by me. –  Mar 02 '16 at 07:29
  • 1
    Ok, then if you use the `Serial` object on the Yun do you get any output on the serial monitor? If so try to use it. Moreover, you can try to put a `delay(500);` after the `client.get` (before the call to `client.available`) – frarugi87 Mar 02 '16 at 10:32

1 Answers1

0

it WORKS.

#include <Bridge.h>
#include <HttpClient.h>
String result = String("");
void setup() {
  delay(5000);
  pinMode(2, OUTPUT);
  Bridge.begin();
  digitalWrite(2, HIGH);

}
boolean was_on = false;
void loop() {
  HttpClient client;

  // PIN 2
  client.get("http://ok.example.com/ajax/light");
  delay(500);
  result = "";
  while (client.available()) {
    char c = client.read();
    result = result + c;
  }


  if(result.indexOf("reboot") >= 0) {
    digitalWrite(2, LOW);
    delay(2000);
    was_on = true;
  } else {
    if(was_on) {
      digitalWrite(2, HIGH);
      delay(1000);
      was_on = false;
    }
  }

  delay(3000);
}