Skip to main content
Hoang Phuc
Associate
May 21, 2017
Question

STM32F746 Webserver files loaded from SD card

  • May 21, 2017
  • 13 replies
  • 4375 views
Posted on May 21, 2017 at 13:36

Hello, 

I want to create a webserver application, and I want my html file modified seperately. So I think that saving my web files (including .html, .css and .js files) into SD card I a good solution, So whenever I want to modify my website, I just need to edit web files and put them into SD card.

I read a lot about lwIP and FatFs package but I still dont know how to figure it out.

Can any one expert on this field give me an example project like that? any IDE project is OK. Or I just need to be guided to configure the STM32cubeMX, and create a simple webserver saved on SD card.

Thank you very much

#fatfs #sd-card #web-server #lwip
This topic has been closed for replies.

13 replies

Haithem Rahmani
ST Employee
May 24, 2017
Posted on May 24, 2017 at 16:39

Hi Hoang,

the STM32Cube provides a http server application based on the LwIP API.Have a look at the project:

Projects\STM32756G_EVAL\Applications\LwIP\LwIP_HTTP_Server_Socket_RTOS

the LwIP comes with a builtin http server, the server is expecting the html pages in the file 'fsdata_custom.c' when the lwip config flag HTTPD_USE_CUSTOM_FSDATA is defined.

for the aforementioned project, the actual html pages and data are under 'Projects\STM32746G-Discovery\Applications\LwIP\LwIP_HTTP_Server_Netconn_RTOS\Fs'

you can use any binary dump tool,

https://kent.dl.sourceforge.net/project/bin2header/current/bin2header_0.0.1_portable-win32.zip

for example, to generate your own  'fsdata_custom.c' from your webpages.

Could you please try and let us know whether it suits your needs?

regards

Haithem.

Hoang Phuc
Associate
May 24, 2017
Posted on May 24, 2017 at 17:49

Hi Haithem, thanks for replying me, that's very helpful, I will try this example.

But I have two question:

Is there any way that I can send my web directly to the client, without converting it into the fsdata_custom file?

I read about TFTP, can I use this interface to do some thing like on-the-air update for my web server?. For example I just need to modified my web files (including css,js or cgi files) on my PC and send them to the kit via TFTP interface(with or without converting to fsdata file is all OK), after that the web server will display with the new updates.

If any of my questions is possible, could you show me a brief guide to do it?

Many thanks no matter if you could or couldn't help me this

andrea formentin
Visitor II
May 30, 2017
Posted on May 30, 2017 at 14:35

Hi Hoang

I'm working on a small project that show logs file using lwip. It's possible to read custtom pages/files from sdcard by using

LWIP_HTTPD_CUSTOM_FILES

LWIP_HTTPD_DYNAMIC_FILE_READ

variables. Then you need to implements 3 custom methods :

int fs_open_custom(struct fs_file *file, const char *name);

void  fs_close_custom(struct fs_file *file);

int fs_read_custom(struct fs_file *file, char *buffer, int count);

I'm still working on it, but this is the starting point if you want to use lwip+sd card without using freertos

Andrea

Hoang Phuc
Associate
June 1, 2017
Posted on June 01, 2017 at 09:02

Thank andrea,

That's very helpful. I will try your suggestion.

Have a good day

andrea formentin
Visitor II
June 5, 2017
Posted on June 05, 2017 at 19:44

here is my starting code:

//open custom file

int fs_open_custom(struct fs_file *file, const char *name) {

    //open log file

    if(strncmp_url(name,'/log_file.txt',13)>0)

    {

        file->data = 'buffer_open_custom';    

        file->len = 1000;

        file->index = 0;

        file->pextension = NULL;

        file->is_custom_file = 1;

    

    //0 if unable to open

    return 1;

    }

}

//read file and fill 'buffer' step by step by 'count' bytes amount

int fs_read_custom(struct fs_file *file, char *buffer, int count)  {

    

    int read = 0;    

    if (file->index < file->len)

    {

        read = 100;

        file->is_custom_file = 1;

        buffer = 'buffer_read_custom';

    }

    else

    {

        //end reached

        read = FS_READ_EOF;

    }

    return read;

}

void  fs_close_custom(struct fs_file *file){

    //close file

    //f_close(&fileOpen);

}

fs_open_custom works ok and the first part of the page is correctly loaded, then fs_read_custom is called 10 times before return FS_READ_EOF but only the first call appends to the buffer 'buffer_read_custom' string, next calls append only part of 'fsdata_custom.c' file. Any idea about this behavior? This is only a proof of concept example. I just need to test this code works correctly

LMI2
Senior III
June 5, 2017
Posted on June 05, 2017 at 22:18

Is it so that there is no example project or code for the SD card in the 746 Discovery? Even though there SD drive included. Probably many of us could one, me included.

andrea formentin
Visitor II
June 5, 2017
Posted on June 05, 2017 at 22:24

read/write from to the sd card is quite simple. My problem is to stream huge custom log files stored on usb to port 80 by using lwip. I need to solve issue with buffer of fs_read_custom method...

Tomas Spacil
Associate
August 30, 2017
Posted on August 30, 2017 at 14:06

Hello.

I have some new experience on this topic.

I implemented 3 custom methods like this:

int fs_open_custom(struct fs_file *file, const char *name)

{

    FRESULT fres;

    char buffer[100];

    sprintf((char *)buffer, 'www%s', name);

    fres = f_open(&Msc.File[0], &buffer[0], FA_OPEN_EXISTING | FA_READ);

    if(fres != FR_OK)

    {

        return 0;

    }

    file->data = NULL;

    file->len = f_size(&Msc.File[0]);

    file->index = 0;

    file->pextension = NULL;

    return 1;

}

void fs_close_custom(struct fs_file *file)

{

    f_close(&Msc.File[0]);

}

int fs_read_custom(struct fs_file *file, char *buffer, int count)

{

    uint16_t DLen = 0;

    if(f_eof(&Msc.File[0]))

    {

        return(FS_READ_EOF);

    }

    if(file->index < file->len)

    {

        f_read(&Msc.File[0], buffer, count, (void *)&DLen);

        return(DLen);

    }

    else

    {

        DLen = FS_READ_EOF;

    }

    return(DLen);

}

I built a project with HAL and cubeMX with automatic configuration of middlewares like LWIP, USB MSC and Fatfs.

It works quete well. Sometimes it happens that styles are not included in pages. I have separete file style.css.

It works fine when I put styles directly into the pages. Maybe, there is some error occur during multiple file calling.

It seems like some data are lost during calling and styles are not presented correctly.

But the bigger problem is in 'shtml' files. Pages with this suffix are displayed for too long. It takes 20 - 30 sec.

When I only change the suffix from 'shtml' to 'html' it works OK but SSI tags are ignored which can be expected.

When I use a method of fsdata.c file 'shtml' pages are displayed correcly without any issues.

Do you have any opinions about 'shtml' files (SSI) behaviour in conjuction with Fatfs and fs_custom methods?

Thank you for your advice.

Tom.

Mark Shoe
Associate III
February 24, 2018
Posted on February 24, 2018 at 16:53

Hi Thomas, Thanks for you custom functions, they do work however i see sometimes more than one file are opened by the browser at the same time. You used &Msc.File[0]  for fs_file. The should be something other than 0 there. The TM code uses &fil[file->id] but my fs_file has no id field. 

This way with multiple files the reader does not know which file to read. How to seperate the files?

Mark Shoe
Associate III
March 15, 2018
Posted on March 15, 2018 at 09:49

Did someone managed to run a http server from the SD card? Please share your lwip.c