What part of this code generates a response?
Posted: Fri Dec 11, 2015 3:59 pm
This code was found on the net and modified by me to add a slider control. The pushbuttons and the slider work but there is one overall problem that I need help on. Pushing any pushbutton or activating the slider while in a browser sends commands to the ESP8266 but then the ESP8266 responds somehow and makes the browser reload the entire page. This wipes out the slider position and data.
How do I prevent the page from being automatically reloaded?
How do I prevent the page from being automatically reloaded?
Code: Select all
wifi.setmode(wifi.STATION)
wifi.sta.config("XXX","YYY")
print(wifi.sta.getip())
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP,3600)
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
-- pwm.setup(1,1000,511)
pwm.setup(led1,1000,511)
-- pwm.start(1)
--tmpwm --global var
tmpwm = 50
if(_GET.somevar) then
pwm.start(led1)
xxx = (_GET.somevar);
tmpwm = tonumber(xxx)
if(tmpwm>1023) then
tmpwm = 1023
end
--pwm.setduty(led1,tmpwm)
end
ramp = 0
pwmout = 100
function motordrive()
ramp = ramp + 80
if (ramp > tmpwm) then
ramp = 0
if rampdir then
pwmout = pwmout + 10
if pwmout > 1023 then
pwmout = 1023
rampdir = false
end
else
pwmout = pwmout - 10
if pwmout < 10 then
pwmout = 5
rampdir = true
end
end
end
pwm.setduty(led1,pwmout)
end
tmr.alarm(0, 1, 1, motordrive)
local _on,_off = "",""
if(_GET.pin == "OFF1")then
gpio.write(led1, gpio.HIGH);
elseif(_GET.pin == "ON1")then
gpio.write(led1, gpio.LOW);
elseif(_GET.pin == "OFF2")then
gpio.write(led2, gpio.HIGH);
elseif(_GET.pin == "ON2")then
gpio.write(led2, gpio.LOW);
end
conn:send('<!DOCTYPE html>')
conn:send('<html>')
conn:send('<body>')
conn:send('<h1><center>ESP8266 Web Server </center></h1>')
conn:send('<p><center>GPIO0 <a href=\"?pin=ON1\"><button style="font-size:300%;">ON</button></a> <a href=\"?pin=OFF1\"><button style="font-size:300%;">OFF</button></center></a></p>')
conn:send('<p><center>GPIO2 <a href=\"?pin=ON2\"><button style="font-size:300%;">ON</button></a> <a href=\"?pin=OFF2\"><button style="font-size:300%;">OFF</button></center></a></p>')
conn:send('<center><font color=red><b>Faster</b></font>')
conn:send('<input style="width:550px; height:50px" type="range" name="cmd" id="cmd" value="" min=1 max=256 step=1 onchange="myFunction()"/>')
conn:send('<font color=red><b>Slower</b></font></center></form>')
conn:send('<center><p id="demo" /p></center>')
conn:send('<script>')
conn:send('function myFunction() {')
conn:send(' var x = document.getElementById("cmd").value;')
conn:send(' x = x * 4 ;')
conn:send(' if (x > 1023) {')
conn:send(' x = 1023;')
conn:send(' }')
conn:send(' document.getElementById("demo").innerHTML = x ;')
conn:send(' var uri = "\?somevar=" + x;')
conn:send(' var enc = encodeURI(uri);')
conn:send(' document.location.href=\enc;')
conn:send('}</script>')
conn:send('</body>')
conn:send('</html>')
client:close();
collectgarbage();
end)
end)