- Sat Apr 04, 2015 12:53 pm
#13478
Moin Peter,
you are mixing up a few things.
1. synchronous calls versus asynchronous calls
synchronous: you call a function and get a result
asynchronous: you tell lua to call you back when something happens (called nodejs style on the example page)
Your function content is using asynch but you seem to expect a sync result.
Normally you would want a synch call. But unfortunately I don't know how to do that on nodemcu
and didn't find an example.
If you want to do it async put your action call into the "receive" callback you install.
If someone knows I would like to know, too!
2. When you call a function 'getURLdate("esp8266.com")' the function is called but you don't do anything
with the result. Either add a "=" before the call or do a "print(...)" of the value.
But before that you have to fix 1.) because you don't have a result.
Hope that helps a least a little bit,
Carsten
katz wrote:Hi,
The code below almost does what I want it to do. I can get the date and time from an external website (esp8266.com in this case). But I cannot access the local variable 'dtm' outside the function. The last two lines in the code should give the same result, but they don't. Is there a way around this?
Thanks, Peter.
Code: Select allgdtm="xxx"
function getURLdate(url)
sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, reply)
--print(reply)
i=string.find(reply,"Date: ")
dtm=string.sub(reply,i+6,i+35)
_G[gdtm]=dtm -- make sure that dtm is global
print("Local date = "..dtm)
sk:close()
end )
sk:send("GET / HTTP/1.1\r\nHost: "..url.."\r\n\r\n")
sk:connect(80,url)
end
getURLdate("esp8266.com")
print("Global date = "..gdtm)