Retry download after 'null' value

I have a weather station that downloads a weather API every five minutes and parses the data for a local display and an upload to a weather site.
the following is where I connect to the API and download the data. Occasionally, although the connection is successful, the API fails to download completely.
My question is, can I make the program retry the download if a 'null' value is returned in any of the fields?

void makehttpRequest() {

  DynamicJsonDocument  doc(4500);
  DynamicJsonDocument filter(5000);
  filter["current"] = true;
  filter["hourly"][0]["weather"][0]["description"] = true;
  filter["daily"][0]["moon_phase"] = true;
  filter["daily"][0]["weather"][0]["description"] = true;

  client.stop();
  if (client.connect(server, 80)) {
    Serial.println("HTTP connecting...");
    client.println("GET /data/2.5/onecall?lat=xx.4976&lon=-xx.1689&exclude=minutely,alerts&units=metric&appid=xx xx xx xx xx");
    client.println("Host: api.openweathermap.org");
    client.println("User-Agent: ArduinoWiFi/1.1");
    client.println("Connection: close");
    client.println();
    Serial.println("Success ");

    unsigned long timeout = millis();
    while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
    }
    deserializeJson(doc, client, DeserializationOption::Filter(filter));
    String day_1 = doc["daily"][1]["weather"][0]["description"];
    hold_day_1 = day_1;
    String day_2 = doc["daily"][2]["weather"][0]["description"];
    hold_day_2 = day_2;
    String day_3 = doc["daily"][3]["weather"][0]["description"];
    hold_day_3 = day_3;
    String day_4 = doc["daily"][4]["weather"][0]["description"];
    hold_day_4 = day_4;
    String day_5 = doc["daily"][5]["weather"][0]["description"];
    hold_day_5 = day_5;
    String hour_1 = doc["hourly"][0]["weather"][0]["description"];
    hold_hour_1 = hour_1;
    String hour_2 = doc["hourly"][1]["weather"][0]["description"];
    hold_hour_2 = hour_2;
    String hour_3 = doc["hourly"][2]["weather"][0]["description"];
    hold_hour_3 = hour_3;
    String hour_4 = doc["hourly"][3]["weather"][0]["description"];
    hold_hour_4 = hour_4;
    String hour_5 = doc["hourly"][4]["weather"][0]["description"];
    hold_hour_5 = hour_5;
    uint32_t api_call = doc["current"]["dt"];
    uint32_t weather_id = doc["current"]["weather"][0]["id"];
    String weather_description = doc["current"]["weather"][0]["description"];
    uint32_t sunrise = doc["current"]["sunrise"];
    uint32_t sunset = doc["current"]["sunset"];
    float wind_speed = doc["current"]["wind_speed"];
    int wind_deg = doc["current"]["wind_deg"];
    float moon_phase = doc["daily"][0]["moon_phase"];

Sure … just test for the error and run a query again, may be after a small random time to avoid sending too many requests to the server (some API limit the incoming requests / perform throttling)

The problem is, Arduino reports a 'null' value but I don't know what value is actually getting sent. I have tried '0' (zero) but this does not work.

Before doing thing like

doc["daily"][1]["weather"][0]["description"]

You need to make sure every element exist. Read this

See the example code where he mentions

Because ArduinoJson implements the Null Object Pattern, it is always safe to read the object: if the key doesn’t exist, it returns an empty value.

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