/**
   ESP32 + DHT22 Example for Wokwi
   
   https://wokwi.com/arduino/projects/322410731508073042
*/

// Incluir as bibliotecas...
#include "DHTesp.h"
#include  <WiFi.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros

// Configuração de parâmetros do WiFi
char ssid[] = "Wokwi-GUEST";
char pass[] =  "1234567890abc";
int keyIndex = 0;
WiFiClient  client;

// Configuração dos parâmetros do sensor
const int DHT_PIN = 15;
DHTesp dhtSensor;
float temperatura = 0;
float umidade = 0;

// Declarar o ID do canal
float channel = 1353120;





//Prototipo de funções
void reconectWiFi();
void lerSensorTempAndHumidity();
void escreveCanalThingSpeak();
void escreveCanaisThingSpeak();

//Função de Setup
void setup() {
  Serial.begin(115200);
  
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  
  WiFi.mode(WIFI_STA);   
  
  ThingSpeak.begin(client);  // Initialize ThingSpeak
  
  //WiFi.begin(ssid, pass);
   
  WiFi.begin("Wokwi-GUEST", "", 6);
  
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Tentando conectar-se ao SSID WPA: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.println("Você está conectado à rede");
  //printCurrentNet();
  //printWifiData();
}

// Função principal do programa
void loop() {
  reconectWiFi();
  lerSensorTempAndHumidity();
  //escreveCanalThingSpeak();
  escreveCanaisThingSpeak();
  
}


// Funções....
void reconectWiFi(){
  // Connect or reconnect to WiFi
  if(WiFi.status() != WL_CONNECTED){
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);     
    } 
    Serial.println("\nConnected.");
  }
}

void lerSensorTempAndHumidity(){
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  temperatura = data.temperature;
  umidade = data.humidity;
  Serial.println("Temp: " + String(data.temperature, 2) + "°C");
  Serial.println("Humidity: " + String(data.humidity, 1) + "%");
  Serial.println("---");
  delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)

}

void escreveCanalThingSpeak(){
  // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
  // pieces of information in a channel.  Here, we write to field 1.
  int x = ThingSpeak.writeField(1353120, 1, temperatura, "5ZOK2CEV1YBLHB0G");//(myChannelNumber, 1, number, myWriteAPIKey)
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));

  }
  delay(20000); // Wait 20 seconds to update the channel again
}

void escreveCanaisThingSpeak(){
  /*
  WriteMultipleFields
  
  Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.
  */
  // set the fields with the values
  ThingSpeak.setField(1, temperatura);
  ThingSpeak.setField(2, umidade);

  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(1353120, "5ZOK2CEV1YBLHB0G");//(myChannelNumber, myWriteAPIKey)
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
  delay(20000); // Wait 20 seconds to update the channel again
}

void lerCanal