Posts Tagged ‘Lua’

Ask Google for the time with an ESP8266

January 21, 2015

This is an example of how to connect to Google and retrieve just the HEAD information, which happens to include the current time.

It would be possible to do this with NTP, but that would require more coding. The results wouldn’t be any more accurate. I am pretty sure Google keeps the time on their servers well synchronized with the actual time.

— retrieve the current time from Google
— tested on NodeMCU 0.9.5 build 20150108

conn=net.createConnection(net.TCP, 0)

conn:on(“connection”,function(conn, payload)
conn:send(“HEAD / HTTP/1.1\r\n”..
“Host: google.com\r\n”..
“Accept: */*\r\n”..
“User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)”..
“\r\n\r\n”)
end)

conn:on(“receive”, function(conn, payload)
print(‘\nRetrieved in ‘..((tmr.now()-t)/1000)..’ milliseconds’)
print(‘Google says it is ‘..string.sub(payload,string.find(payload,”Date: “)
+6,string.find(payload,”Date: “)+35))
conn:close()
end)
t = tmr.now()
conn:connect(80,’google.com’)

The program can be saved to a file, google.lua. Then, dofile(‘google.lua’) will retrieve and print the time. From this location, the normal elapsed time from requesting to printing was under one tenth of a second. Pretty good.

google

Simple scripts like this are easy to enter and test with LuaLoader.

Advertisements

Using the ESP8266 as a power monitor

November 23, 2014

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.

A 3.3 volt wallwart is connected in parallel with the pump relay to power the esp8266 in a waterproof box. It couldn’t be any simpler.

Wallwart and ESP8266. That's all it takes!

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.

Inspired by Mark and Xavi’s How to use ESP8266 ESP-01 as a SENSOR web client post, I decided to use Lua for this quick job.

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


wifi.setmode(wifi.STATION)
wifi.sta.config('SSID','password')

Check with print(wifi.sta.getip()).

This is conveniently stored permanently in flash memory!

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


file.remove('benlo.lua')
file.open('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,'se.rv.er.ip') ]])
file.writeline([[conn:send("GET /yourscript.php HTTP/1.1\r\n") ]])
file.writeline([[conn:send("Host: yourserver.com\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 )]])
file.close()

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

The output from the webserver script gets sent to the UART if the computer is connected. Otherwise, it just blinks the blue LED on the board, giving visual feedback of successful operation.

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


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

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…

Links:
My ESP8266 Posts at benlo.com
Mark and Xavi’s Post on using Lua
NodeMCU Lua Developmen
ESP8266 Forum with Lua Threads