由于我已将0.9.6-dev_20150704版本的固件(使用https://nodemcu-build.com/和pyflasher)更新为更新版本,如下所示:
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地址时,它无法连接到服务器。你有任何想法如何解决它?
答案 0 :(得分:2)
以下代码(来自http://nodemcu.com/index_en.html的示例)已停止工作:
您的代码示例未在该页面上列出。你将两个单独的例子拼凑成一个单独的程序,然后失败了。这部分是因为其中一个例子不再有用了。
问题就在这里:
Severity Code Description Project File Line Suppression State
Error One or more projects are incompatible with .NETStandard,Version=v1.5.
Error Project Api is not compatible with netstandard1.5 (.NETStandard,Version=v1.5). Project Api supports: net462 (.NETFramework,Version=v4.6.2)
它表明wifi.sta.config
是一个同步操作,阻止设备从接入点获取IP地址。事实并非如此,因此,当下一行执行时,设备获得了IP,这几乎是不可能的。如果您检查串行控制台,可能会在那里看到wifi.sta.config("SSID","password")
print(wifi.sta.getip())
--192.168.18.110
。
更糟糕的是,当nil
运行时仍然没有IP。因此,服务器套接字不受任何限制,您创建了一个僵尸服务器。
这里的主要信息:等到设备获得IP并继续。我们曾经有一个漂亮的simple template in our documentation,但为了完整起见,它最近更新了:https://nodemcu.readthedocs.io/en/latest/en/upload/#initlua。对于初学者来说,它可能会被剥夺:
net.createServer
此外,如果您要从浏览器进行测试,您的服务器应该发送正确的HTTP标头。像这样:
-- 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