ESP-IDF SPIFFS can't find files on ESP32?

mikemoy
Posts: 627
Joined: Fri Jan 12, 2018 9:10 pm

Re: ESP-IDF SPIFFS can't find files on ESP32?

Postby mikemoy » Wed Sep 26, 2018 5:17 pm

I would assume you have the following register handlers in your code (or similar)

Code: Select all

        httpd_register_uri_handler(server, &index_html);
        httpd_register_uri_handler(server, &favicon_ico);
        httpd_register_uri_handler(server, &on_off_switch_js);
        httpd_register_uri_handler(server, &on_off_switch_onload_js);
        httpd_register_uri_handler(server, &on_off_switch_css);
The issue I am now running into is that these will fire at will, even before one is finished. thus causing a heap corruption. Have you gotten this yet?

Also, just in case you didn't catch this you need to move / add this to the bottom of your readfile().
free(buf);
return((char*)buf);

Code: Select all

char *readFile(char *fname)
{
    int size, res;
    char *buf;
    struct stat st;

    if (stat(fname, &st) == 0) {
        size = st.st_size;
    } else {
        ESP_LOGE(TAG, " Error - cannot find file!");
        return NULL;
    }

    ESP_LOGI(TAG, " Opening file %s, size: %d", fname, size);

    FILE *fd = fopen(fname, "rb");
    if (fd == NULL) {
        ESP_LOGE(TAG, " Error opening file");
        return NULL;
    }
   
    if (size == 0) {
        ESP_LOGE(TAG, " Error - file size zero");
        return NULL;
    }

    buf = calloc(size, 1); // File buffer
    if (buf == NULL) {
        ESP_LOGE(TAG, " Error allocating read buffer");
        free(buf);
        fclose(fd);
        return NULL;
    }

    res = size;
    res = fread(buf, 1, size, fd);
    if (res <= 0) {
        ESP_LOGE(TAG, " Error reading from file");
    }
    else {
        ESP_LOGI(TAG, " %d bytes read", res);
        buf[res] = '\0';
    }

    

    res = fclose(fd);
    if (res) {
        ESP_LOGE(TAG, " Error closing file");
    }

    free(buf);
    return((char*)buf);

} 

nockieboy
Posts: 13
Joined: Fri Sep 21, 2018 1:21 pm

Re: ESP-IDF SPIFFS can't find files on ESP32?

Postby nockieboy » Wed Sep 26, 2018 5:56 pm

Yes, I'd spotted that and corrected for it. Here's a little more detail on the error:

Code: Select all

I (2227) APP: Server ready...
I (312675) APP:  Opening file /spiffs/index.htm, size: 928
I (312676) APP:  928 bytes read
CORRUPT HEAP: multi_heap.c:429 detected at 0x70732f3c
abort() was called at PC 0x4008e04f on core 1
0x4008e04f: multi_heap_assert at C:/msys32/home/user/esp-idf/components/heap/multi_heap.c:377
 (inlined by) multi_heap_malloc_impl at C:/msys32/home/user/esp-idf/components/heap/multi_heap.c:429


Backtrace: 0x4008e6e8:0x3ffbe270 0x4008e8bd:0x3ffbe290 0x4008e04f:0x3ffbe2b0 0x40082805:0x3ffbe2d0 0x40082831:0x3ffbe2f0 0x40082e3d:0x3ffbe310 0x4000beaf:0x
3ffbe330 0x40131d24:0x3ffbe350 0x4012c657:0x3ffbe370 0x40129d30:0x3ffbe390 0x4012a24d:0x3ffbe3b0 0x40127e9e:0x3ffbe420 0x40128d0d:0x3ffbe440 0x40129159:0x3f
fbe460 0x4008c62d:0x3ffbe490
0x4008e6e8: invoke_abort at C:/msys32/home/user/esp-idf/components/esp32/panic.c:660

0x4008e8bd: abort at C:/msys32/home/user/esp-idf/components/esp32/panic.c:660

0x4008e04f: multi_heap_assert at C:/msys32/home/user/esp-idf/components/heap/multi_heap.c:377
 (inlined by) multi_heap_malloc_impl at C:/msys32/home/user/esp-idf/components/heap/multi_heap.c:429

0x40082805: heap_caps_malloc at C:/msys32/home/user/esp-idf/components/heap/heap_caps.c:130

0x40082831: heap_caps_malloc_default at C:/msys32/home/user/esp-idf/components/heap/heap_caps.c:130

0x40082e3d: _malloc_r at C:/msys32/home/user/esp-idf/components/newlib/syscalls.c:37

0x40131d24: mem_malloc at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/core/mem.c:124

0x4012c657: pbuf_alloc at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/core/pbuf.c:1331

0x40129d30: tcp_pbuf_prealloc at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/core/tcp_out.c:268

0x4012a24d: tcp_write at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/core/tcp_out.c:572

0x40127e9e: lwip_netconn_do_writemore at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/api/api_msg.c:1427 (discriminator 6)

0x40128d0d: lwip_netconn_do_write at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/api/api_msg.c:1819

0x40129159: tcpip_thread at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/api/tcpip.c:483

0x4008c62d: vPortTaskWrapper at C:/msys32/home/user/esp-idf/components/freertos/port.c:401


CPU halted.
Here's my code:

Code: Select all

