NodeMCU Server not responding

809 views Asked by At

Using NodeMCU to act as a wifi access point. The access point is working and I can connect to it, and when I visit 192.168.4.1, print(payload) my request is printed to the console so that's all working but Safari on iOS complaining that the server has stopped responding.

function initialConnection()
  wifi.setmode(wifi.SOFTAP)

    -- Setup as Access Point
    print(wifi.ap.config({
        ssid = "SSIDNAME",
        pwd = "SSIDPASSWORD",
        auth = wifi.OPEN
    }))

  server = net.createServer(net.TCP, 30)

  if server then
    server:listen(80, function(conn)
      conn:on("receive",function(conn,payload)
        print(payload)
        conn:send("<h1> Hello, NodeMCU!!! </h1>")
      end)
    end)
  end

  -- statusLed:flashBlue( 100 )
end

This is the request which gets printed to the console

GET /?gddf HTTP/1.1
Host: 192.168.4.1
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0 Mobile/14C92 Safari/602.1
Accept-Language: en-us
DNT: 1
Cache-Control: max-age=0
1

There are 1 answers

3
jankop On BEST ANSWER

Test this:

srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
conn:on("receive", function(sck, payload)
    print(payload)
    sck:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>Hello, NodeMCU.</h1>")
end)
conn:on("sent", function(sck) sck:close() end)end)