【发布时间】:2017-05-07 22:05:47
【问题描述】:
我正在尝试使用 frightanic.com 的 NodeMCU 自定义构建,使用 lua 在 ESP8266 上创建本地 http 服务器。
当我创建一个本地 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
这是不可能的吗?如果是,那我该怎么做?
如果我在计时器内创建一次服务器,则它不会创建任何本地服务器,即。我无法打开我的本地服务器 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
【问题讨论】:
标签: lua esp8266 httpserver nodemcu panic