【发布时间】:2015-07-04 19:21:50
【问题描述】:
我想花时间使用带有 nodeMCU 的 EPS8266 来设置我的 RTC over I2C。
这是我的剧本:
-- 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
如果我再次执行 scipt(没有重置),我会从之前的执行中获取日期。 如何执行“httpget.lua”并在后面的 scipt 中获取“日期”?
我使用 ESP8266 和 NodeMCU 0.9.6 build 20150406,由 Lua 5.1.4 提供支持。 https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#index
我通过带有 ESPlorer v2.0 的 USB 将脚本加载到我的 ESP8266。 conn.net... 命令是 NodeMCU 固件的一部分(参见链接)。您只能使用 EPS8288 和 NodeMCU 固件运行脚本。我的问题是:我找不到正确结束 conn:net 例程并将数据返回到下一个程序部分的方法。
【问题讨论】:
-
connect调用可能是异步的,但我不知道你用的是什么库。
-
顺便说一句,您已经定义了事件处理程序,并且 print.lua 在继续下一行之前不会等待连接成功。另外,我不知道lua是否会将date的值传递到不同的范围内。
-
我使用 ESP8266 和 NodeMCU 0.9.6 build 20150406,由 Lua 5.1.4 提供支持。我用 ESPloer 加载脚本。
-
Google nodemcu 非官方常见问题解答并阅读。
标签: lua esp8266 nodemcu esplorer