-3

I'd like the arduino to write to a file whenever an ajax call is made. The ajax works, but it doesn't write to the file. All other code inside the ajax handler does execute.

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;
}
3
  • 2
    Hi, and welcome to the Arduino StackExchange community. Just a couple of questions to try and clarify the situation: 1. Are you able to write to the SD card outside of your function? I.e., can you write something in setup()? 2. Can you expand your code snippet to include the parts where you configure the SD card?
    – Majenko
    Jan 26, 2016 at 11:38
  • 1
    Where have you defined SD and configFile? The statement configFile.close(); belongs into the {} just after the line with configFile.write("Dipshit");.
    – ott--
    Jan 26, 2016 at 18:42
  • 1
    I fixed it already, ran out of RAM again.
    – JordyvD
    Jan 27, 2016 at 19:33

1 Answer 1

0

You have run out of RAM. Consider moving string literals into flash with thr F() macro.

1
  • 1
    We've done that before, we don't use any strings right now though.
    – JordyvD
    Jan 28, 2016 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.