Post your best Lua script examples here

User avatar
By yes8s
#3581 Ok, so I think I'm starting to get the hang of this Lua language and the ESP8266. I've put together an example to (eventually) be able to control my AC over the net. In the future I want to extend the functionality to read temperature in my home and also sense and control some other things like lights etc.

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.

To do:
- 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.

Untitled.jpg
Attachments
(4.03 KiB) Downloaded 530 times
User avatar
By alonewolfx2
#3583 Thank you for your sharing. Safari, chrome or webkit on Android have different payload size and method. I think that's why it can't work webserver with all browsers. I am trying to figure out. But I am new on lua and lua string handling different and difficult. We wish to see i2c side on your code.
User avatar
By gerardwr
#3592
yes8s wrote: I've put together an example to (eventually) be able to control my AC over the net. In the future I want to extend the functionality to read temperature in my home and also sense and control some other things like lights etc.

Nice work, thanks for sharing. You elaborate post, deserves an elaborate reply ;)

I have the same interest. Currently I control wireless switches with Arduino+433Mhz-RF-transmitter, and have wireless analog/digital sensors using Arduino+433Mhz-RF-Receivers. The arduino code can also run (with minor modifications) on my Raspberry using the WiringPi library. Doing the same with an ESP would be a nice challenge.

yes8s wrote: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.

Thanks for "the flowers", nice to see that my example has inspired you. Your examples deserves a separate post.

yes8s wrote:- 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.

Yes, the updated version I published displays .html files too. In the "homepage" I list all files on the ESP as clickable links. When a link is clicked the requested hmtl page is displayed in the browser. I'll have at look at your code, maybe I can use some of it.

yes8s wrote:- 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.

I my latest version I made a start with handling Lua scripts embedded in a html file, analog to the use of PHP. If you're interested have a look here, could use some sparring the finish the code.
http://www.esp8266.com/viewtopic.php?f=19&t=611&start=40

yes8s wrote:- 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.

In my latest version I have some minor code that's executed when a .lua file is clicked. The click is recognized in the current code, but that's all. In a next version I intend to do a dofile("then_clicked_file") here. Does that make sense to you?

yes8s wrote: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 read post where a user tried to generate a pulse by setting GPIO to High, wait for 40? msecs, set GPIO to LOW. His experience was that the actual pulse was nowhere near the expected 40? msecs. Did not try this myself yet, but if his experience is true, generating a protocol to control a RF transmitter is out of the question.

Keep us posted on your progress, please.
User avatar
By gerardwr
#3670 Hi Hani,

I like the professional formatting of the code, I'm a "sloppy" programmer myself.

Some inspiring parts of your code for me, like better handling of multiple type requests.
Ultimately I would like to handle requests like
= GET /index.html
= GET /to_db.html?name=Joe&age=24

I see how you put "real world" data into the html. I have a prototype now where this html is possible:
Code: Select all<html>
<body>
<BR>
    Hello. This HTML page contains a lua script<BR>
<?lua
print("Lua line in HTML file excuted")
print("GPIO0="..gpio.read(8))
print("Chip="..node.chipid())
print("Heap="..node.heap())
?>
</body>
</html>


So you can embed Lua in html.

Development is in progress, 1st embedded Lua line in html is now executed OK. Some more coding for the subsequent nodes. Am struggling with the code to hand Lua strings to the interpreter and getting results back in a string. Come to the [Development} topic of the webserver if you can help.

Otherwise, have fun!