-->
Page 1 of 1

simple 3-object switch, so why is it resetting HTML?

PostPosted: Tue Apr 30, 2019 9:31 am
by gelfling6
Hopefully, this hasn't been posted before, But...
the simple direct Wifi <---> NodeMCU (in AP Mode) single object switch, but adding 2 more connections/LED's, (a simple Common-Anode tri-color LED as the base setup right now.). now if I strip-out the extra 2 objects/LED's, it works fine, but the full code, all attempts to call-up the html page, are refused (web page reset)...
the code:
Code: Select allwifi.setmode(wifi.SOFTAP)
wifi.ap.config({ssid="turtle",pwd="5352475347"})
led1 = 3
led2 = 4
led3 = 2
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
gpio.mode(led3, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        buf = buf.."HTTP/1.1 200 OK\n\n"
        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.."<h1> ESP8266 Web Server</h1>";
        buf = buf.."<p>GPIO3 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>GPIO4 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        buf = buf.."<p>GPIO2 <a href=\"?pin=ON3\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF3\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.LOW);
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "ON3")then
              gpio.write(led2, gpio.LOW);
        elseif(_GET.pin == "OFF3")then
              gpio.write(led2, gpio.HIGH);

        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

So, what is going wrong here? is the code too big for the NodeMCU to handle all at once? should I be splitting it into lua files , or, is there a incorrect piece of the code? be gentle, still a newbie at this.

Re: simple 3-object switch, so why is it resetting HTML?

PostPosted: Wed May 01, 2019 1:44 am
by marcelstoer
The send function is asynchronous - as are many others with NodeMCU. You're likely closing the socket before everything has been sent. Remember the general programming model.

The NodeMCU programming model is similar to that of Node.js, only in Lua. It is asynchronous and event-driven. Many functions, therefore, have parameters for callback functions.


See our documentation at https://nodemcu.readthedocs.io/en/lates ... #example_6 for a good example.

Re: simple 3-object switch, so why is it resetting HTML?

PostPosted: Wed May 01, 2019 8:02 am
by gelfling6
figured as much.. Odd thing is, 2-object (only 2 LED's), configuring the device as a station, rather than an access point, it should (???) work, but I'm trying to keep it down to one point (the laptop w/Wifi) to the other (the ModeMCU), not requiring a router (3rd device) in between. (as the local router is one of those fun-ones you need to log into the network through a firewall.) Back to the tinkering board agaion, I guess. thanks for the input