What's wrong with my httpd code?

eben80
Posts: 1
Joined: Thu Oct 15, 2020 9:16 am

What's wrong with my httpd code?

Postby eben80 » Thu Oct 15, 2020 12:05 pm

Hi all,
I'm trying to serve html from my SPIFSS but I only get a blank page.
When I print the char buffer to Serial it looks like the complete html.

What is wrong with my code? Can anyone help please?

Code: Select all

char file_buf[24000];

static esp_err_t index_handler(httpd_req_t *req)
{
    httpd_resp_set_type(req, "text/html");

    unsigned int file_size = 0;

    File file = SPIFFS.open("/index.html");

    if (!file)
    {
        Serial.println("Failed to open file for reading");
        return ESP_FAIL;
    }
    else
    {
        file_size = file.size();
        Serial.print("File Size:  ");
        Serial.println(file_size);

        uint16_t i = 0;
        while (file.available())
        {
            file_buf[i] = file.read();
            
            i++;
        }
        file_buf[i] = '\0';
        Serial.print("Size of char buffer:  ");
        Serial.println(sizeof(file_buf));
        Serial.print(file_buf); //use for debug
    }

    httpd_resp_set_type(req, "text/html");
    httpd_resp_send(req, file_buf, -1);
    file.close();
    return ESP_OK;
This is the serial output:

Code: Select all

WiFi connected, your IP address is
192.168.43.94
SPIFFS Mounted
Starting web server on port: '80'
Starting stream server on stream port: '9601'
Camera Ready! Use 'http://192.168.43.94' to connect
stream Ready! Use 'http://192.168.43.94:9601/stream
File Size:  23586
Size of char buffer:  24000
<!doctype html>
<html>
......Truncated...
</html>
The browser window stays blank and doesn't finish loading.

Who is online

Users browsing this forum: No registered users and 93 guests