2016-06-01 1 views
2

ESP8266에서 작업 중이며 Lua를 사용하여 프로그램을 작성하려고합니다. 나는 TCP 서버를 만들려고했지만 아래의 예제 코드를 작성할 때 "단 하나의 TCP 서버 만 허용되었습니다"라는 오류 메시지 이름을 사용합니다. 나는 서버를 만들고 닫을 수 없다.하나의 tcp 서버 만 허용 Lua를 사용하는 ESP8266

어떻게 해결할 수 있습니까?

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, "&", 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("<!DOCTYPE html> ") 
     c:send("<html> ") 
     c:send("<body> ") 
     c:send("<h1>ESP8266 Wireless control setup</h1>") 
     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("<h2>" .. mac_mess1 .. "</h2>") 
     c:send("<h2>" .. mac_mess2 .. "</h2>") 
     c:send("<h2>Enter SSID and Password for your WIFI router</h2>") 
     c:send("</form> </html>") 
     c:send("<form action='' method='get'>") 
     c:send("SSID:") 
     c:send("<input type='text' name='SSID' value='' maxlength='100'/>") 
     c:send("<br/>") 
     c:send("Password:") 
     c:send("<input type='text' name='Password' value='' maxlength='100'/>") 
     c:send("<input type='submit' value='Submit' />") 
    end); 
end); 
+0

열기 두 가지 질문 여기 예 후를 닫고 새로 만들 경우 서버가 이미 시작되었는지 여부를 서버 점검을 .. 작성하기 전에. –

+0

2 개의 다른 새로운 화제 권리의 밑에 2 개의 질문을 의미 하는가? – ahmd14

+0

예. SO는 질문 및 답변 사이트이므로 질문은 "주제"(BBS 비유) 일 수 있습니다. –

답변

2

Nodemcu는 하나의 tcp 서버 만 허용합니다. init.lua 파일을 저장하려고 할 때 esplorer ide를 사용하면 이런 일이 발생합니다.

  1. nodemcu에 이미있는 init 파일이 삭제됩니다.
  2. 새로운 init.lua 파일이 작성됩니다.
  3. dofile("init.lua")은 init.lua 파일을 실행하는 nodemcu에서 실행됩니다.

세 번째 단계를 실행할 때 이미 하나 이상의 것을 만들 수없는 nodemcu에서 실행중인 tcp 서버가 있습니다.

해상도 : - 간단히 nodemcu를 재설정하십시오. 최신 init.lua가 실행되고 tcp 서버가 작동 중입니다.

+0

아마도 이것은 "순정 주의자"대답 일 것입니다. –

13

이미 ESP8266에서 실행중인 TCP 서버를 닫아야합니다. 이 두 문제에 대한 SO에

if srv~=nil then 
    srv:close() 
end 

srv=net.createServer(net.TCP) 
srv:listen(80,function(conn) 
    conn:on("receive",function(conn,payload) 
    print(payload) 
    conn:send("<h1> Hello, World.</h1>") 
    end) 
end) 
+1

나는이 대답에 동의합니다. – ambassallo

관련 문제