我按照this重置了wifishield固件。教程。 如果做得不正确,以后会导致任何错误吗?
编辑2
我还是不知道问题出在哪里。所以我把我的 .ino 文件
here 。 (编译后:25.052 字节)
如果有人可以检查它是否在另一个环境中运行并报告它,我将非常高兴。 我用假值替换了传感器,所以你不需要添加任何硬件。只需在草图顶部添加您的路由器 ssid 和密码即可。
如果您将 SD 卡部分注释掉,您将看到它运行了更多时间。但总体来说是不会改变的。它的行为看起来像是耗尽了任何资源,就像 Udo Klein 提到的 SRAM 一样。
只是为了检查我是否理解正确:
代码越少,可用闪存就越多。但这应该不会影响SRAM中正在运行的程序,不是吗? --> 但是,如果某些代码部分(如 SD 卡)被注释掉,为什么会有不同的行为呢?
如果我保存大量变量,则会减少可用 SRAM 的数量。 (然后它应该可以通过 MemoryFree.h lib 看到 --> 它告诉我 const ~6kB free)
每次我从函数“返回”时,它都会减少必要的堆栈内存大小,并且本地定义的函数变量会自行调用它的析构函数?
堆内存中保存的值是否会随着每次循环而增长,但在函数调用结束时会丢失引用?
编辑
只需像昨天一样运行相同的代码,它就工作了两次(-> 只要程序正在运行,它就会连续发出 http 请求并返回到主 Loop() 方法)。 重新启动就成功了!
所以看来原因不仅仅在于此处显示的代码逻辑内部。我可以在哪里寻找任何想法?是否可以重新定义变量而不是覆盖其中的值?
<小时/>我的 Arduino Mega 2560 上的 Wifi 扩展板的 C 代码应该读取传感器数据并发出 HTTP 请求。然后将两个结果打印到SD卡上。
问题:
负责从[1]调用的网络请求的函数,仅在第一次调用后返回。
<小时/>setup方法是初始化串口连接和传感器引脚,进一步调用下面的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()
}
控制台输出
如下所示:
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(),连接是否仍处于打开状态?
我可以成像的唯一原因是与网络有关的东西。
最佳答案
另一个原因可能是您的 RAM 不足。您想了解progmem 。此外,您正在使用 String 对象。我会尽力避免这些。至少你应该检查how much RAM you have actually left. .
关于c - Arduino 程序资源不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19081698/