/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/7946428b-c804-4d4e-8c9b-c1d9f2e73f64

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudTemperatureSensor temperatura;
  CloudRelativeHumidity umidade;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

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

#define DHTPIN 15 // 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);
}