2017-05-06 48 views
0

我正尝试在lua上使用NodeMCU自定义构建由frightanic.com在ESP8266上创建本地http服务器。PANIC:调用Lua API中的未受保护的错误(wificonfig.lua:33:地址正在使用)

当我创建一个本地http服务器以及已经在端口80上侦听并从我的服务器站点获取数据的连接时,它给我一个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 

是没可能做到这一点?如果是,比我怎么做呢?

如果我在定时器内部创建服务器一次,比它不创建任何本地服务器ie。我无法打开我的本地服务器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上运行的操作系统,但通常这个错误意味着已经有一个套接字绑定到这个地址和端口,并且你不能在同一个位置有另一个套接字。因此,如果已经有一个Web服务器在这个系统上运行,那么你不能在同一地址启动另一个Web服务器。

如果您一旦成功启动服务器,无法通信(由于服务器的HTTP响应不正确),然后尝试重新启动服务器,也会发生此类错误:重新启动服务器需要一段时间一旦连接建立到这个套接字,同一套接字,所以你可能需要等待几分钟才能再次启动服务器(或重新启动系统)。

以你的代码仔细看可以看到服务器从计时器里开始:

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

从快看看the documentation of tmr它看起来像你正在呼吁与tmr.ALARM_AUTO定时器,使其会一再重复。这意味着在您已经设置了服务器之后,计时器会重复,因此会尝试再次设置服务器 - 在同一个端口上。这会导致使用中的地址错误。

+0

我最初使用HTTP/1.0 200自己确定,但是当它不能比使用HTTP/1.0尝试时,因为我对这些东西没有太多的知识。 错误所指的地址是我的WiFi模块的IP地址(作为连接到接入点的站)还是其他地址? 正如你可以在我的代码中看到我打开了端口80上的客户端连接到一些服务器,然后我close.conn:连接(80,“server.co.in”) – aditgupta100

+0

我甚至试着听其他端口说9999但我得到了同样的错误。 – aditgupta100

+0

错误消息指的是您的WiFi模块本地。我建议你先简化你的代码,即只实现一个服务器,而不是别的。 –

相关问题