我想要使用具有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加載腳本。 –