Post your best Lua script examples here

User avatar
By ohgary
#33215 I am trying to get my ESP to present a web page with a text input box. Once the data has been inputted switch to another page. I have a secondary program that sets up the wifi so I can get to the page and that is working ok.
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>&nbsp;<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
User avatar
By forlotto
#33223 I don't see where you are trying to get anything to reload so to speak ? Please explain.
User avatar
By ohgary
#33245 in the first pass the page says SETUP AP with a text box. once you hit submit the variable is reset that should display the outlet on/off input buttons.

I would think that if I close the connection and then reopen it with the Outlet buttons now being setup I would get a new page.

Things work correctly but I have to run the SETUP AP twice. to actually see the outlet page, but the variable that indicates I should be in outlet mode changes as soon as I hit submit on the AP page.
User avatar
By forlotto
#33321 Have to look into it more but this just caught me

Code: Select allif apmode == 2 then

should the value not be if
Code: Select allapmode == 1 then
?

Alternately you could try and set initial status to apmode==1

You are utilizing a counter which forces you to go through the routine again more or less I don't see the point you are also using apmode as a global variable with an implicit value of x in each case as well both of which I don't know if are needed or good practice just to through that out there as well.

I will try and help you figure this out but bare with me here I am very new to this as well.

I also notice some typos in your connotations that you are using to buffer your web page might want to spell off correctly right now it says of there are a couple of other things it just is basically good practice to pay close attention to these things to eliminate confusion not that it is the cause of what is happening "the typos" that is but might wish to fix these...

Take care I'll try and keep an eye on your progress.

Currently you are using apmode to do nothing other than print status ... The text upon submission does nothing either it just spits out what you put in as printed. You could just eliminate it and just buffer your page with the on/off function currently as it stands to be honest.