我正尝试在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
我最初使用HTTP/1.0 200自己确定,但是当它不能比使用HTTP/1.0尝试时,因为我对这些东西没有太多的知识。 错误所指的地址是我的WiFi模块的IP地址(作为连接到接入点的站)还是其他地址? 正如你可以在我的代码中看到我打开了端口80上的客户端连接到一些服务器,然后我close.conn:连接(80,“server.co.in”) – aditgupta100
我甚至试着听其他端口说9999但我得到了同样的错误。 – aditgupta100
错误消息指的是您的WiFi模块本地。我建议你先简化你的代码,即只实现一个服务器,而不是别的。 –