Post your best Lua script examples here

User avatar
By GeoNomad
#3194 I needed a simple way to monitor the pump on our well.

Once in a while, something goes wrong and it fails to run. We don't notice until the holding tank empties and there is no water. Then we have to go figure out what happened - breaker problem, switch problem, power bill not paid, etc...

Other times, like last month, the pump remains on 24/7 and the water overflows the tank until someone notices. It took 35 days at 2KW before anyone noticed. $400 electric bill!!!!! The well and tank are not visible or even easily accessible from the house.

Seems like a good application for the esp8266.

When power is on, it pings a php script on my webserver. A simple program that logs the times and reports by email if the pump hasn't run for more than 24 hours, or if it has run for more than 2 hours straight. Normal operation is an hour a day.

On the esp8266 set up the AP SSID and password as usual and run benlo.lua every 30 seconds when the power is on.

Setup for feeding over the serial terminal (is there a better way?)

Code: Select allfile.remove('benlo.lua')'benlo.lua','w')
file.writeline([[conn=net.createConnection(net.TCP, 0) ]])
file.writeline([[conn:on("receive", function(conn, payload) print(payload) end) ]])
file.writeline([[conn:connect(80,'') ]])
file.writeline([[conn:send("GET /yourscript.php HTTP/1.1\r\n") ]])
file.writeline([[conn:send("Host:\r\n") ]])
file.writeline([[conn:send("Accept: */*\r\n") ]])
file.writeline([[conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n") ]])
file.writeline([[conn:send("\r\n") ]])
file.writeline([[tmr.alarm(30000, 0, function() dofile('benlo.lua') end )]])

As you can see, this script just connects to the web server once every 30 seconds when the power is present.

To start on power up, init.lua waits 10 seconds for the connection to the WiFi AP and starts benlo.lua

Code: Select allfile.remove('init.lua')'init.lua','w')
file.writeline([[print('Peter LUA module 0.1')]])
file.writeline([[tmr.alarm(10000, 0, function() dofile('benlo.lua') end )]])

I suppose, benlo.lua could just be incorporated into init.lua, but I like the idea of keeping it separate.

Just what I needed.

Thanks and kudos to the developers who put up the Lua code, it was pretty easy to craft this in an afternoon without knowing anything about Lua or the esp8266.

Feedback on dumb things I am doing would be useful. Just starting out...

User avatar
By l0ur3nz0
#3280 Just to be clear: your not sensing anything, right? You're using the same power/relay that starts the pump to power the ESP and then you know the pump is running - is that it?

Clever. But, you could easilly add some sort of sensing to get statistic and realtime monitoring.
User avatar
By GeoNomad
#3311 Yes, you are right. All I am doing is detecting that the power is on because when it is on, the module connects to the server.

Obviously, I could add lots of things, but the project wouldn't have been finished in one afternoon.