如果延迟时间大于30秒,DELAY会崩溃吗?

时间:2014-07-16 19:46:52

标签: arduino

我有这个变量DELAY_INTERVAL,我设置它来测量我的arduino mega板在再次向我的服务器提交请求之前会延迟多长时间。

我发现当这个变量设置为30秒时我的电路板运行正常,但是一旦我将它设置为45秒,我的电路板就会崩溃。

我已插入一堆printlns用于调试目的:

void loop() {
  Serial.print("Free RAM: ");
  Serial.println(getFreeRam(), DEC);

  Serial.println(1);
  getEC();
  Serial.println(2);
  getPH();
  Serial.println(3);
  createPoint(ecSensorString, phSensorString);
  Serial.println(4);

  delay(DELAY_INTERVAL*1000);
  Serial.println(5);
}

请注意,这会无限期地持续30秒而不是45秒。

这是控制台在45秒输出的内容:

Free RAM: 6551
1
getEC:*OK
2
getPH:
3
a
postPointToServer:ec=*OK&ph=&interval=45s
b
c
d
e
f
g
h
4
<output monitor just stalls>

所以我发现延迟是罪魁祸首。但为什么以及如何解决这个问题呢?

如果有人有兴趣,我的其余代码。

void setup() {
  //initRGB();
  Serial.begin(9600);

  ecSensorString.reserve(30);
  phSensorString.reserve(30);

  initSensors();
  initWireless();

  Serial.println("Setup Complete");
}

void loop() {
  Serial.print("Free RAM: ");
  Serial.println(getFreeRam(), DEC);

  Serial.println(1);
  getEC();
  Serial.println(2);
  getPH();
  Serial.println(3);
  createPoint(ecSensorString, phSensorString);
  Serial.println(4);

  delay(DELAY_INTERVAL*1000);
  Serial.println(5);
}

void initWireless() {
  /* Initialise the module */
  if (!cc3000.begin()) {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    //displayRed();
    while(1);
  }
  Serial.println(F("Initialization Complete"));

  Serial.print(F("Attempting to connect to "));
  Serial.println(WLAN_SSID);
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    //displayRed();
    while(1);
  }
  Serial.println(F("Wireless Connected!"));

  /* Wait for DHCP to complete */
  while (!cc3000.checkDHCP()) {
    delay(100); // ToDo: Insert a DHCP timeout!
  }
  Serial.println(F("DHCP Requested!"));

  if (IP_NOT_DOMAIN) {
    ip = cc3000.IP2U32(IP_1, IP_2, IP_3, IP_4);
  } else {
    ip = 0;
    // Try looking up the website's IP address
    while (ip == 0) {
      if (! cc3000.getHostByName(WEBSITE, &ip)) {
        Serial.println(F("Couldn't resolve!"));
      }
      delay(500);
    }
  }
}


void initSensors() {
  Serial2.begin(38400);
  Serial3.begin(38400);

  // Tell the EC sensors to stop continuous mode
  Serial2.print("c,0\r");
  // Tell the pH sensor to stop all readings
  Serial3.print("E\r");

  // TODO: EC sensor's first reading is always empty, possibly delay?
}


void createPoint(String ec, String ph) {
  if (ec && ph) {
    postPointToServer("ec=" + ec + "&ph=" + ph);
  } else if (ec) {
    postPointToServer("ec=" + ec);
  } else if (ph) {
    postPointToServer("ph=" + ph);
  } else {
    Serial.println("createPoint: null");
  }
}


void postPointToServer(String data) {
  Serial.println("a");
  char interval_c[20];
  sprintf(interval_c, "&interval=%ds", DELAY_INTERVAL);
  data += interval_c;

  Serial.print("postPointToServer:");
  Serial.println(data);

  Serial.println("b");
  Adafruit_CC3000_Client www = cc3000.connectTCP(ip, PORT);
  Serial.println("c");

  if (www.connected()) {
    www.fastrprint(F("POST ")); www.fastrprint(WEBPAGE); www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));

    Serial.println("d");
    String auth_raw = serial + ":" + token;
    char auth_input[200];
    char auth_output[200];
    auth_raw.toCharArray(auth_input, auth_raw.length() + 1);
    base64_encode(auth_output, auth_input, auth_raw.length());
    www.fastrprint(F("Authorization: Basic ")); www.fastrprint(auth_output); www.fastrprint(F("\r\n"));

    www.fastrprint(F("Content-Type: application/x-www-form-urlencoded")); www.fastrprint(F("\r\n"));

    Serial.println("e");
    char len_c[7];
    itoa(data.length(), len_c, 10);
    www.fastrprint(F("Content-Length: ")); www.fastrprint(len_c); www.fastrprint(F("\r\n"));

    // Extra empty line for post arguments
    www.fastrprint(F("\r\n"));

    Serial.println("f");
    char data_c[200];
    data.toCharArray(data_c, data.length() + 1);
    www.fastrprint(data_c); www.fastrprint(F("\r\n"));

    www.fastrprint(F("\r\n"));
    www.println();

    //displayGreen();
  } else {
    Serial.println(F("Connection failed"));
    //displayRed();
  }

  Serial.println("g");
  www.close();
  Serial.println("h");
}


void getEC() {
  ecSensorString = "";
  Serial2.print("r\r");       // Read only 1

  char inchar;
  while (Serial2.available()) {
    inchar = (char)Serial2.read();
    if (inchar != '\r') {
      ecSensorString += inchar;
    }
  }

  Serial.print("getEC:");
  Serial.println(ecSensorString);
}


void getPH() {
  phSensorString = "";
  Serial3.print("R\r");       // Read only 1

  char inchar;
  while (Serial3.available()) {
    inchar = (char)Serial3.read();
    if (inchar != '\r') {
      phSensorString += inchar;
    }
  }

  Serial.print("getPH:");
  Serial.println(phSensorString);
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。

你需要在最后放一个L来将这个常量显式转换为一个长整数:

delay(DELAY_INTERVAL*1000L);