I'm trying to create a local http server on ESP8266 with lua using NodeMCU custom build by frightanic.com.
When i create a local http server along with a connection that is already listening on port 80 and fetching data from my server site, it is giving me PANIC error.
Here's my code :
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
Is it not possible to do this ? If yes, than how can i do it ?
If i create server once inside timer, than it doesn't create any local server ie. i'm not able to open my local server 192.168.x.y.
Here's my modified code :
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