//Librerias
#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
//Puertos GPIO
int led=32;
//DHT22
DHT dht(33, DHT22); //DHTPIN, DHTTYPE
//Variables status
int valor_temperatura=0;
int valor_humedad=0;
//Thingspeak
String url="https://api.thingspeak.com/update?"; //Server URL
String api_key="G3LEWEYWEHRETHSI"; //API key personal del tablero
String sensor="field1";
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);// Configurar puerto 2 como salida
dht.begin();//Inicializamos DHT
delay(1000);
Serial.begin(115200);
//Conect to wifi network
WiFi.begin("Wokwi-GUEST", ""); //Wifi_Network, Wifi_Password ******CHANGE
//Wait until connection
while((WiFi.status() != WL_CONNECTED)) {
delay(500);
Serial.print(".");
}
Serial.println("Wifi conected");
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
valor_temperatura=dht.readTemperature();
valor_humedad=dht.readHumidity();
Serial.print("Temperatura:");
Serial.println(valor_temperatura);
Serial.print("Humedad:");
Serial.println(valor_humedad);
if(valor_humedad>=50){ //Si la temperatura es mayor a 50%
digitalWrite(led,HIGH);
}
else{
digitalWrite(led,LOW);
}
// wait for WiFi connection
if((WiFi.status() == WL_CONNECTED)) {
// start connection and send HTTP header, return error code
HTTPClient http;
String message=url+"api_key="+api_key+"&"+sensor+"="+String(valor_humedad);
http.begin(message); //HTTP
int httpCode = http.GET();
Serial.println(message);
Serial.print("GET code:");
Serial.println(httpCode);
http.end();
}
delay(1000);
}