ESP8266 Webserver Project

Moderator: Sprite_tm

User avatar
By Sprite_tm
#3869
kisesp wrote:So probably the esp can't scan if in softap mode.Two options remaining 1.scan before start the server (at user main) and list that on scan.cgi 2. when scancgi start switch to sta mode then scan and switch back and return the results.
I think the 1. option the good choice because if somebody want to use always in softap mode no reason to check the AP-s list, if need the sta mode it's need only once, when do config-s.

Well, scanning actually seems to work when in AP+STA mode. I'm thinking of switching to that by default. It has some other interesting issues tho'.

kisesp wrote:How variable parse work? I not really understand %counter% and cgi callback.
I want to add variable parse inside html like %led1% but in any html, I think something global struct variable with name and options, then if found %var% then search at struct and replace with value, or trigger a function to execute and return the value.
Thanks.


Not gonna work. You actually need to connect the .tpl-file to a function that 'fills in the blanks'. For eg the led page (the bit that shows if the led is on or off; the action of turning it on or off is handled by a cgi) the hooking up is done in user-main.c:
Code: Select all{"/led.tpl", cgiEspFsTemplate, tplLed},


The tplLed function then fills in the %ledstate% in the led.tpl file:
Code: Select all//Template code for the led page.
void ICACHE_FLASH_ATTR tplLed(HttpdConnData *connData, char *token, void **arg) {
   char buff[128];
   if (token==NULL) return;

   os_strcpy(buff, "Unknown");
   if (os_strcmp(token, "ledstate")==0) {
      if (currLedState) {
         os_strcpy(buff, "on");
      } else {
         os_strcpy(buff, "off");
      }
   }
   espconn_sent(connData->conn, (uint8 *)buff, os_strlen(buff));
}


So basically, if you want to use the same %something% thing in multiple pages, you'll have to repeat the hooking up line of code in user-main.c multiple times, but you can use the same function to do the replacement.
User avatar
By kisesp
#3893 Ok, thanks for information finally understand why no results for scan, because the modules are shipped with default settings for AP mode, if I upload the empty bin to 0x7E000 stil AP mode.
The fix: you have to pull GPIO0 to ground for reset, that set mode 3(AP+STA) or before flash you have to set AP+STA with at commands and not use the empty bin.

So basically, if you want to use the same %something% thing in multiple pages, you'll have to repeat the hooking up line of code in user-main.c multiple times, but you can use the same function to do the replacement.

It's ok for 1 variable and 1 page or same variable and multiple page, but how to use multiple vars on single page?

Thanks.
User avatar
By Sprite_tm
#3899
kisesp wrote:It's ok for 1 variable and 1 page or same variable and multiple page, but how to use multiple vars on single page?

Thanks.


The function gets passed the thing between %-signs as the 'token'-argument; that's why I do the strcmp() with 'ledstate'. If you have a second variable, you can add a separate strcmp on that and if it matches, replace it with whatever else you want.
User avatar
By kisesp
#3900
Code: Select allThe function gets passed the thing between %-signs as the 'token'-argument; that's why I do the strcmp() with 'ledstate'. If you have a second variable, you can add a separate strcmp on that and if it matches, replace it with whatever else you want.

Now got it, but stil not so "universal" if I have a lot of same variables on different pages.
I want something much universal to let use variables everywhere, but I don't want to bother you so I will try to modify to my needs.

Meanwhile small bug found at cgiEspFsTemplate()
If I put a single % without closing it then after % nothing displaying on the page.
Here is the fix (I hope it's fine, it's working for me but I'm not 100% sure)
Code: Select all if (tpd->tokenPos<(sizeof(tpd->token)-1)) tpd->token[tpd->tokenPos++]=buff[x];
+else {
+  sp=sizeof(tpd->token);
+  e=&buff[x-sp];
+  tpd->tokenPos=-1;
+}