Arduino程序耗尽资源

时间:2013-09-29 18:29:13

标签: c function http arduino wifi

我在this教程之后重置了wifi屏蔽固件。 如果做得不对,可能会导致以后出现任何错误吗?

EDIT2

我仍然不知道问题出在哪里。所以我把.ino file here。 (编译:​​25.052字节)

如果有人可以检查它是否在另一个环境中运行并报告相关内容,我会非常高兴。 我用假值替换了传感器,因此您无需添加任何硬件。只需要在草图上添加路由器ssid和密码。

如果你评论SD卡部分,你会发现它已经运行了一段时间。但总的来说它并没有改变。它的行为似乎耗尽了任何资源,比如Udo Klein提到的SRAM。

只是为了检查我是否理解正确:

  • 更少的代码会导致更多的免费闪存。但那不应该影响SRAM中的运行程序不是吗? - >但是,为什么不同的行为如果某些代码部分(如SD卡)被注释掉了?

  • 如果我节省了大量变量,它会降低可用SRAM的数量。 (然后它可以通过MemoryFree.h lib - >看到它告诉我const~6kB免费)

  • 每次从函数返回时,它都会减少必要的堆栈内存大小,本地定义的函数变量会通过它的半身调用它的desctuctor吗?

堆内存中是否保存了值,这些值随着每个循环而增长但在函数调用结束时丢失了引用?

修改

只运行与昨天相同的代码并且它正在工作两次( - >它不断发出http请求并返回主循环()方法,只要程序正在运行)。 重启就做了!

因此看起来原因不仅仅在于此处显示的代码逻辑内部。我能找到的任何想法?可以重新改造变量而不是覆盖那里的价值吗?


Arduino Mega 2560上用于Wifi屏蔽的我的C代码应该读出传感器数据并发出HTTP请求。然后将两个结果打印到SD卡。

问题:

负责从 [1] 调用的网络请求的函数,仅在首次调用后返回。


设置方法是初始化串行连接和传感器引脚,进一步调用以下Wifi初始化方法initWifi()以建立网络连接。

void initWifi(){
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
  Serial.println("WiFi shield not present");
  // don't continue:
  while(true);
}

  // attempt to connect to Wifi network:
  while(status != WL_CONNECTED){
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network  
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  } 
  Serial.println("Connected to wifi");
}

loop()方法通常看起来像这样:

...
File myFile = SD.open("sensor.txt", FILE_WRITE);      
if(myFile){    
  String timestamp = requestTimestamp();         <------ call from HERE [1]
  Serial.println("current date is: "+timestamp);

  myFile.println(String(cm)+"  "+timestamp);
  myFile.close();
  Serial.println(String(cm)+"  "+timestamp);
}
...

requestTimestamp()是发出HTTP HEAD请求的函数,并且永远不会返回到loop()方法(在它完美地工作之后!)。

String requestTimestamp(){
  Serial.println("\nRequest timestamp");

  WiFiClient client;
  if(client.connect(server, 80)){
    client.println("HEAD / HTTP/1.1");
    client.println("Host:www.google.com");
    client.println("HTTP-date: asctime-date");
    client.println("Connection: close");
    client.println();
  }  

  String time_str = "-1";
  boolean timestampKnown = false;
  while(client.connected() && !timestampKnown){
    String line = "";
    while(client.available()){    //  && !timestampKnown
      char c = client.read();

      if(c == '\n'){
        if(line.startsWith("Date:")){
          String DATE = line.substring(11, line.length()-4);
          time_str = formatTimestamp(DATE);
          Serial.println("-->"+time_str);
          timestampKnown = true;
        }

        Serial.println(line);
        line = "";
      }else{
        line += String(c);
        c = char();
      }
    }

    //if(timestampKnown)
      //break;
  }
  client.stop();

  Serial.println("--------------------------"+time_str);

  return time_str;   <--- from here it never returns to loop()
}

console output看起来像这样:

SD card initialized.
Attempting to connect to SSID: ********
Connected to wifi

Request timestamp
HTTP/1.1 302 Found
Location: http://www.google.de/?gws_rd=cr&ei=Gm5IUvXHEbeg4AON9YCwCw
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=9684aab183999fb2:FF=0:TM=1380478490:LM=1380478490:S=EnaD0yx20-9BT6-4; expires=Tue, 29-Sep-2015 18:14:50 GMT; path=/; domain=.google.com
Set-Cookie: NID=67=R6OUfZAakXBBYSp_a9QRO56OzZxYS2X6RmpFlByzSOMgVXalyfYOuilvzQZjaNPRK9409kjjPsDIOEI4h44qIfljzYfS_57MrsQNaKp8S35iMUHKkgLwrkgGs7dRy6gQ; expires=Mon, 31-Mar-2014 18:14:50 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
-->20:14 29.9.2013
Date: Sun, 29 Sep 2013 18:14:50 GMT
Server: gws
Content-Length: 258
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
Connection: close

--------------------------20:14 29.9.2013  <-- last line inside function before return
current date is: 20:14 29.9.2013  <-- printed from main loop() method
220  20:14 29.9.2013  <-- main loop() also




Request timestamp
HTTP/1.1 302 Found
Location: http://www.google.de/?gws_rd=cr&ei=Im5IUvO5GvSs4AOyhYHoAw
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=f1a3f58848455aa8:FF=0:TM=1380478498:LM=1380478498:S=cwG8W2Ll10fiiu_e; expires=Tue, 29-Sep-2015 18:14:58 GMT; path=/; domain=.google.com
Set-Cookie: NID=67=v_OGI8alGJj4TJEBZSjz9EYUJljTm58uBSxG_rdAcz6OIUNzoDLPGCBx_UlRw5jFkIKINivce2UhisHnEpsWJlFyQVLSG7n9Jkoopo-g2gNi0BgFbVXjXypcvA5SYBX9; expires=Mon, 31-Mar-2014 18:14:58 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
-->20:14 29.9.2013
Date: Sun, 29 Sep 2013 18:14:58 GMT
Server: gws
Content-Length: 258
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
Connection: close 

<--- HERE: print out of the results like before is missing, and nothing futher happens......

服务器是否关心我读出的响应量? 可能是连接仍然是打开的,尽管:HTTP 1.0,Connection:close和client.stop()?

我可以成像的唯一原因是与网络有关。

1 个答案:

答案 0 :(得分:0)

另一个原因可能是你的RAM用完了。您想了解progmem。此外,您正在使用String对象。我会尽量避免这些。至少你应该检查how much RAM you have actually left.