-->
Page 1 of 5

Serving HTML pages.

PostPosted: Thu May 14, 2015 6:04 pm
by atrus
I've seen the example webserver, but all the returned html/text has been hardcoded. Is there any way to serve separate webpages?

Re: Serving HTML pages.

PostPosted: Thu May 14, 2015 7:21 pm
by Dave S
You should check out the forum post http://www.esp8266.com/viewtopic.php?f=34&t=376. The ESP-HTTPD Project supports serving static pages. You will need to use the SDK.

But IMHO, it is better to use a separate server for webpages, the micro-controller is just too limited in resources to serve up complex or large pages.

That is what hosting packages are for. Very inexpensive. And if you don't need a personalized domain name, free options are available. You would need to reference the pages' images/media residing on a separate server anyway as you would soon run out of flash memory to store the page's dependent files if you try do serve up everything on an ESP8266.

Good luck!

Re: Serving HTML pages.

PostPosted: Thu May 14, 2015 11:47 pm
by atrus
I'm not trying to serve excessively large or complex pages, just trying to keep the stuff the webserver serves separate from the webserver itself.

Re: Serving HTML pages.

PostPosted: Fri May 15, 2015 5:30 pm
by Dave S
You should be able to serve up a page using lua NodeMCU something like (listening on port 8266):
Code: Select allsrv=net.createServer(net.TCP)
srv:listen(8266,function(conn)
  conn:on("receive", function(client,request)
   .
   .
   .
  file.open("mypage.htm","r")
  while true do       
    str = file.readline()
   if str == nil then break end
   client:send(buf);
  end
  file.close()
  end)
end)


Create your html file (mypage.htm) and load it to the ESP8266 the same way init.lua is loaded.

I am not a big fan of NodeMCU since it does not leave much memory for your code and it leaks memory like a sieve.

Hope this gives you an idea how to do it. Good luck with your project.