A print statement outside the trm.alarm clause is proving impossible to implement and if I try to embed a html statement like
buf = buf.."<p style='color:black'>" ..vars.."</p>";
I get a Lua error message about trying to concat a NILL value.
Any ideas on how to display the content of 'vars' would be welcome.
wifi.setmode(wifi.STATION)
wifi.sta.config("virginmedia7991485","cduqmqdp")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function() -- start timer 1
cfg =
{
ip="192.168.0.201",
netmask="255.255.255.0",
gateway="192.168.0.1"
}
wifi.sta.setip(cfg) -- overwrite the DHCP info
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1) -- stop timer 1
print("Config done, IP is "..wifi.sta.getip())
end
end)
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(82,function(conn) -- Listen on port 82
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.."<html>";
buf = buf.."<head>";
buf = buf.."<title>ESP8266 LED Remote Control</title>";
buf = buf.."</head>";
buf = buf.."<body style='background-color:#FFFF77'>";
buf = buf.."<h1 style='color:blue'> ESP8266 Web Server</h1>";
buf = buf.."<p style='color:green'>GPIO0_LED GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a> <a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
buf = buf.."<p style='color:red'>GPIO2_LED GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a> <a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
buf = buf.."Last Command Status: ";
if(_GET.pin == "ON1")then
gpio.write(led1, gpio.HIGH);
buf = buf.."GPIO_0 is ON </p>";
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.LOW);
buf = buf.."GPIO_0 is OFF </p>";
elseif(_GET.pin == "ON2")then
gpio.write(led2, gpio.HIGH);
buf = buf.."GPIO_2 is ON </p>";
elseif(_GET.pin == "OFF2")then
gpio.write(led2, gpio.LOW);
buf = buf.."GPIO_2 is OFF </p>";
else
buf = buf.."Undefined</p>";
end
buf = buf.."</body>";
buf = buf.."</html>";
client:send(buf);
client:close();
collectgarbage();
end)
end)