2017-05-06 14 views
0

frightanic.com의 NodeMCU 사용자 빌드를 사용하여 lua로 ESP8266에서 로컬 http 서버를 만들려고합니다.패닉 : Lua API (wificonfig.lua : 33 : 사용중인 주소)에 대한 보호되지 않은 오류

포트 80에서 이미 수신 대기중인 연결과 로컬 http 서버를 만들고 서버 사이트에서 데이터를 가져 오는 경우 PANIC 오류가 발생합니다.

wifi.setmode(wifi.STATION) 
wifi.sta.config("SSID","password") 
wifi.sta.connect() 

tmr.alarm(1,10000, 1, function() 
    if (wifi.sta.getip() == nil) then 
     print("IP unavaiable, Waiting...") 
    else 
     foo() 
     local_server() 
    end 
end) 


function foo() 
     conn = nil 
     conn=net.createConnection(net.TCP,0) 
     conn:on("receive", function(conn, payload) 
      print("payload : "..#payload); 
     end) 
     conn:connect(80,"server.co.in") 
     conn:on("connection", function(conn, payload) 
      conn:send("GET /mypage?id=deadbeef HTTP/1.0\r\nHost: server.co.in\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end) 

     conn:on("sent",function(conn) 
       print("Closing server connection") 
       conn:close() 
      end) 
end 

function local_server() 
    srv=net.createServer(net.TCP) 
    srv:listen(80,function(conn) 
     conn:on("receive", function(client,request) 
      local buf = "Hello world"; 
      client:send('HTTP/1.0\n\n') 
      client:send('<!DOCTYPE HTML>\n') 
      client:send('<html>\n') 
      client:send('<head><meta content="text/html; charset=utf-8">\n') 
      client:send('<title>Local server</title></head>\n') 
      client:send('<style>body{ background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style>\n";') 
      client:send(buf); 

      conn:on("sent",function(conn) 
       srv:close() -- Even tried with client:close() 
      end) 
     end) 
    end) 

end 

는이 작업을 수행 할 수 없습니다 :

여기 내 코드입니까? 그렇다면 어떻게 할 수 있습니까?

타이머를 서버에 한 번만 만들면 모든 로컬 서버가 생성되지 않습니다. 로컬 서버 192.168.x.y를 열 수 없습니다.

wifi.setmode(wifi.STATION) 
wifi.sta.config("SSID","password") 
wifi.sta.connect() 

tmr.alarm(1,10000, 1, function() 
    if (wifi.sta.getip() == nil) then 
     print("IP unavaiable, Waiting...") 
    else 
     print("Creating a server"); 
     srv=net.createServer(net.TCP,0) 
     srv:listen(80,function(conn) 
     end) 
     tmr.stop(1) 
     tmr.alarm(1,10000, 1, function() 
       foo() 
       local_server() 
     end) 
    end 
end) 


function foo() 
     conn = nil 
     conn=net.createConnection(net.TCP,0) 
     conn:on("receive", function(conn, payload) 
      print("payload : "..#payload); 
     end) 
     conn:connect(80,"server.co.in") 
     conn:on("connection", function(conn, payload) 
      conn:send("GET /mypage?id=deadbeef HTTP/1.0\r\nHost: server.co.in\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end) 

     conn:on("sent",function(conn) 
       print("Closing server connection") 
       conn:close() 
      end) 
end 

function local_server() 
     conn:on("receive", function(client,request) 
      local buf = "Hello world"; 
      client:send('HTTP/1.0\n\n') 
      client:send('<!DOCTYPE HTML>\n') 
      client:send('<html>\n') 
      client:send('<head><meta content="text/html; charset=utf-8">\n') 
      client:send('<title>Local server</title></head>\n') 
      client:send('<style>body{ background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style>\n";') 
      client:send(buf); 

      conn:on("sent",function(conn) 
       srv:close() -- Even tried with client:close() 
      end) 
     end) 
end 

답변

0
 client:send('HTTP/1.0\n\n') 

이 유효한 HTTP 응답되지 않습니다 :

여기 내 수정 된 코드입니다. 최소한 HTML 페이지를 제공하는 응답이

HTTP/1.0 200 ok\r\n 
Content-type: text/html\r\n 
\r\n 
here comes the HTML body 

과 같아야하지만 사용중인 주소에 대한 불만 때문에이 아마 공황의 원인이 아니다. ESP8266에서 실행중인 OS에 대해서는 아무 것도 모르지만 일반적으로이 오류는 이미이 주소와 포트에 바인드 된 소켓이 있으며 동일한 위치에 다른 소켓을 가질 수 없다는 것을 의미합니다. 따라서 이미이 시스템에서 실행중인 웹 서버가있는 경우 동일한 주소에서 다른 웹 서버를 시작할 수 없습니다.

이러한 오류는 서버를 성공적으로 시작했거나 (서버의 부적절한 HTTP 응답으로 인해) 통신에 실패한 다음 서버를 다시 시작하려고 시도한 경우에도 발생할 수 있습니다. 다시 사용할 수 있으려면 시간이 걸립니다 이 소켓에 연결되면 동일한 소켓. 서버를 다시 시작하거나 시스템을 재부팅하기 전에 몇 분 정도 기다려야 할 수도 있습니다. 당신은 그래서 그 tmr.ALARM_AUTO와 타이머를 호출하는 것 같습니다 the documentation of tmr에서 얼핏에서

tmr.alarm(1,10000, 1, function() 
... 
     local_server() 

: 하나의 서버가 타이머 내부에서 시작되는 것을 볼 수 있습니다 코드를 자세히 살펴보면

반복해서 반복됩니다. 즉, 서버를 이미 설정 한 후에 타이머가 반복되어 동일한 포트에서 서버를 다시 설정하려고합니다. 이로 인해 사용중인 주소의 오류가 발생합니다.

+0

처음에는 HTTP/1.0 200 ok 자체를 사용했지만 HTTP/1.0으로 시도하는 것보다 작동하지 않는 경우가 많았습니다. 오류가 참조하는 주소는 내 WiFi 모듈 (액세스 포인트에 연결된 스테이션으로 작동)의 IP 주소이거나 다른 주소입니까? 내 코드에서 볼 수 있듯이 포트 80에서 클라이언트 연결을 열었습니다. 그런 다음 close.conn : connect (80, "server.co.in") – aditgupta100

+0

나는 다른 포트를 듣고 시도했습니다. 9999 하지만 같은 오류가 발생했습니다. – aditgupta100

+0

오류 메시지가 귀하의 WiFi 모듈에 대해 로컬임을 나타냅니다. 먼저 코드를 단순화하는 것이 좋습니다. 즉, 서버 만 구현하면됩니다. –