This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

Weather service not working anymore?
#61
See this post: https://forum.logicmachine.net/showthrea...5#pid12735
Reply
#62
(02.05.2019, 12:51)jetsetter Wrote: Hi to all,

I tried Michell's code (with either his or my city code) and I am always getting an alert:

Fetch failed: error:1409442E:lib(20):func(148):reason(1070)

and the script stops. I tried to find any error description in yr.no but no luck.

Any ideas?
Thank you in advance.

Did you put https in the url? 
The script works only with http.

Regards.
Michel.
Reply
#63
(18.01.2019, 07:50)admin Wrote: Since there's no response from Yahoo we suggest using https://apixu.com instead. You will have to create a free account and make sure that your script does not fetch data too quickly (not less than once in 5 minutes).

Here's a short example, change key variable to your API key, location variable to required location in textual form. This example fetches current weather condition and forecast for today and tomorrow. Fields can be found in apixu documentation: https://www.apixu.com/doc/forecast.aspx (day Element).

Code:
require('json')
https = require('ssl.https')
escape = require('socket.url').escape

key = 'my_api_key'
location = 'my location name'

url = 'https://api.apixu.com/v1/forecast.json?key=%s&q=%s&days=2'
url = string.format(url, key, escape(location))

res = https.request(url)

data = json.pdecode(res)
if type(data) ~= 'table' then
  alert('failed to load weather data')
  return
end

if data.error then
  log('error', data.error)
  return
end

current = data.current
today = data.forecast.forecastday[ 1 ].day
tomorrow = data.forecast.forecastday[ 2 ].day

-- log(current, today, tomorrow)

-- temperature in C
grp.write('32/1/1', current.temp_c)
-- "feels like" temperature in C
grp.write('32/1/2', current.feelslike_c)
-- humidity as percentage
grp.write('32/1/3', current.humidity)
-- wind speed in kilometers per hour
grp.write('32/1/4', current.wind_kph)
-- uv index
grp.write('32/1/5', current.uv)
-- weather condition text
grp.write('32/1/6', current.condition.text)
-- pressure in millibars
grp.write('32/1/7', current.pressure_mb)
-- precipitation amount in millimeters
grp.write('32/1/8', current.precip_mm)

-- minimum temperature in celsius for the day
grp.write('32/2/1', today.mintemp_c)
-- maximum temperature in celsius for the day
grp.write('32/2/2', today.maxtemp_c)
-- average temperature in celsius for the day
grp.write('32/2/3', today.avgtemp_c)
-- average humidity as percentage
grp.write('32/2/4', today.avghumidity)
-- maximum wind speed in kilometers per hour
grp.write('32/2/5', today.maxwind_kph)
-- uv index
grp.write('32/2/6', today.uv)
-- weather condition text
grp.write('32/2/7', today.condition.text)
-- total precipitation in millimeters
grp.write('32/2/8', today.totalprecip_mm)

-- minimum temperature in celsius for the day
grp.write('32/3/1', tomorrow.mintemp_c)
-- maximum temperature in celsius for the day
grp.write('32/3/2', tomorrow.maxtemp_c)
-- average temperature in celsius for the day
grp.write('32/3/3', tomorrow.avgtemp_c)
-- average humidity as percentage
grp.write('32/3/4', tomorrow.avghumidity)
-- maximum wind speed in kilometers per hour
grp.write('32/3/5', tomorrow.maxwind_kph)
-- uv index
grp.write('32/3/6', tomorrow.uv)
-- weather condition text
grp.write('32/3/7', tomorrow.condition.text)
-- total precipitation in millimeters
grp.write('32/3/8', tomorrow.totalprecip_mm)
Now i get always the alert failed to load weather data.
What im doing wrong ?
The same script has worked before.
Is there something changed in the apixu ?

Reply
#64
Works for me, try logging server response.

Replace:
Code:
res = https.request(url)

With:
Code:
res, code, hdrs, stat = https.request(url)
log(res, code, hdrs, stat)

And post what you get in Logs tab.
Reply
#65
(29.08.2019, 06:43)admin Wrote: Works for me, try logging server response.

Replace:
Code:
res = https.request(url)

With:
Code:
res, code, hdrs, stat = https.request(url)
log(res, code, hdrs, stat)

And post what you get in Logs tab.
Thanks
This is what i get : 

weater_data_apixu
29.08.2019 20:49:37
* arg: 1
  * nil
* arg: 2
  * nil
* arg: 3
  * nil
* arg: 4
  * nil
Reply
#66
Which firmware version are you running? Can you send your Apixu key via PM?
Reply
#67
(29.08.2019, 19:24)admin Wrote: Which firmware version are you running? Can you send your Apixu key via PM?
Version: 20170620
Reply
#68
This is probably due to older FW, as a work-around you can use HTTP request instead:

Code:
require('json')
http = require('socket.http')
escape = require('socket.url').escape

key = 'my_api_key'
location = 'my location name'

url = 'http://api.apixu.com/v1/forecast.json?key=%s&q=%s&days=2'
url = string.format(url, key, escape(location))

res = http.request(url)

data = json.pdecode(res)
...
Reply


Forum Jump: