Post your best Lua script examples here

User avatar
By Pol
#77472 Hi,


I have following code successfully running on my ESP8266, nodemcu-1.5.4.1 firmware and I can open my web service on Android ( Chrome, Firefox), and Windows (IE, Chrome) but it will never open on IOS device (Safari,Chrome)

Where should I insert the commends to get IOS browser *Safari, Chrome) to display a webpage ?

client:send("HTTP/1.1 200 OK\r\n");
client:send("Content-type: text/html\r\n");
client:send("Connection: close\r\n\r\n");



Following code runs well allowing to trigger relay connection:


-- must switch to 9600 baud to control relay
uart.setup(0,9600,8,0,1)

-- join wifi
wifi.setmode(wifi.SOFTAP)

local cfg

cfg = {
ip = "10.10.10.10",
netmask = "255.255.255.0",
gateway = "10.10.10.10"
}
wifi.ap.setip(cfg)

cfg = {
ssid = "XXXX",
pwd = "xxxx"
}
wifi.ap.config(cfg)


-- Lua web server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)

local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = buf.."<head><meta name='viewport' content='width=200px, initial-scale=1.0'/>"
buf = buf.."<style>body { background-color: #606060;} h2{border: 4px solid #000; border-radius: 23px; padding: 20px; font-size: 80px;} h1{color: #fff; margin-top: 15px;} footer{color: #0000ee; font-size: 15px; margin-top: 25px;}</style>"
buf = buf.."<font face='verdana'><center><h1> WiFi Inteligent House</h1></center>";
buf = buf.."<a href=\"?pin=ON1\">&nbsp;<center><h2>_1_</h2></center></a>&nbsp<a";
buf = buf.."<footer><center><p>Project 2017</p></center></footer>";
local _on,_off = "",""
if(_GET.pin == "ON1")then
tmr.stop(0);
uart.write( 0, 0xA0,0x01,0x01,0xA2);
tmr.alarm(0, 1000, 1, function() uart.write( 0, 0xA0,0x01,0x00,0xA1) end )
end
client:send(buf);
client:close();
collectgarbage();
end)
end)
User avatar
By Pol
#77512 My problem to access ESP8266 running nodeMCU LUA website by IOS devices is fixed now :-)

Runs OK on SW release:
nodemcu_integer_0.9.6-dev_20150704.bin
nodemcu-1.5.4.1

Does NOT run on SW R:
2.0.0-master
2.2.0-master_20180608




PROBLEM: iOS ignoring NodeMCU http server response and SAFARI/CHROME browser shows no connection.

With the following code I am able to access my web page running ESP8266 nodeMCU LUA:
• Android - Chrome/Firefox,
• Windows - IE/Chrome/Firefox
• IOS (iPAd/IPhone) - Safari/Chrome
and send with just button click a command to close relay for given time 1000ms in order to open garege door or to manipulate with any other device.


local buf = "HTTP/1.1 200 OK\r\n, Content-type: text/html\r\n, Connection: close\r\n\r\n";


full code bellow:



-- must switch to 9600 baud to control relay
uart.setup(0,9600,8,0,1)

-- join wifi
wifi.setmode(wifi.SOFTAP)

local cfg

cfg = {
ip = "10.10.10.10",
netmask = "255.255.255.0",
gateway = "10.10.10.10"
}
wifi.ap.setip(cfg)

cfg = {
ssid = "GATE",
pwd = "1234567890"
}
wifi.ap.config(cfg)


-- Lua web server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)

local buf = "HTTP/1.1 200 OK\r\n, Content-type: text/html\r\n, Connection: close\r\n\r\n";


local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = buf.."<head><meta name='viewport' content='width=200px, initial-scale=1.0'/>"
buf = buf.."<style>body { background-color: #606060;} h2{border: 4px solid #000; border-radius: 23px; padding: 20px; font-size: 80px;} h1{color: #fff; margin-top: 15px;} footer{color: #0000ee; font-size: 15px; margin-top: 25px;}</style>"
buf = buf.."<font face='verdana'><center><h1> WiFi Inteligent House</h1></center>";
buf = buf.."<a href=\"?pin=ON1\">&nbsp;<center><h2>_G_A_T_E_</h2></center></a>&nbsp<a";
buf = buf.."<footer><center><p>Project 2017</p></center></footer>";
local _on,_off = "",""
if(_GET.pin == "ON1")then
tmr.stop(0);
uart.write( 0, 0xA0,0x01,0x01,0xA2);
tmr.alarm(0, 1000, 1, function() uart.write( 0, 0xA0,0x01,0x00,0xA1) end )
end
client:send(buf);
client:close();
collectgarbage();
end)
end)