This program will show the text input page and from the debug will switch into the second page per the logic but the pages doesnt load. If i submit the data a second time even though its already in the new mode it will show the second page.
Here is my init script.
print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("SSID","PWD")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
end
end)
Here is my web load code. Not sure if its an HTML issue or a ESP code issue.
-- need ip setup done first. testing
apmode=0
--Create a servers and give us a simple web page.
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
local _on,_off = "",""
print ("apmode is " .. apmode )
if apmode == 0 then
buf = buf.."<h1>SETUP AP</h1>";
buf = buf .."<FORM ACTION=\"?\"> mytext: <INPUT NAME=\"TEXT\"><INPUT TYPE=SUBMIT> </FORM>"
if(_GET.TEXT ~= nil )then
if(_GET.TEXT ~= "" )then
mytext = _GET.TEXT
print ("mytext " .. mytext)
apmode=apmode + 1
end
end
end
if apmode == 2 then
print ("into on/off")
buf = buf.."<h1> Outlet On/Off</h1>";
buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a> <a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
if(_GET.pin == "ON2")then
print ("on 2 is " .. _GET.pin )
elseif(_GET.pin == "OFF2")then
print ("of 2 is " .. _GET.pin )
end --end if
end
client:send(buf);
client:close();
collectgarbage();
-- if text has been input then switch to pin toggle mode.
if ( apmode == 1 ) then
print ("Setting to outlet mode:")
apmode=2
end
end)
end)
Here is the debug
pmode is 0
apmode is 0
mytext ccxcxcxcx -- correct page has been displayed.
Setting to outlet mode: -- switches modes but still have the old text input. Hit subit again
apmode is 2
into on/off
apmode is 2
into on/off -- switches to off/mode with correct display.
apmode is 2
into on/off