Fetching Json to class member

I basically have multiple APIs (weather, time, etc) which I like to update every hr or so and display..
For whatever reason, the fetched data panics the esp32
H file:

class ShaService {
protected:
  String source_url;
  DynamicJsonDocument *payload;
  int payload_size;

private:
  UpdateRate update_rate;
  u_int32_t updated_at = 0;

  void httpGETRequest();

public:
  ShaService(UpdateRate update_rate, int payload_size, String source_url);
  void display();

  bool needsUpdate();
  void updated();
  virtual void fetch();
};

C file

ShaService::ShaService(UpdateRate update_rate, int payload_size,
                       String source_url) {
  this->update_rate = update_rate;
  this->payload_size = payload_size;
  this->source_url = source_url;
  this->payload = new DynamicJsonDocument(payload_size);
}
void ShaService::fetch() {
  if (this->needsUpdate()) {
    WiFiClientSecure client;
    HTTPClient http;
    debug_print("payload %s\n", this->source_url);
    Serial.println(this->source_url);
    client.setInsecure();
    http.begin(client, this->source_url);

    // Send HTTP GET request
    int httpResponseCode = http.GET();
    if (httpResponseCode > 0) {
      debug_print("HTTP Response code: %d\n", httpResponseCode);
      DeserializationError err =
          deserializeJson(*this->payload, http.getStream());
      if (err) {
        debug_print("Could not undestand the json : %s", err.c_str());
      }
      serializeJsonPretty(*this->payload, Serial); // Works fine
    } else {
      printf("Error code: %d\n", httpResponseCode);
    }
    // Free resources
    http.end();
    updated();
  } else {
    printf("No Need to fetch");
  }
  delay(10 * 1000);
}

panic here ||
\ /

void ShaService::display() {
  Serial.println("Display....\n");
  serializeJsonPretty(*payload, Serial);
}

I assume the error is in that part of the code that you're hiding from us.

Why are you accessing class variables in one method by "this->variable" and in the other by "variable" directly?

You are correct .. The code crashed earlier, but I did not have enough logs to verify ...
Thank you

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