Current Lua downloadable firmware will be posted here

User avatar
By MaxItaly
#64509 Hello,
i'am building a simple AP web server:

wifi.setmode(wifi.SOFTAP)
wifi.ap.config({ssid="myssid",pwd="password"})
wifi.ap.setip({ip="192.168.1.204",netmask="255.255.255.0",gateway="192.168.1.254"})

srv=net.createServer(net.TCP)

srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
buf = buf.."<h1> myAP server </h1>";
buf = buf.."<h2> 192.168.1.254</h2>";
...
client:send(buf);
client:close();
collectgarbage();
end)
end)

the code work fine with version Nodemncu 0.9.6. With new version 2.0.0 master (mudules wifi,net,file,gpio..) the connection at ip_address fail : Google/Firefox show "ERR_CONNECTION_TIMED_OUT".
I execute the file.format without solution.
Can help me ?
Thanks :D
User avatar
By marcelstoer
#64526 There's an important paragraph on http://nodemcu.readthedocs.io/en/latest ... g-firmware

Lua scripts written for one NodeMCU version (like 0.9.x) may not work error-free on a more recent firmware. For example, Espressif changed the socket:send operation to be asynchronous i.e. non-blocking.


By running the following two statements back-to-back

Code: Select allclient:send(buf);
client:close();


you're probably closing the socket before anything was sent to the client. You should only close the socket in the on-sent callback. There's a really nice example at http://nodemcu.readthedocs.io/en/latest ... socketsend but a modified version of your code would be

Code: Select allsrv = net.createServer(net.TCP)

srv:listen(80, function(conn)
  conn:on("receive", function(socket, request)
    local buf = "";
    buf = buf .. "<h1> myAP server </h1>";
    buf = buf .. "<h2> 192.168.1.254</h2>";

    socket:send(buf);
  end)
  conn:on("sent", function(socket)
    socket:close();
  end)
end)