https request in LUA for DarkSky forecast Topic is solved

Moderator: leecollings

Post Reply
Kifo
Posts: 10
Joined: Wednesday 22 March 2017 12:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

https request in LUA for DarkSky forecast

Post 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)?
alanlsmith
Posts: 132
Joined: Monday 07 August 2017 17:17
Target OS: Linux
Domoticz version: Latest β
Location: Near London (Saaarf!)
Contact:

Re: https request in LUA for DarkSky forecast

Post 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.
Last edited by alanlsmith on Thursday 13 December 2018 18:31, edited 2 times in total.
Domoticz Latest β, RPi 4B with 110Gb SSD for Domoticz, RPi 4B with 110Gb SSD for Node-Red & a RPi 2B for logging / IP addressing. RFXCOM, PiZiGate, Z-Wave, Harmony, Hue lamps and a bit of Broadlink.
Kifo
Posts: 10
Joined: Wednesday 22 March 2017 12:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: https request in LUA for DarkSky forecast

Post by Kifo »

YES! That does the trick.
Thanks a lot, you saved my day (and my big domiticz installation).
EllaCarter
Posts: 1
Joined: Tuesday 28 April 2020 7:46
Target OS: Linux
Domoticz version:
Contact:

Re: https request in LUA for DarkSky forecast

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests