/*
   Código demonstrativo para a aula de Sistemas Embarcados,
   Curso de Ciência da Computação e Engenharia da Computação
   EEP.
 
   https://arduinojson.org/v6/assistant/

*/

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>

// Substituir nome_dispositivo pelo nome do seu dispositivo.
// const String payload = "http://dweet.io/get/latest/dweet/for/nome_dispositivo";

const String payload = "http://dweet.io/get/latest/dweet/for/nome_dispositivo";

LiquidCrystal_I2C lcd(0x27, 16, 2);

HTTPClient cliente;

void setup() {

  WiFi.begin("Wokwi-GUEST", "");

  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
  }

  lcd.init();
  lcd.backlight();
  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print(" Escola de Eng. ");
  lcd.setCursor(0, 1);
  lcd.print(" de  Piracicaba ");

  delay(3000);

  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("    Sistemas    ");
  lcd.setCursor(0, 1);
  lcd.print("   Embarcados   ");

  delay(3000);

  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("   Exercicio    ");
  lcd.setCursor(0, 1);
  lcd.print("----------------");

  delay(3000);

}

void loop() {

  cliente.begin(payload);

  int statusCode = cliente.GET();
  String response = cliente.getString();

  StaticJsonDocument<512> doc;

  DeserializationError error = deserializeJson(doc, response);

  if (error) {
    return;
  }

  const char* this_ = doc["this"];
  const char* by_ = doc["by"];
  const char* the_ = doc["the"];

  JsonObject with_0 = doc["with"][0];
  const char* with_0_thing = with_0["thing"];
  const char* with_0_created = with_0["created"];

  JsonObject with_0_content = with_0["content"];
  int with_0_content_TempC = with_0_content["TempC"];
  int with_0_content_TempF = with_0_content["TempF"];
  int with_0_content_Cen = with_0_content["Cen"];
  int with_0_content_Pol = with_0_content["Pol"];

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temperatura (C) ");
  lcd.setCursor(0, 1);
  lcd.print(with_0_content_TempC);

  delay(3000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temperatura (F) ");
  lcd.setCursor(0, 1);
  lcd.print(with_0_content_TempF);

  delay(3000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Distancia (Cm) ");
  lcd.setCursor(0, 1);
  lcd.print(with_0_content_Cen);

  delay(3000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Distancia (Pol) ");
  lcd.setCursor(0, 1);
  lcd.print(with_0_content_Pol);

  delay(3000);

}