Current Lua downloadable firmware will be posted here

User avatar
By Trickuncle
#36239 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?

Code: Select allwifi.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>&nbsp;<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>&nbsp;<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)
 
User avatar
By Trickuncle
#36240 Running the browser console (F12), the response to clicking an element (pushbutton, etc.) is two parts:
Key Value Response HTTP/1.0 200 OK
which I was expecting but then the second part is all of the webpage HTML and javascript which I don't want.
User avatar
By xtal
#36247 I'm having a similiar problem... I have several buttons ..
On some buttons the page updates immediately [ instant refresh ?]
Other buttons will wait until the meta refresh time [15 sec in my case] then update...
1> BlinkEyes waits meta refresh [ at 1 time was immediate, have no clue what changed]
2> Get/Disp/Adc immediate
3> serial output immediate input sometime immed, but mostly waits meta redresh
3> Refresh change immediate [5-10 step1, 10-30 step 5, repeat]
4> baud change is immediate, but baud display is meta refresh [9600, 38400,115200]

I thought that maybe the top of the page was getting updated before the vars got changed..
But I don't really see how that could happen...
I don't know much about html, I've just snipped/glued/tried until I got what I wanted,
so may have some html issues, however the only issue that bothers me is the button responses...

As for not wanting refresh, maybe possible to cache the page , If you can figure how to control
caching....
User avatar
By Trickuncle
#36250 Another variable is that different browsers produce different results.

On my Win8 box, Firefox sends slider control data that 'sticks' - it doesn't reset to default after changing. But the slider control 'handle' in the browser returns to the center default position. IE works the same but Chrome doesn't end up changing the pwm value in the ESP8266 module at all.

On my Android phone, Chrome works the same - ie., it doesn't while Firefox sets the pwm value but immediately resets it back to default value.