char *readFile(char *fname)
{
    int size, res;
    char *buf;
    struct stat st;

    if (stat(fname, &st) == 0) {
        size = st.st_size;
    } else {
        ESP_LOGE(TAG, " Error - cannot find file!");
        return NULL;
    }

    ESP_LOGI(TAG, " Opening file %s, size: %d", fname, size);

    FILE *fd = fopen(fname, "rb");
    if (fd == NULL) {
        ESP_LOGE(TAG, " Error opening file");
        return NULL;
    }
    
    if (size == 0) {
        ESP_LOGE(TAG, " Error - file size zero");
        return NULL;
    }

    buf = calloc(size, 1); // File buffer
    if (buf == NULL) {
        ESP_LOGE(TAG, " Error allocating read buffer");
        free(buf);
        fclose(fd);
        return NULL;
    }

    res = size;
    res = fread(buf, 1, size, fd);
    if (res <= 0) {
        ESP_LOGE(TAG, " Error reading from file");
    }
    else {
        ESP_LOGI(TAG, " %d bytes read", res);
        buf[res] = '\0';
    }

    res = fclose(fd);
    if (res) {
        ESP_LOGE(TAG, " Error closing file");
    }

    free(buf);
    return((char*)buf);
}

esp_err_t index_get_handler(httpd_req_t *req)
{
    /* Send response with custom headers and body set as the
     * string passed in user context*/
    // Get the file to return
    char* resp_str = (char*) readFile("/spiffs/index.htm");
    // Set the response headers
    httpd_resp_set_hdr(req, "status", "200");
    httpd_resp_set_hdr(req, "content-type", "text/html; charset=UTF-8");
    httpd_resp_set_hdr(req, "server", "ESP32-10103");
    // Return the response
    httpd_resp_send(req, resp_str, strlen(resp_str));
    free(resp_str);
    return ESP_OK;
}
No errors during assembly. I'd say it's definitely the read routine that's causing the problem - almost certainly because memory space isn't getting freed from unused variables or something to do with the intricacies of mixing pointers and other var types, but as I said earlier, I'm brand-new to C and learning it on the fly, so I have no idea what the problem is or how to fix it. :oops:

mikemoy
Posts: 627
Joined: Fri Jan 12, 2018 9:10 pm

Re: ESP-IDF SPIFFS can't find files on ESP32?

Postby mikemoy » Wed Sep 26, 2018 8:07 pm

I got it working sending large files without crashing. If your still stuck let me know and I send what I have.

nockieboy
Posts: 13
Joined: Fri Sep 21, 2018 1:21 pm

Re: ESP-IDF SPIFFS can't find files on ESP32?

Postby nockieboy » Thu Sep 27, 2018 8:17 am

Yes please! Still stuck with this mysterious corrupted heap problem. :?

EDIT: Actually, it seems I've sorted the heap corruption. I changed:

Code: Select all

buf = calloc(size, 1); // File buffer
to

Code: Select all

buf = (char*) calloc(size, sizeof(char*)); // File buffer
in the readFile() function and the errors have gone away. I'm just having trouble getting a .JS file to the browser now. Think there's an issue with how I'm handling the request or something:

Code: Select all

W (17298) httpd: httpd_accept_conn: error in accept (23)
W (17298) httpd: httpd_server: error accepting new connection

mikemoy
Posts: 627
Joined: Fri Jan 12, 2018 9:10 pm

Re: ESP-IDF SPIFFS can't find files on ESP32?

Postby mikemoy » Thu Sep 27, 2018 12:44 pm

I wound up using to include the binary web site files in my project instead. IMHO it was a better solution. But I did keep the way we were doing it for reference just in case.

Here is what I had in the end.

Code: Select all

int readFile(char *fname, httpd_req_t *req)
{
    int res;
    char buf[1024];

    FILE *fd = fopen(fname, "rb");
    if (fd == NULL) 
    {
        printf("Error opening file (%d) %s\n", errno, strerror(errno));
        httpd_resp_send_404(req);
        return 0;
    }
    
    do
    {
        res = fread(buf, 1, sizeof(buf), fd);
        if (res > 0) 
        {
            httpd_resp_send_chunk(req, buf, res);    
            printf("Read %d\n", res);
        }

    }while(res >0);

    httpd_resp_send_chunk(req, NULL, 0);

    res = fclose(fd);
    if (res) 
    {
        printf("Error closing file\n");
    }

    return 1;

}


/* An HTTP GET handler */
esp_err_t index_html_handler(httpd_req_t *req)
{
    printf("Main Page Requested\r\n");

    httpd_resp_set_type(req,"text/html");

    readFile("/spiffs/index.html", req);

    return ESP_OK;
}

Thats weird about the .js file. I am serving up 8 files with no issues using this method. .js & .css as well.
One thing I used to help using firefox pressing F12 before I load the site, and clicking on the network tab to check whats going on.

nockieboy
Posts: 13
Joined: Fri Sep 21, 2018 1:21 pm

Re: ESP-IDF SPIFFS can't find files on ESP32?

Postby nockieboy » Thu Sep 27, 2018 3:42 pm

Yes, I've been using the debugger to see exactly what's going on. However, I've been using the Opera browser - I switched to Firefox and the problem seems have disappeared... bizarre! :)

mikemoy
Posts: 627
Joined: Fri Jan 12, 2018 9:10 pm

Re: ESP-IDF SPIFFS can't find files on ESP32?

Postby mikemoy » Thu Sep 27, 2018 3:48 pm

Opera has a really good debugger. I just starting using it and I like it better.

nockieboy
Posts: 13
Joined: Fri Sep 21, 2018 1:21 pm

Re: ESP-IDF SPIFFS can't find files on ESP32?

Postby nockieboy » Thu Sep 27, 2018 3:53 pm

Yep, it's been my weapon of choice over Chrome and FF for a year or two, during some heavy web-dev work. :D

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: ESP-IDF SPIFFS can't find files on ESP32?

Postby fly135 » Mon Dec 10, 2018 9:45 pm

Did you guys ever figure out how to write a file system to the flash where you could read the files? I just tired this and couldn't get it to work. I started another thread with more details.

viewtopic.php?f=2&t=8387

Who is online

Users browsing this forum: No registered users and 129 guests