NodeMCU服务器没有响应

时间:2017-01-01 23:53:37

标签: esp8266 nodemcu

使用NodeMCU充当wifi接入点。接入点正常工作,我可以连接到它,当我访问192.168.4.1时,print(payload)我的请求被打印到控制台,这样一切正常,但iOS上的Safari抱怨服务器已停止响应。

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

这是打印到控制台的请求

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 个答案:

答案 0 :(得分:1)

测试一下:

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)
相关问题