I\'m trying to make a simple HTTP server with NodeMCU. I start up nodeMCU then connect it to the wifi and then run the program below. I can connect to the server from my browser
First of all, you shouldn't use those old 0.9.x binaries. They're no longer supported and contain lots of bugs. Build a custom firmware from the dev
(1.5.1) or master
(1.4) branch: http://nodemcu.readthedocs.io/en/dev/en/build/.
With version >1.0 of the SDK (this is what you get if you build from current branches) conn:send
is fully asynchronous i.e. you can't call it multiple consecutive times. Also, you must not call conn:close()
right after conn:send()
as the socket would possibly get closed before send()
is finished. Instead you can listen to the sent
event and close the socket in its callback. Your code works fine on an up-to-date firmware if you consider this.
A more elegant way of asynchronous sending is documented in the NodeMCU API docs for socket:send(). However, that method uses more heap and is not necessary for simple cases with little data like yours.
So, here's the full example with on("sent")
. Note that I changed the favicon to an external resource. If you use "/" the browser still issues an extra request against your ESP8266.
a = 0
function receive(conn, payload)
print(payload)
a = a + 1
local content="<!DOCTYPE html><html><head><link rel='icon' type='image/png' href='http://nodemcu.com/favicon.png' /></head><body><h1>Hello!</h1><p>Since the start of the server " .. a .. " connections were made</p></body></html>"
local contentLength=string.len(content)
conn:on("sent", function(sck) sck:close() end)
conn:send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length:" .. contentLength .. "\r\n\r\n" .. content)
end
function connection(conn)
conn:on("receive", receive)
end
srv=net.createServer(net.TCP, 1)
srv:listen(8080, connection)