我想要使用具有nodeMCU的EPS8266来通过I2C设置我的RTC。Lua脚本不会按顺序执行
这是我sript:
-- file print.lua
local file = assert(loadfile("httpget.lua"))
file() --get Date and Time from google
print("Print follows:") --this should be executed after "file()"
print(date)
这是文件httpget.lua
:
-- file httpget.lua
print('httpget.lua started')
conn=net.createConnection(net.TCP, 0)
-- show the retrieved web page
conn:on("receive", function(conn, payload)
date = string.sub(payload,string.find(payload,"Date: ")
+6,string.find(payload,"Date: ")+35)
conn:close()
end)
conn:on("connection", function(conn, payload)
print('\nConnected')
conn:send("HEAD/HTTP/1.1\r\n"
.."Host: google.com\r\n"
.."Accept: */*\r\n"
.."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"
.."\r\n\r\n")
end)
-- when disconnected, let it be known
conn:on("disconnection", function(conn, payload)
print("Disconnected\r\n"..date)
end)
conn:connect(80,'google.com')
conn = nil
结果是:
> dofile("print.lua")
httpget.lua started
Print follows: -- this should be at the end
nil -- date==nil because httpget.lua not executed
>
Connected
Disconnected
Sun, 26 Apr 2015 10:30:03 GMT
如果我再次执行素文字(不复位)我从之前的执行中得到日期。 我能做些什么来执行“httpget.lua”并在随后的scipt中获取“日期”?
我使用一个ESP8266与NodeMCU 0.9.6构建20150406由Lua 5.1.4驱动。 https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#index
我使用ESPlorer v2.0通过USB向我的ESP8266加载sripts。 conn.net ...命令是NodeMCU固件的一部分(请参阅链接)。您只能使用EPS8288和NodeMCU固件运行脚本。我的问题是:我无法正确结束conn:net例程并将数据返回到下一个程序部分。
连接调用可能是异步的,但我不知道你用的是什么库。 – ryanpattison
另一方面,您已经定义了事件处理程序,并且print.lua不会等待连接成功,然后继续到下一行。另外,我不知道lua是否会将日期的值传递给不同的范围。 – Kyle
我使用带有由Lua 5.1.4支持的NodeMCU 0.9.6 build 20150406的ESP8266。我使用ESPloer加载脚本。 –