luawebserveresp8266nodemcu

Lua NodeMCU web server connection error (address in use stack traceback)


I want to turn on and off a led from a web server using NodeMCU but every time I compile the same error occurs. I am in a beginner level so I need some help. I believe this error has something to do with the function listen(). Maybe because I changed my DNS, the port 80 can't be used.

Error:

 dofile("ConectarRedeWireless.lua");
  192.168.137.57    255.255.255.0   192.168.137.1
 ConectarRedeWireless.lua:13: address in use
 stack traceback:
 [C]: in function 'listen'
 ConectarRedeWireless.lua:13: in main chunk
 [C]: in function 'dofile'
    stdin:1: in main chunk

Code:

-- Conexao na rede Wifi
wifi.setmode(wifi.STATION)
wifi.sta.config("goncalo","936674888")
print(wifi.sta.getip())
-- Definicoes do pino do led
led1 = 1
gpio.mode(led1, gpio.OUTPUT)
-- Definicoes do Web Server
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
    buf = buf.."<h1><u>FILIPEFLOP</u></h1>";
    buf = buf.."<h2><i>ESP8266 Web Server</i></h2>";
    buf = buf.."<p><a href=\"?pin=LIGA1\"><button><b>LED 1 LIG</b></button></a> <br/><br/><a href=\"?pin=DESLIGA1\"><button><b>LED 1 DES</b></button></a></p>";
            local _on,_off = "",""
    if(_GET.pin == "LIGA1")then
          gpio.write(led1, gpio.HIGH);
    elseif(_GET.pin == "DESLIGA1")then
          gpio.write(led1, gpio.LOW);
    end
    client:send(buf);
    client:close();
    collectgarbage();
end)
end)

Solution

  • First of all I suggest you use a proper init sequence in which you wait for your device to be fully connected to the WiFi until you set up the server. We keep a template in our documentation.

    srv:listen(80,function(conn) can only be called once for any given port because you can't have multiple server sockets listening on the same port. That's why you get the

    address in use

    error. Our net.server:listen() documentation shows how to check whether srv is available before you listen. You can extend that by running srv:close() first before you listen again. Something like this:

    sv = net.createServer(net.TCP, 30)
    if sv then
      sv:close()
      sv:listen(80, function(conn)
        -- do something
      end)
    end
    

    Btw, where did you get that code sample from? I've seen this several times here on Stack Overflow but it's got more flaws that should be corrected.