Arduino 程序耗尽资源 [英] Arduino program running out of resources

查看:39
本文介绍了Arduino 程序耗尽资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重置了 wifi shields 固件,遵循 教程.如果没有正确完成,这会导致以后出现任何错误吗?

I reset the wifi shields firmware, following this tutorial. Could that cause any errors later, if not done correct?

我仍然不知道问题出在哪里.所以我把我的 .ino 文件 这里.(已编译:25.052 字节)

I have still no idea where the problem could be. So i put my .ino file here. (it's compiled: 25.052 Bytes)

如果有人可以检查它是否在另一个环境中运行并报告它,我会非常高兴.我用假值替换了传感器,所以你不需要添加任何硬件.只需在草图顶部添加您的路由器 ssid 和密码即可.

I would be very pleased if someone could check if it's running on another environment, and report about it. I replaced the sensor by fake values, so you don't need to add any hardware. Just need to add your router ssid and the password on top of the sketch.

如果您注释掉 SD 卡部分,您会看到它运行了一段时间.但一般不会改变.它的行为看起来像是耗尽了任何资源,就像 Udo Klein 提到的 SRAM.

If you comment the SD card part out you will see that it's running for some more time. But in general it doesn't change. It's behaviour looks like running out of any resource, like Udo Klein mentioned SRAM.

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

Just to check if i understood correctly:

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

  • less code causes more free flash memory. But that should not affect the running program in SRAM isn't it? --> But why then different behaviour if some code parts (like SD card) are commented out?

如果我保存了很多变量,它会降低空闲 SRAM 的数量.(然后它应该可以被 MemoryFree.h lib 看到 --> 这告诉我 const ~6kB free)

if i save lots of variables it lowers the amount of free SRAM. (Then it shoud be visible by MemoryFree.h lib --> which is telling me const ~6kB free)

每次我从一个函数返回"时,它都会减少必要的堆栈内存大小,并且本地定义的函数变量通过它的lef调用它的解析器?

every time i 'return' from a function it will reduce the neccessary stack memory size, and local defined function variables call it's desctuctor by itslef?

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

Are there values saved in heap memory which is growing with every loop but lose reference at the end of a function call?

就像昨天一样运行相同的代码,它运行了两次(-> 只要程序在运行,它就会不断地发出 http 请求并返回到主循环()方法).重启就成功了!

just run the same code like yesterday and it was working twice (-> it made continuously http requests and returned to the main loop() method as long as the program was running). A restart did it!

所以看起来原因不仅在此处显示的代码逻辑内.我可以在哪里寻找任何想法?是否可以重新定义变量而不是覆盖那里的值?

so it looks like the reason is not only inside the code logic shown here. Any ideas where i could look for? Could there variables redefeined instead overwritting there value?

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

My C Code for Wifi shield on Arduino Mega 2560 should read out sensor data and make a HTTP request. Then print out both results to SD Card.

负责从[1]调用的网络请求的函数,只有在第一次调用后才返回.

The function responsible for network requests called from [1], return ONLY after it's first call.

setup方法是初始化串口和sensor pin,进一步调用下面的Wifi初始化方法initWifi()建立网络连接.

The setup method is initializing the serial connection and the sensor pins, further calling the following Wifi initialization method initWifi()to establish a network connection.

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() 方法中(在它完美运行一次之后!).

requestTimestamp() is the function which makes an HTTP HEAD request, and never return back into the loop() method (after it was working once perfectly!).

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()
}

控制台输出看起来像这样:

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(),连接是否仍然打开?

Does the server care about how much of it's response i read out? Could it be that the connection is still open, despite: HTTP 1.0, Connection: close and client.stop()?

我能成像的唯一原因是与网络有关.

The only reason i can imaging would be something related to the network.

推荐答案

另一个原因可能是内存不足.您想了解 progmem.此外,您正在使用 String 对象.我会尽量避免这些.至少你应该检查你实际还剩多少内存..

Another reason might be that you are running out of RAM. You want to learn about progmem. In addition you are working with String objects. I would try to avoid those. At least you should check how much RAM you have actually left..

这篇关于Arduino 程序耗尽资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