This example is based off of gerardwr's great example here: viewtopic.php?f=19&t=611. I was going to post this in that thread but I thought I'd start a seperate topic as I'd like to see gerards thread continue as an evolution of his version only without confusing things.
This example is not very "pretty" and is a little clunky but it does what I wanted it to do for now: Read form data and pass variables dynamically to a webpage.
Ok, so here a few things I've learned:
- Initially I served up the entire website in the function httpserver() by using a crap load of conn:send() commans. This used up huge amounts of the heap. I replaced this with code that reads the page dynamically from a stored html fileand this severly reduced the heap usage.
- String handling in Lua is quite powerful, maybe more so that many other languages, and with more research I think I can further reduce memory by utilizing this power.
- Find a better way of detecting valid HTML requests including better differention between GET and POST methods. (i.e. Return 404 errors in the event of bad requests)
- Find a better more dynamic way of passing variables to and from Lua and the webpage. Currently I'm using a number of string.gsub comands and arbitrability replacing #xxxx text with data from a lua table.
- Make things more portable and abstract such that the webpage defines what's needed of the lua code so I can reuse lua server files without having to change them much across projects.
- POST request handling doesn't work for all browsers at the moment. (Firefox + Chrome are ok but iPhone is not working).
- Actually control the AC. My AC is controlled by an IR remote. I've already reverse engineered the protocal from this remote previously for another project I've completed so I've got another micro that can drive an IR LED and control the AC. I want to first see if I can port that protocol to the ESP but I'm not sure if Lua can produce the accurate microsecond timing the protocol requires - I need to play around with the timer module. If that doesn't work then I'll just pass simple commands via I2C (or uart) to this other small micro to handle the IR side of things.
Any feedback or suggestions will be appreciated.