Lua脚本不按顺序执行

Lua脚本不按顺序执行,lua,esp8266,nodemcu,esplorer,Lua,Esp8266,Nodemcu,Esplorer,我想使用带有nodeMCU的EPS8266来设置I2C上的RTC 这是我的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

我想使用带有nodeMCU的EPS8266来设置I2C上的RTC

这是我的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
如果我再次执行scipt(不重置),我将从之前的执行中获取日期。 如何执行“httpget.lua”并在随后的scipt中获取“日期”


我使用的ESP8266带有NodeMCU 0.9.6构建20150406,由Lua 5.1.4提供动力


我通过带有ESPlorer v2.0的USB将SRIPT加载到我的ESP8266。conn.net。。。命令是NodeMCU固件的一部分(请参阅链接)。您只能使用EPS8288和NodeMCU固件运行脚本。我的问题是:我无法正确结束conn:net例程并将数据返回到下一个程序部分。

正如评论员指出的那样,网络代码将异步运行,即
conn:on
调用将立即返回,并在稍后调用它们的回调。
conn:connect
调用可能不是异步的,但这没有帮助

conn:connect
调用完成后,您的
print
调用将立即运行,尝试打印全局变量
date
。大多数情况下,这将打印
nil
,因为从Google获取数据的网络延迟将在>10毫秒内,这意味着您的代码已经有足够的时间执行打印语句。在一些罕见的情况下,如果你真的很幸运有网络延迟的话,你可能会得到正确的日期(尽管这会非常令人惊讶)

要解决此问题,您需要将完成网络请求时要执行的代码放入一个回调中,然后将回调传递给接收数据的
conn:on
。但是,在您当前的代码结构中,用一种好的方式来实现这一点有点困难

一个简单的解决方案是:

local function onReceiveCb(str)
 print("Print follows:")
 print(str)
end
local file = assert(loadfile("httpget.lua"))
....
注意,在包含httpget代码之前,我已经添加了一个onReceiveCb
函数。在httpget中,调用回调:

conn:on("receive", function(conn, payload) 
                     date = string.sub(payload,string.find(payload,"Date: ")
                     +6,string.find(payload,"Date: ")+35)
                     conn:close()
                     onReceiveCb(date) -- Call the callback!
                     end) 

带有回调函数的建议无效。我有一个编译器错误。我现在用另一种方式解决了这个问题。 在conn:on(“断开”,函数(conn,payload)中,我加载文件以设置RTC。这样,我可以将数据传递给设置RTC的程序。(请参阅我的输出)

谢谢你的帮助!!!

> dofile("httpget.lua");
httpget.lua started
> 
Connected
Disconnected

----------------
Date: Mon, 27 Apr 2015 12:02:17 GMT -- printed in httpget.lua
Date: Mon, 27 Apr 2015 12:02:17 GMT -- printed in set_date.lua
Set RTC:
23 2 19 2 39 4 21  -- Bytes 0-6 in decimal of the DS1307 (1h for Daylight Savings Time added)
done
——这是工作脚本:

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: ")
                     +0,string.find(payload,"Date: ")+35)
                     conn:close()
                     end) 

-- when connected, request page (send parameters to a script)
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")
                         print("----------------")
                         print(date)
                         dofile("set_date.lua");
                         end)

conn:connect(80,'google.com')
conn=nil

connect调用可能是异步的,但我不知道您使用的是什么库。现在,您已经定义了事件处理程序并打印了。lua不会等到连接成功后再继续下一行。此外,我也不知道lua是否会将date值传递到不同的作用域。我使用了一个带有NodeMCU 0.9.6构建的ESP826620150406由Lua 5.1.4提供动力。我用Esplore加载纸条。谷歌nodemcu非官方常见问题解答并阅读。很好,你解决了!你可以将自己的答案标记为正确答案(因此显示为已回答)。如果我的答案有帮助,请投上一票。