Weather API Tutorial

The new Weather API Tutorial shows how to use the OpenWeather API to get current condition and forecast data using the (usually free) Open Weather API, use Webhooks to extract only the data you are interested in, and send this to your Particle device.

It also has an API request and response viewer like in the Cloud API Tutorial so you can better understand the requests and responses.

In order to select which fields you want to send to your device, you just click the checkboxes. For example: temperature, pressure, and humidity. I also selected 3 days of day temp and night temp.

Then it automatically creates your webhook for you!

{
  "url": "https://api.openweathermap.org/data/2.5/onecall",
  "noDefaults": true,
  "rejectUnauthorized": true,
  "event": "GetWeatherData",
  "requestType": "GET",
  "responseTopic": "{{PARTICLE_DEVICE_ID}}/{{PARTICLE_EVENT_NAME}}",
  "query": {
    "lat": "42.999999",
    "lon": "-75.999999",
    "exclude": "minutely,hourly,alerts",
    "units": "metric",
    "appid": "xxx"
  },
  "responseTemplate": "{\"temp\":{{current.temp}},\"pres\":{{current.pressure}},\"hum\":{{current.humidity}},\"daily\":[{\"day\":{{daily.0.temp.day}},\"night\":{{daily.0.temp.night}} },{\"day\":{{daily.1.temp.day}},\"night\":{{daily.1.temp.night}} },{\"day\":{{daily.2.temp.day}},\"night\":{{daily.2.temp.night}} }] }"
}

It also generates sample code for parsing the abbreviated response data on your device using the built-in JSON parser:

void subscriptionHandler(const char *event, const char *data) {
    JSONValue outerObj = JSONValue::parseCopy(data);
    JSONObjectIterator iter(outerObj);
    while(iter.next()) {
        if (iter.name() == "temp") {
            Log.info("temp=%lf", iter.value().toDouble());
        }
        if (iter.name() == "pres") {
            Log.info("pressure=%lf", iter.value().toDouble());
        }
        if (iter.name() == "hum") {
            Log.info("humidity=%lf", iter.value().toDouble());
        }
        if (iter.name() == "daily") {
            JSONArrayIterator iter2(iter.value());
            for(size_t ii = 0; iter2.next(); ii++) {
                Log.info("daily array index %u", ii);
                JSONObjectIterator iter3(iter2.value());
                while(iter3.next()) {
                    if (iter3.name() == "day") {
                        Log.info("temp.day=%lf", iter3.value().toDouble());
                    }
                    if (iter3.name() == "night") {
                        Log.info("temp.night=%lf", iter3.value().toDouble());
                    }
                }
            }
        }
    }
}
9 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.