Page 1 of 1

https request in LUA for DarkSky forecast

Posted: Wednesday 12 December 2018 19:01
by Kifo
As Weather Underground will stop its free service soon I am trying to adapt my lua scripts to use forecast data from DarkSky.

The DarkSky API works only with https requests. Thus I only get the reply "Bad Request" instead of the JSON reply if I use the following code to access the DarkSky forecast.

Code: Select all

    io = require('io')
    http = require('socket.http')
    json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
    reply = http.request("https://api.darksky.net/forecast/[APIcode]/[lat],[lon]?units=si")
    print(reply)
Of course I replaced the [] section with my own data.
How can I make a https request in lua (I am still using Domoticz V3.5877 for other compatibility reasons)?

Re: https request in LUA for DarkSky forecast

Posted: Wednesday 12 December 2018 19:48
by alanlsmith
if it helps this is the code snippet that collects the data from the script I use:

Code: Select all


function os_capture(cmd, raw)
    local f = assert(io.popen(cmd, 'r'))
    local s = assert(f:read('*a'))
    f:close()
    if raw then return s end
    s = string.gsub(s, '^%s+', '')
    s = string.gsub(s, '%s+$', '')
    s = string.gsub(s, '[\n\r]+', ' ')
    return s
end

local api_key="mykey" -- darksky secret key
local coord="lat, long"                 -- latitude,longitude

-- Querying Darksky --
json = (loadfile "/home/alan/domoticz/scripts/lua/JSON.lua")()  -- For Linux
result=os_capture('curl -s "https://api.darksky.net/forecast/'..api_key..'/'..coord..'?lang=en&units=ca&exclude=hourly,flags"')
Edit: First line edited for grammar.

Re: https request in LUA for DarkSky forecast

Posted: Thursday 13 December 2018 17:03
by Kifo
YES! That does the trick.
Thanks a lot, you saved my day (and my big domiticz installation).

Re: https request in LUA for DarkSky forecast

Posted: Tuesday 28 April 2020 13:12
by EllaCarter
Weather forecast is doe for the dark and depth of the sky. All the issues of the skies and buy custom essays are fully equipped for the forecast for the matters and all top class of the joys for the tam. This is enduring for the flexible means for humans.