1
votes

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
Not sure if it makes any difference but in the second callback the socket parameter shouldn't be called conn as this "overlays" with the conn of the first callback. Make it something like function(sck,payload) sck:send(). - Marcel Stör
Out of curiosity I printed the [address of] the connection parameter at each level of nesting, i.e., print(conn) conn:on("sent", function(sck) print(sck) sck:close() end)end). The address of the object passed as conn was the same as sck. The only object that had a different address was the one passed to the disconnection callback, they didn't match any of the values used anywhere else in the code. - Mark McGinty

1 Answers

1
votes

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)