【问题标题】:Arduino not writing to fileArduino不写入文件
【发布时间】:2016-05-02 19:33:41
【问题描述】:

我希望 arduino 在进行 ajax 调用时写入文件。 ajax 工作,但它不写入文件。 ajax 处理程序中的所有其他代码都会执行。

void handle_ajax(){
int startUrlIndex= HTTP_req.indexOf("button");
int endUrlIndex = HTTP_req.indexOf(" HTTP");
String url = HTTP_req.substring(startUrlIndex, endUrlIndex);

int startButtonIndex = url.indexOf("device-") + 7;// 7 is length of device-, I really just want the number.
int endButtonIndex = url.indexOf("&");
String button = url.substring(startButtonIndex, endButtonIndex);

int startStateIndex = url.indexOf("state=") + 6; // 6 is length of state=, I really just want the number.
String state = url.substring(startStateIndex);

int device = button.toInt();
int newState = state.toInt();

dim_light(device, newState * 12);
write_config("", "text");
}

bool write_config(String line, String text){
configFile = SD.open("config.ini", FILE_WRITE);

if(configFile){
    configFile.write("Dipshit");
}
configFile.close();
Serial.println("Works.");
return true;
}

【问题讨论】:

  • 你有什么结果?你能在'if(configFile)'里面放一个闪烁的LED吗? SD的引脚是否可用?你用什么盾牌?请提供更多信息
  • 找到了,内存的问题...再次

标签: ajax arduino sd-card arduino-uno arduino-ide


【解决方案1】:

我认为提供的代码没有任何问题。

先检查基础知识:

  • SD 卡是标准 SD 卡或 SDHC 卡。
  • SD 卡使用 FAT16 或 FAT32 文件系统格式化。
  • SD.begin() 命令中的 CS 引脚使用了正确的引脚。这取决于所使用的硬件。 http://www.arduino.cc/en/Reference/SDCardNotes
  • SPI 连接正确(大多数 Arduino 板上的引脚 11、12 和 13)。
  • 硬件 SS 引脚设置为输出(即使它不用作 CS 引脚)。

根据过去的经验,我知道这些小 Arduino 在读取和写入 SD 卡时会很快耗尽 SRAM。 ReadWrite 示例程序仅使用了大约 50% 的 UNO SRAM!
要测试这是否是您的问题,请运行 SD 卡读/写示例程序(在 SD.begin() 命令中使用正确的 CS 引脚)。如果这可行,那么问题很可能是您的 SRAM 已用完。尝试使用具有 4 倍 SRAM 容量的 Arduino MEGA 2560。

编辑:最新的 Arduino IDE (v1.6.8) 实际上计算了全局变量使用了多少 SRAM。它不考虑局部变量。

【讨论】:

  • 对不起,我应该关闭它。
  • 这个答案有帮助吗?如果是这样,请点击答案旁边的“打勾”图标接受它,也可以投票赞成它。这向其他用户表明您发现该答案很有用,并且它还阻止 Stack Exchange 定期“碰撞”您的问题以期获得可接受的答案。感谢您的理解与合作!
  • 感谢您的帮助,我会 1-up 因为它是一个很好的答案,但它没有解决它
【解决方案2】:

发现问题:Ram

arduino 在打开 SD 卡时内存不足导致失败。

如果其他人遇到同样的问题,您需要 300 或更多字节的内存。通过串行打印检查这一点FreeRam()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2015-08-05
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多