
#include "OpenWeatherMapAPI.h"
#include "WeatherForecastSample.cpp"
#include "OLEDScreen.h"

const char *ssid = "aBcAdfh";//inserire nome rete WiFi
const char *password = "wyZ23tX";//inserire password rete WiFi
const String query = "Milano,it"; 
const String apiKey = "d5g8d4c3380283728b5d22f5d5df8bd9";

const uint64_t updateInterval = 1000 * 3600 * 2; 
uint64_t lastUpdate = 0;

void setup()
{
  Serial.begin(115200);

  OpenWeatherMapAPI::init(ssid, password);
  OLEDScreen::init();
}

void loop() {
  WeatherForecastSample* data;
  int count = 0;
  bool status = OpenWeatherMapAPI::getForecast(query, apiKey, &data, &count);
  if (!status) {
    Serial.println("An error occured");
    while (true) {}
  }
  
  lastUpdate = millis();

  while (true) {
    // time to update forecast data
    if (lastUpdate + updateInterval < millis()) {
      OpenWeatherMapAPI::getForecast(query, apiKey, &data, &count);
      lastUpdate = millis();
    }

    for (int i = 0; i < count; i++) {
      OLEDScreen::printForecast(data[i]);
      delay(5000);
    }
  }
}
