ESP8266 is a WiFi module that can be programmed to serve web pages. The capability is similar to a typical WiFi router serving its own administrative web pages. We can hack a WiFi router with OpenWRT or we can just program ESP8266.
The server-side example codes can be found at the micro-api framework repo. Here we are going to prepare for the client-side development.
ESP8266 is a general purpose WiFi module where the web pages can be more than just administrative. However, due to the limited resources of ESP8266 the processes should be mostly pushed to the client side. See libesphttpd and Micro API.
Our web server will be based on libesphttpd. It comes with a tiny read-only file system (espfs) and a utility to pack the entire web file structure into a single blob file which is to be flashed into ESP8266. The web server will serve the files just like any other web server.
We need to set up a localhost web server and start developing for the client side. We can use any web framework. We can use Javascript, CSS, images, etc. They will all run at the client side. However, we need to observe the total file size so that they will fit into ESP8266. And we need to minimize the processes on the server so that to lessen the burden of the ESP8266 module. Make use of the micro API framework.
Do develop and test as much as possible on localhost. Once we are satisfied with the web pages than we can pack them into a blob and flash it into ESP8266. Then we can continue with further development, integration and testing.
mkespfsimage is the utility to pack the entire web file structure into a blob file. It is a bit primitive. It makes use of the find command to list all the files in a given folder. And then pipes the list to the utility which will print out the blob into stdout. We need to redirect the output into a file.
$ find | mkespfsimage > webpages.espfs
mkespfsimage can be found within our libesphttpd installation at
libesphttpd/espfs/mkespfsimage/mkespfsimage.
Make sure that the find command is executed from within our web folder. We can have something like the following in our makefile:
webpages: $(WEBDIR)/webpages.espfs
$(WEBDIR)/webpages.espfs:
cd $(WEBDIR); \
find ! -name 'webpages.espfs' | $(ESPFS)/mkespfsimage > webpages.espfs; \
cd -
Notice that we can tell find not to include the blob file webpages.espfs. The command line 'cd -' means to go back to the previous folder.
To make the web pages blob file:
$ make webpages
The webpages.espfs file will be created in the web folder.
We need to be prepared with the address to flash the web pages blob into. It depends on our ESP8266 flash size and our code size. We don't want to overlap our codes with the blob and causes unpredictable run time errors. And the address for the blob must start at a sector of 4 kB, above the codes. See the notes on ESP8266's flash.
ESPFS_ADDR = 0x200000
ESPFS_SIZE = 0x100000
To flash the web pages blob file:
$ make webpages_flash
We are going to extend the micro api sample code so that it will serve our web pages. Add a line with cgiEspFsHook:
static HttpdBuiltInUrl builtInUrls[] = {
{"/api.cgi", cgiApi, NULL},
{"*", cgiEspFsHook, NULL},
{NULL, NULL, NULL}
};
And initialize the file system:
#define ESPFS_POS 0x200000
void ICACHE_FLASH_ATTR user_init()
{
...
captdnsInit();
// 0x40200000 is the base address for spi flash memory mapping, ESPFS_POS is the position
// where image is written in flash that is defined in Makefile.
espFsInit((void*)(0x40200000 + ESPFS_POS));
httpdInit(builtInUrls, 80);
...
The web files blob will be compressed with heatshrink. It is the default behavior. However, the heatshrink decoder's codes are not wrapped with ICACHE_FLASH_ATTR. Hence, it will use up the iRAM segment and squeeze our code. We may need to rebuild libesphttpd to disable heatshrink by setting USE_HEATSHRINK to no in the makefile.
The server-side example codes can be found at the micro-api framework repo. Here we are going to prepare for the client-side development.
ESP8266 is a general purpose WiFi module where the web pages can be more than just administrative. However, due to the limited resources of ESP8266 the processes should be mostly pushed to the client side. See libesphttpd and Micro API.
Localhost
Our web server will be based on libesphttpd. It comes with a tiny read-only file system (espfs) and a utility to pack the entire web file structure into a single blob file which is to be flashed into ESP8266. The web server will serve the files just like any other web server.
We need to set up a localhost web server and start developing for the client side. We can use any web framework. We can use Javascript, CSS, images, etc. They will all run at the client side. However, we need to observe the total file size so that they will fit into ESP8266. And we need to minimize the processes on the server so that to lessen the burden of the ESP8266 module. Make use of the micro API framework.
Do develop and test as much as possible on localhost. Once we are satisfied with the web pages than we can pack them into a blob and flash it into ESP8266. Then we can continue with further development, integration and testing.
Web Pages Blob
mkespfsimage is the utility to pack the entire web file structure into a blob file. It is a bit primitive. It makes use of the find command to list all the files in a given folder. And then pipes the list to the utility which will print out the blob into stdout. We need to redirect the output into a file.
$ find | mkespfsimage > webpages.espfs
mkespfsimage can be found within our libesphttpd installation at
libesphttpd/espfs/mkespfsimage/mkespfsimage.
Make sure that the find command is executed from within our web folder. We can have something like the following in our makefile:
webpages: $(WEBDIR)/webpages.espfs
$(WEBDIR)/webpages.espfs:
cd $(WEBDIR); \
find ! -name 'webpages.espfs' | $(ESPFS)/mkespfsimage > webpages.espfs; \
cd -
Notice that we can tell find not to include the blob file webpages.espfs. The command line 'cd -' means to go back to the previous folder.
To make the web pages blob file:
$ make webpages
The webpages.espfs file will be created in the web folder.
Flashing Web Pages Blob
We need to be prepared with the address to flash the web pages blob into. It depends on our ESP8266 flash size and our code size. We don't want to overlap our codes with the blob and causes unpredictable run time errors. And the address for the blob must start at a sector of 4 kB, above the codes. See the notes on ESP8266's flash.
ESPFS_ADDR = 0x200000
ESPFS_SIZE = 0x100000
webpages_flash: $(WEBDIR)/webpages.espfs
if [ $$(stat -c '%s' $(WEBDIR)/webpages.espfs) -gt $$(( $(ESPFS_SIZE) )) ]; then echo "webpages.espfs too big!"; false; fi
esptool.py write_flash $(ESPFS_ADDR) $(WEBDIR)/webpages.espfs
In the makefile sample above it will check the size of the blob before flashing it into ESP8266.
To flash the web pages blob file:
$ make webpages_flash
Serving Web Pages
We are going to extend the micro api sample code so that it will serve our web pages. Add a line with cgiEspFsHook:
static HttpdBuiltInUrl builtInUrls[] = {
{"/api.cgi", cgiApi, NULL},
{"*", cgiEspFsHook, NULL},
{NULL, NULL, NULL}
};
And initialize the file system:
#define ESPFS_POS 0x200000
{
...
captdnsInit();
// 0x40200000 is the base address for spi flash memory mapping, ESPFS_POS is the position
// where image is written in flash that is defined in Makefile.
espFsInit((void*)(0x40200000 + ESPFS_POS));
httpdInit(builtInUrls, 80);
}
Heatshrink
The web files blob will be compressed with heatshrink. It is the default behavior. However, the heatshrink decoder's codes are not wrapped with ICACHE_FLASH_ATTR. Hence, it will use up the iRAM segment and squeeze our code. We may need to rebuild libesphttpd to disable heatshrink by setting USE_HEATSHRINK to no in the makefile.
Comments
Post a Comment