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

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

问题描述

我按照这个教程。
如果没有做错,可能会导致以后出现任何错误吗?



Edit2



我还有不知道问题出在哪里。所以我把我的 .ino文件 这里。 (它的编译:25.052字节)



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



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



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




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


  • 如果我节省了大量的变量它会降低免费的SRAM。 (然后它会被MemoryFree.h lib - >告诉我const~6kB免费)


  • 每次我从一个函数返回'它将减少必要的堆栈内存大小,本地定义的函数变量通过itslef调用它的desctuctor?




是否有值保存在堆内存中,每个循环都在增长,但在函数调用结束时会丢失引用?



编辑



<只是像昨天一样运行相同的代码并且它正在工作两次( - >它连续地发出http请求并且只要程序正在运行就返回到主循环()方法)。
重新启动它!



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






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



问题:



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






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

  void initWifi(){
//检查是否存在盾牌:
if(WiFi.status()== WL_NO_SHIELD){
Serial。 println(WiFi shield not present);
//不要继续:
while(true);
}

//尝试连接到Wifi网络:
while(status!= WL_CONNECTED){
Serial.print(试图连接到SSID: );
Serial.println(ssid);
//连接到WPA / WPA2网络
status = WiFi.begin(ssid,pass);

//连接等待10秒:
delay(10000);
}
Serial.println(连接到wifi);
}

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

  ... 
文件myFile = SD.open(传感器。 txt,FILE_WRITE);
if(myFile){
String timestamp = requestTimestamp(); < ------来自HERE [1]
Serial.println(当前日期是:+时间戳);

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

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

 字符串requestTimestamp(){
Serial.println(\ nRequest timestamp);

WiFiClient客户端;
if(client.connect(server,80)){
client.println(HEAD / HTTP / 1.1);
client.println(主持人: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);

返回time_str; < ---从这里它永远不会返回循环()
}

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

 初始化SD卡。 
尝试连接到SSID:********
连接到wifi

请求时间戳
HTTP / 1.1 302找到
位置: http://www.google.de/?gws_rd=cr&ei=Gm5IUvXHEbeg4AON9YCwCw
缓存控制:私有
内容类型:text / html; charset = UTF-8
Set-Cookie:PREF = ID = 9684aab183999fb2:FF = 0:TM = 1380478490:LM = 1380478490:S = EnaD0yx20-9BT6-4;到期=星期二,29-Sep-2015 18:14:50 GMT;路径= /; domain = .google.com
Set-Cookie:NID = 67 = R6OUfZAakXBBYSp_a9QRO56OzZxYS2X6RmpFlByzSOMgVXalyfYOuilvzQZjaNPRK9409kjjPsDIOEI4h44qIfljzYfS_57MrsQNaKp8S35iMUHKkgLwrkgGs7dRy6gQ;到期=星期一,2014年3月31日18:14:50 GMT;路径= /;域= .google.com; HttpOnly
P3P:CP =这不是P3P政策!有关详细信息,请参阅http://www.google.com/support/accounts/bin/answer.py?hl=zh-CN&answer=151657。
- > 20:14 29.9.2013
日期:2013年9月29日星期日18:14:50 GMT
服务器:gws
内容长度:258
X-XSS-Protection:1; mode = block
X-Frame-Options:SAMEORIGIN
Alternate-Protocol:80:quic
Connection:close

---------- ---------------- 20:14 29.9.2013< - 返回
当前日期之前函数内的最后一行是:20:14 29.9.2013< - 从主循环()方法打印
220 20:14 29.9.2013< - main loop()也




请求时间戳
HTTP / 1.1 302找到
位置:http://www.google.de/?gws_rd = cr& ei = Im5IUvO5GvSs4AOyhYHoAw
缓存控制:私有
内容类型:文本/ HTML; charset = UTF-8
Set-Cookie:PREF = ID = f1a3f58848455aa8:FF = 0:TM = 1380478498:LM = 1380478498:S = cwG8W2Ll10fiiu_e;到期=星期二,29-Sep-2015 18:14:58 GMT;路径= /; domain = .google.com
Set-Cookie:NID = 67 = v_OGI8alGJj4TJEBZSjz9EYUJljTm58uBSxG_rdAcz6OIUNzoDLPGCBx_UlRw5jFkIKINivce2UhisHnEpsWJlFyQVLSG7n9Jkoopo-g2gNi0BgFbVXjXypcvA5SYBX9;到期=星期一,2014年3月31日18:14:58 GMT;路径= /;域= .google.com; HttpOnly
P3P:CP =这不是P3P政策!有关详细信息,请参阅http://www.google.com/support/accounts/bin/answer.py?hl=zh-CN&answer=151657。
- > 20:14 29.9.2013
日期:2013年9月29日星期日18:14:58 GMT
服务器:gws
内容长度:258
X-XSS-Protection:1; mode = block
X-Frame-Options:SAMEORIGIN
Alternate-Protocol:80:quic
Connection:close

< --- HERE:print out of像之前的结果丢失了,没有进一步发生......

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



我可以成像的唯一原因将是与网络相关的东西。

解决方案

另一个原因可能是你的RAM用完了。您想了解 progmem 。此外,您正在使用String对象。我会尽量避免这些。至少你应该查看你实际剩下多少内存。


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

Edit2

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

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.

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:

  • 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?

  • 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)

  • 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?

Edit

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?


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.

Problem:

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


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

The loop() method in general is looking like that:

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

The console output looks like that:

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......

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.

解决方案

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天全站免登陆