【问题标题】:NodeMCU: code responsible for creating server stops working after building new firmwareNodeMCU:负责创建服务器的代码在构建新固件后停止工作
【发布时间】:2017-10-28 13:16:22
【问题描述】:

由于我已将固件(使用 https://nodemcu-build.com/ 和 pyflasher)从 0.9.6-dev_20150704 版本更新到更新版本,如下所示:

NodeMCU custom build by frightanic.com  
branch: master  
commit: c8ac5cfb912ff206b03dd7c60ffbb2dafb83fe5e  
SSL: false  
modules: file,gpio,net,node,rtcmem,rtctime,tmr,uart,wifi  
build   built on: 2017-05-27 13:10  
powered by Lua 5.1.4 on SDK 2.1.0(116b762)  

以下代码(来自http://nodemcu.com/index_en.html 的示例)停止工作:

print(wifi.sta.getip())
--nil
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
print(wifi.sta.getip())
--192.168.18.110

-- a simple http server
srv=net.createServer(net.TCP) 
srv:listen(80,function(conn) 
    conn:on("receive",function(conn,payload) 
    print(payload) 
    conn:send("<h1> Hello, NodeMcu.</h1>")
    end) 
end)

需要明确的是,连接似乎是有效的(因为我在路由器的列表中看到了MCU),但是当我在浏览器中输入MCU的相应地址时,它无法连接到服务器。您有任何解决方法的想法吗?

【问题讨论】:

  • IIRC,NodeMCU 支持 ping,因此快速“ping 192.168.18.110”可能会确定 IP 堆栈是否粘连。另外,如果电源不稳定,这些设备通常会开始动作不稳定(VCC 上是否有一个很好的电容器来平衡流量?)。
  • ping 诊断结果是流量正确,没有丢包。我很确定电源没问题。
  • @Czarek,这解决了吗?
  • @Marcel Stör 是的,谢谢。

标签: lua nodemcu


【解决方案1】:

以下代码(来自http://nodemcu.com/index_en.html 的示例)停止工作:

您的代码示例未在该页面上列出。您从那里将两个单独的示例拼接到一个程序中,但失败了。部分原因是其中一个示例不再有用。

问题就在这里:

wifi.sta.config("SSID","password")
print(wifi.sta.getip())
--192.168.18.110

它建议wifi.sta.config 是一个同步操作,在设备从接入点获得 IP 地址之前一直阻塞。情况并非如此,因此,在执行下一行时,设备获得 IP 几乎是不可能的。如果您检查您的串行控制台,您可能会在那里看到nil

更糟糕的是,当net.createServer 运行时,仍然没有 IP。因此,服务器套接字没有绑定到任何东西,您创建了一个僵尸服务器。

这里的主要信息是:等到设备获得 IP 后再继续。我们曾经有一个漂亮的simple template in our documentation,但为了完整起见,它最近更新了:https://nodemcu.readthedocs.io/en/latest/en/upload/#initlua。对于初学者,它可能会被简化为:

-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there
dofile("credentials.lua")

function startup()
  if file.open("init.lua") == nil then
    print("init.lua deleted or renamed")
  else
    print("Running")
    file.close("init.lua")
    -- the actual application is stored in 'application.lua'
    -- dofile("application.lua")
  end
end

-- Define WiFi station event callbacks
wifi_got_ip_event = function(T)
  -- Note: Having an IP address does not mean there is internet access!
  -- Internet connectivity can be determined with net.dns.resolve().
  print("Wifi connection is ready! IP address is: " .. T.IP)
  print("Startup will resume momentarily, you have 3 seconds to abort.")
  print("Waiting...")
  tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
end



-- Register WiFi Station event callbacks
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, wifi_got_ip_event)

print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config({ ssid = SSID, pwd = PASSWORD, save = true })
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default

此外,如果您从浏览器进行测试,您的服务器应该发送正确的 HTTP 标头。像这样的:

local content = "<h1>Hello, world!</h1>"
conn:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n" .. content)

【讨论】:

  • 也许我们可以让所有者更新nodemcu.com上的内容。
猜你喜欢
  • 2019-10-09
  • 2012-02-25
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-07
  • 2019-06-18
  • 1970-01-01
相关资源
最近更新 更多