为什么我的代码永远不会重启命令?

时间:2016-03-02 06:34:26

标签: c++ linux arduino arduino-yun

它一直失败,失败和失败。

Arduino YUN我有这个代码。如果值“rebo​​ot”发现它执行操作,它必须转到某个站点并获取值“rebo​​ot”,如果“rebo​​ot”则不执行任何其他操作。

有时重启命令正在运行,但它并不总是有效,这让我感到难过。

有谁知道为什么我的代码不起作用?

#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 个答案:

答案 0 :(得分:0)

可行。

#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);
}