Welcome to Smart Home Forum by FIBARO
Dear Guest,
as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.
As a member you can:
- Start new topics and reply to others
- Follow topics and users to get email updates
- Get your own profile page and make new friends
- Send personal messages
- ... and learn a lot about our system!
Regards,
Smart Home Forum by FIBARO Team
Question
FamSetsaas 0
Dear experts,
I'm trying to do an HTTP call from LUA, to retrieve the external IP address. However, it is obvious that I need to wait for the response, but have not been able to find out exactly where to do this. Here is my code:
function myPublicIP(str)
fibaro:debug("A")
str = "https://api.ipify.org/?format=json"
local http = net.HTTPClient()
fibaro:debug("B")
http:request(str, {
success = function(status)
fibaro:debug("C")
fibaro:debug(status.status)
if status.status ~= 200 and status.status ~= 201 then fibaro:debug("failed"); end
fibaro:debug("D")
fibaro:debug(status.data);
result = json.decode(status.data)
ip = result.ip
fibaro:debug("E")
fibaro:debug(ip)
end,
error = function(err)
fibaro:debug('[ERROR] ' .. err)
end
})
fibaro:debug("F")
return ip
end -- function
fibaro:debug("G")
fibaro:debug(myPublicIP())
When running this, the debug output after "E" shows that the IP address is retrieved correctly. But this comes too late, and the function returns nil. So there is a need for a sleep (which I guess is the simplest) somewhere, but have not figured this out.
I do not really understand how the code is executed. The letters are output in this order: G A B F C D E. So I just need to ensure that C D E are completed before continuing to F.
Any help would be appreciated.
Link to comment
Share on other sites
8 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.