title: Only one tcp server allowed ESP8266 with Lua
body: <p>I am working on ESP8266 and i am trying to program with Lua. I tried to create TCP server,but i take an error message name "only one tcp server allowed" when i wrote sample code below. I create a server and I cant close.</p>
  
  <p>How can I fix it?</p>
  
  <pre><code>print("ESP8266 mode is: " .. wifi.getmode());
  
  cfg = {};
  -- Set the SSID of the module in AP mode and access password
  cfg.ssid = "SSID";
  cfg.pwd = "password";
  if ssid and password then
      print("ESP8266 SSID is: " .. cfg.ssid .. " and PASSWORD is: " ..
              cfg.password)
  end;
  -- Now you should see an SSID wireless router named ESP_STATION when you scan for available WIFI networks
  -- Lets connect to the module from a computer of mobile device. So, find the SSID and connect using the password selected
  wifi.ap.config(cfg);
  ap_mac = wifi.ap.getmac();
  -- create a server on port 80 and wait for a connection, when a connection is coming in function c will be executed
  sv = net.createServer(net.TCP, 30);
  sv:listen(80, function(c)
      c:on("receive", function(c, pl)
          -- print the payload pl received from the connection
          print(pl);
          print(string.len(pl));
          -- wait until SSID comes back and parse the SSID and the password
          print(string.match(pl, "GET"));
          ssid_start, ssid_end = string.find(pl, "SSID=");
          if ssid_start and ssid_end then
              amper1_start, amper1_end = string.find(pl, "&amp;", ssid_end + 1);
              if amper1_start and amper1_end then
                  http_start, http_end = string.find(pl, "HTTP/1.1", ssid_end + 1);
                  if http_start and http_end then
                      ssid = string.sub(pl, ssid_end + 1, amper1_start - 1);
                      password = string.sub(pl, amper1_end + 10, http_start - 2);
                      print("ESP8266 connecting to SSID: " .. ssid .. " with PASSWORD: " .. password);
                      if ssid and password then
                          sv:close();
  
                          -- close the server and set the module to STATION mode
                          wifi.setmode(wifi.STATION);
                          tmr.stop(2)
  
  
  
                          print("ESP8266 mode now is: " .. wifi.getmode());
                          -- configure the module wso it can connect to the network using the  received SSID and password
                          wifi.sta.config(ssid, password);
                          print("Setting up ESP8266 for station mode…");
                          print("Please restart your device");
                          tmr.delay(10000000);
                          print("Mode is " .. wifi.getmode());
                          print("Heap:" .. node.heap())
                          print("");
                      end;
                  end;
              end;
          end;
          -- this is the web page that requests the SSID and password from the user
          c:send("&lt;!DOCTYPE html&gt; ")
          c:send("&lt;html&gt; ")
          c:send("&lt;body&gt; ")
          c:send("&lt;h1&gt;ESP8266 Wireless control setup&lt;/h1&gt;")
          mac_mess1 = "The module MAC address is: " .. ap_mac
          mac_mess2 = "You will need this MAC address to find the IP address of  the module, please take note of it."
          c:send("&lt;h2&gt;" .. mac_mess1 .. "&lt;/h2&gt;")
          c:send("&lt;h2&gt;" .. mac_mess2 .. "&lt;/h2&gt;")
          c:send("&lt;h2&gt;Enter SSID and Password for your WIFI router&lt;/h2&gt;")
          c:send("&lt;/form&gt; &lt;/html&gt;")
          c:send("&lt;form action='' method='get'&gt;")
          c:send("SSID:")
          c:send("&lt;input type='text' name='SSID' value='' maxlength='100'/&gt;")
          c:send("&lt;br/&gt;")
          c:send("Password:")
          c:send("&lt;input type='text' name='Password' value='' maxlength='100'/&gt;")
          c:send("&lt;input type='submit' value='Submit' /&gt;")
      end);
  end);
  </code></pre>