OpenWeatherMap onecall - api

Hi,

I am trying to use https://api.openweathermap.org/data/2.5/onecall api to display some info, but even if I am using "exclude"=>"minutely,hourly,daily,alerts" seems to be very heavy sometimes.

No all call are working and the info is not refreshed.

Do you have some suggestions using OneCall from OpenWeatherMap to retrieve correct info?

Thanks

  • I published here my code.

    Currently I don't know what else to do

  • It's the same in the sim.  And it's not really a problem, as the data could be older that the current time, and how much older can be based on the specific weather station(s).

    My main weather source is Weather Underground.  In the case of my own weather station, it sends updates every 15 minutes.  The station down the block looks to be about once an hour.

    Garmin weather looks to updated every 20 minutes with Android, and once an hour with iOS last I checked.

  • using Toybox.Application as App;

    function onTemporalEvent()
    {
    var AK = App.getApp().getProperty("WeatherApi");
    var a = App.getApp().getProperty("a");
    var b = App.getApp().getProperty("b");
    if(AK != null && a != null && b != null)
    {
    Comm.makeWebRequest
    (
    "">api.openweathermap.org/.../onecall",
    {
    "lat" =>a.toFloat(),
    "lon"=>b.toFloat(),
    "exclude"=>"minutely,hourly,daily,alerts",
    "appid"=>AK,
    "units" => (Sys.getDeviceSettings().temperatureUnits==Sys.UNIT_STATUTE) ? "imperial" : "metric"},
    {
    :methods => 1,
    :headers => {"Content-Type" => 0},
    :responseType => 0
    },
    method(:receiveWeather)
    );
    }
    }

  • hi, yes I've tested it also Android=20min / Iphone= 1h

  • this is the code from onBackgroundData

    function onBackgroundData(data) 
    	{
    		if(data != null)
    		{
    			System.println(data);
    			if(data[0] != null)
    			{
    				Application.getApp().setProperty("temperature",data[0]);
    			}
    			if(data[1] != null)
    			{
    				Application.getApp().setProperty("icon",data[1]);
    			}
    			if(data[2] != null)
    			{
    				Application.getApp().setProperty("uvi",data[2]);
    			}
            }
            
            WatchUi.requestUpdate();
        } 

  • you could have it lighter like

    if(data!=null){

    Application.getApp().setProperty("Weather",data);

    }

  • One thing I consistently do regardless of the API, in the background for onReceive() here's my basic code:

    function onReceive(responseCode,data) {
      if(responseCode!=200) {return responseCode;}
      else {return data;}
    }

    So if there is an error, I see that, otherwise the data.  Then in onBackgroundData()

    function onBackgroundData(data) {
        if(data instanceof Number) {
            //handle the error, maybe display it?
        } else {
            //got data and do what's needed
        }
    }

  • @tak79

    If you want to know why it fails, you have to look at response code of the request (as others already mentioned). And let it run long time.
    Don't use System.println here, it might increase memory usage (as I've read).
    In simulator open the Http Traffic Logs window, CTRL+H on Windows, or Simulator window, File Menu, then "View HTTP Traffic", and you will see detailed info of headers and body.

    OWM says that current weather data is refreshed every 10 minutes for their "Current Weather Data" API. No info about One Call API, but I assume it's same. However, it does not mean that there really is new data every 10 minutes.

    This is how a typical response look like in Traffic Logs window, using in Alfa Zulu.
    It often happens that only "dt", the timestamp changes, while other data is same.

    {"lat":99.3597,"lon":99.8994,"timezone":"Europe/Berlin","timezone_offset":7200,"current":{"dt":1624468614,"sunrise":1624418138,"sunset":1624476091,"temp":22.9,"feels_like":22.97,"pressure":1016,"humidity":66,"dew_point":16.22,"uvi":0.71,"clouds":40,"visibility":10000,"wind_speed":4.63,"wind_deg":60,"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}]}}

    "dt" is the time when the request was made, btw.

  • it seems that One Call api works only with a subscription from now