Using Openweathermap with Electric Imp

I have not yet found a use for this, but in case anyone else could use it, or maybe I could use it myself later, I am going to post the function here..

OPENWEATHERMAP_ZONE <- "Kolding,DK";
function openWeatherMap()
{
  local url = "http://api.openweathermap.org/data/2.5/weather?q=" + OPENWEATHERMAP_ZONE + "&mode=json&units=metric";
  http.get(url).sendasync(function(resp)
  {
    local data = http.jsondecode(resp.body);
    
    if ("message" in data)
    {
      server.log("OpenWeatherMap returned: " + resp.body);
    }
    else
    {
      server.log("main.temp: " + data.main.temp);
      server.log("main.temp_min: " + data.main.temp_min);
      server.log("main.temp_max: " + data.main.temp_max);
      server.log("main.pressure: " + data.main.pressure);
      server.log("main.humidity: " + data.main.humidity);
      
      server.log("wind.speed: " + data.wind.speed);
      server.log("wind.deg: " + data.wind.deg);
      server.log("wind.var_beg: " + data.wind.var_beg);
      server.log("wind.var_end: " + data.wind.var_end);
      
      server.log("weather.main: " + data.weather[0].main);
      server.log("weather.description: " + data.weather[0].description);
      server.log("weather.icon: " + data.weather[0].icon);
      server.log("weather.id: " + data.weather[0].id);
      
      server.log("data.name: " + data.name);
      server.log("data.dt: " + data.dt);
      
      server.log("clouds.all: " + data.clouds.all);
      
      server.log("sys.country: " + data.sys.country);
      server.log("sys.sunrise: " + data.sys.sunrise);
      server.log("sys.sunset: " + data.sys.sunset);
    }
  });
}
openWeatherMap();

This should return something like

2014-04-18 15:23:15 UTC+2: [Agent] main.temp: 10
2014-04-18 15:23:15 UTC+2: [Agent] main.temp_min: 10
2014-04-18 15:23:15 UTC+2: [Agent] main.temp_max: 10
2014-04-18 15:23:15 UTC+2: [Agent] main.pressure: 1011
2014-04-18 15:23:15 UTC+2: [Agent] main.humidity: 61
2014-04-18 15:23:15 UTC+2: [Agent] wind.speed: 4.1
2014-04-18 15:23:15 UTC+2: [Agent] wind.deg: 360
2014-04-18 15:23:15 UTC+2: [Agent] wind.var_beg: 310
2014-04-18 15:23:15 UTC+2: [Agent] wind.var_end: 60
2014-04-18 15:23:15 UTC+2: [Agent] weather.main: Clouds
2014-04-18 15:23:15 UTC+2: [Agent] weather.description: broken clouds
2014-04-18 15:23:15 UTC+2: [Agent] weather.icon: 04d
2014-04-18 15:23:15 UTC+2: [Agent] weather.id: 803
2014-04-18 15:23:15 UTC+2: [Agent] data.name: Kolding
2014-04-18 15:23:15 UTC+2: [Agent] data.dt: 1397821800
2014-04-18 15:23:15 UTC+2: [Agent] clouds.all: 75
2014-04-18 15:23:15 UTC+2: [Agent] sys.country: Denmark
2014-04-18 15:23:15 UTC+2: [Agent] sys.sunrise: 1397794132
2014-04-18 15:23:15 UTC+2: [Agent] sys.sunset: 1397846030

I hope anyone find a use for it. 🙂

Leave a Reply