2013-05-16 53 views
0

我正在開發一個arduino項目,它在服務器上發出http請求並讀取http頁面內容。在循環中我有一個功能:Arduino在時間之後崩潰

String eventValue = check_for_event(); 

String check_for_event(){ 
if (client.connect(server, 80)) { 
    client.print("GET /check_for_event.php?q="); 
    client.print(class_code); 
    client.println(" HTTP/1.0"); 
    client.println(); 

    return readPage(); 

} else{ return "704"; } 
} 

String readPage(){ 
    stringPos = 0; 
    memset(&inString, 0, 32); 
    int print_flag=0; 
    while(true){ 

    if (client.available()) { 
     char c = client.read(); 
     if (c == '<') { 
     startRead = true; 
     }else if(startRead){ 
     if(c != '>'){ 
      inString[stringPos] = c; 
      stringPos ++; 
     }else{ 
      startRead = false; 
      client.stop(); 
      client.flush(); 
      print_flag=1; 
      return inString; 
     } 

     } 
    } 

    } 
    if(print_flag==0){return "804";} 
} 

這是我的代碼的一部分,我每3秒調用一次。它可以正常工作幾個小時,但突然崩潰,我不明白爲什麼。我發現崩潰在check_for_event函數的某處。

+0

'class_code'裏有什麼?如果您的HTTP請求花費的時間超過三秒,會發生什麼? – Blazemonger

+0

這是我在服務器數據庫中保存的用於識別arduino的代碼。秒數沒有問題,因爲我檢查是否已經超過3秒鐘來創建新的查詢。 – giorgos

回答

0

很可能你的內存不足。既然你不顯示你的代碼,確切的原因很難確定。要麼你有內存泄漏,要麼只是溢出你的內存。我建議檢查接收到的http頁面的大小是否變化,以及崩潰是否只發生在特定的大頁面上。

恕我直言arduino只有2K的RAM是不太理想的HTTP處理。在Raspi的範圍內的東西會更合適。儘管如此,如果你想堅持使用Arduino,你可能需要考慮使用更少內存的技術。首先要對learn about the available memory

接下來是stop wasting memory for string constants。如果這是不夠的 - 這很可能 - 你必須通過你的代碼並優化它。除非您處於成本緊張的限制之下(例如因爲您設計了1000個設備)或者您的時間基本上是免費的,否則切換到更強大的硬件是一個更好的主意。