
#include <WiFi.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 display(128, 64, &Wire, -1);

const char* ssid     = "aBcAdfh"; //inserire nome rete WiFi
const char* password = "wyZ23tX"; //inserire password rete WiFi
String APIKEY = "d5g8d4c3380283728b5d22f5d5df8bd99";//inserire codice API OWM
String CityID = "2158177"; //inserire codice località
bool id = false;
WiFiClient client;
char servername[] = "api.openweathermap.org";
String result;

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

  Serial.print("Connessione a ");
  WiFi.mode(WIFI_STA);
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  delay(200);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(20, 0);
  display.print("Connessione");
  display.display();
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    display.print(".");
    display.display();
  }

  Serial.println("");
  Serial.println("INDIRIZZO IP: ");
  Serial.println(WiFi.localIP());
  display.clearDisplay();
  display.setCursor(20, 0);
  display.println("CONNESSO");
  display.println("INDIRIZZO IP:");
  display.println(WiFi.localIP());
  display.display();
  delay(1000);
  display.clearDisplay();

}

void loop()
{
  if (client.connect(servername, 80))
  {
    client.println("GET /data/2.5/weather?id=" + CityID + "&units=metric&APPID=" + APIKEY);
    client.println("Host: api.openweathermap.org");
    client.println("User-Agent: ArduinoWiFi/1.1");
    client.println("Connection: close");
    client.println();
  }
  else {
    Serial.println("connection failed");
    Serial.println();
  }

  while (client.connected() && !client.available())
    delay(1);
  while (client.connected() || client.available())
  { //connected or data available
    char c = client.read();
    result = result + c;
  }

  client.stop();
  result.replace('[', ' ');
  result.replace(']', ' ');
  //Serial.println(result);
  char jsonArray [result.length() + 1];
  result.toCharArray(jsonArray, sizeof(jsonArray));
  jsonArray[result.length() + 1] = '\0';
  StaticJsonDocument<1024> doc;
  DeserializationError  error = deserializeJson(doc, jsonArray);


  if (error) {
    Serial.print(F("deserializeJson() failed with code "));
    Serial.println(error.c_str());
    return;
  }

  String location = doc["name"];
  String country = doc["sys"]["country"];
  int temperature = doc["main"]["temp"];
  int humidity = doc["main"]["humidity"];
  float pressure = doc["main"]["pressure"];
  int id = doc["id"];
  float Speed = doc["wind"]["speed"];
  int degree = doc["wind"]["deg"];
  float longitude = doc["coord"]["lon"];
  float latitude = doc["coord"]["lat"];

  Serial.println();
  Serial.print("PAESE: ");
  Serial.println(country);
  Serial.print("Località: ");
  Serial.println(location);
  Serial.print("ID Località: ");
  Serial.println(id);
  Serial.printf("Temperatura: %d°C\r\n", temperature);
  Serial.printf("Umidità: %d %%\r\n", humidity);
  Serial.printf("Pressione: %.2f hPa\r\n", pressure);
  Serial.printf("Velocità del Vento: %.1f m/s\r\n", Speed);
  Serial.printf("Direzione del Vento: %d°\r\n", degree);
  Serial.printf("Longitudine: %.2f\r\n", longitude);
  Serial.printf("Latitudine: %.2f\r\n", latitude);

  display.clearDisplay();
  display.setCursor(35, 0);
  display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  display.print(country);
  display.print(" ");
  display.println(location);

  display.println();
  display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  display.print("TEMP:");
  display.print(temperature);
  display.print((char)247);
  display.print("C");
  display.print("    UMID:");
  display.print(humidity);
  display.println("% ");
  display.setCursor(0, 30);
  display.print("PRESSIONE:");
  display.print(pressure);
  display.println(" hPa");
  display.setCursor(0, 43);
  display.print("VV:");
  display.print(Speed);
  display.print(" m/s");
  display.print("   DV:");
  display.print(degree);
  display.println((char)247);
  display.setCursor(0, 56);
  display.print("LAT:");
  display.print(latitude);
  display.print(" ");
  display.print("  LON:");
  display.println(longitude);
  display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
  display.display();

  delay(600000);
}
