#include "thingProperties.h"
#include "DHT.h"

#define DHTPIN 16 // GPIO that is connected to the sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  dht.begin();

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here

  leituraSensor();

  Serial.println();
  Serial.print("Umidade: ");
  Serial.print(umidade);
  Serial.print("%");
  Serial.print(" | ");
  Serial.print("Temperatura: ");
  Serial.print(temperatura);
  Serial.print("°C");
  delay(5000);
}

void leituraSensor(){
  float t = dht.readTemperature();
  float u = dht.readHumidity();
   if (isnan(u) || isnan(t)) {
    Serial.println(F("Falha ao ler os dados do sensor!"));
    return;
   }
  temperatura = t;
  umidade = u;
  //delay(5000);
}