#include <WiFi.h>
#include <HTTPClient.h>
#include "DHTesp.h"
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String url = "https://graph.facebook.com/v18.0/261529630385508/messages";
//Decaramos el variable que almacena el pin a conectar el DHT11
int pinDHT = 15;
//Instanciamos el DHT
DHTesp dht;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
//Inicializamos el dht
dht.setup(pinDHT, DHTesp::DHT22);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
}
void loop() {
//Obtenemos el arreglo de datos (humedad y temperatura)
TempAndHumidity data = dht.getTempAndHumidity();
int temperatura = 22;
int humedad = 50;
if(temperatura < 30 && humedad < 70){
Serial.print("Sending POST request to " + url + "... ");
HTTPClient http;
http.begin(url);
// Configurar la solicitud como POST
http.addHeader("Authorization","Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJEZXZpY2VDYWxsYmFja19zZTAxIiwic3ZyIjoidXMtZWFzdC5hd3MudGhpbmdlci5pbyIsInVzciI6InBhYmxvYnlyb25waW5sb3phbm8ifQ.454X3W_dRh-HDBC0JciS0HkPcAqXhKTdcGuLRUCWaU4");
http.addHeader("Content-Type", "application/json"); // Puedes ajustar el tipo de contenido según sea necesario
// Aquí puedes configurar el cuerpo de la solicitud POST, por ejemplo, un JSON
String postData = "{\"temperature\":" + String(data.temperature, 2) + ",\"humidity\":" + String(data.humidity, 1) + "}";
// Realizar la solicitud POST
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
http.end(); // Cerrar la conexión después de la solicitud
delay(5000);
}
if(temperatura > 30 || humedad < 70){
Serial.println(String(data.temperature, 2));
Serial.println(String(data.humidity, 1));
//COLOCAMOS EL TOKEN QUE NOS ENTREGA META
String token="Bearer EAANrq982w9kBO0pexh9KVa6hZA2MZBIZCFqpbkmrbmZAdDyNZAy6fEflQFqVaFeZBvnJsn1NLGC1OA0bDBbeU4ZAo2JewiZB7yC1NjVMmPRNOhT3hbOOdTZBNmZCZBZArPsJY4S9Wpc8ElhlNuzzuzVAL40XjumdrIciVpk2Yv8AmqNR7tgkVSalI4eKHmcZCHweIT6ZBmGW8RYg6L7ZARJhQKUTGMZD";
//COLOCAMOS LA URL A DONDE SE ENVIAN LOS MENSAJES DE WHATSAPP
String servidor = "https://graph.facebook.com/v18.0/261529630385508/messages";
//CREAMOS UNA JSON DONDE SE COLOCA EL NUMERO DE TELEFONO Y EL MENSAJE
String payload = "{\"messaging_product\":\"whatsapp\",\"to\":\"541131298020\",\"type\":\"text\",\"text\": {\"body\": \"Hay alteracion de la humedad y temperatura\"}}";
Serial.println("Hay alteraciones en la temperatura o humedad");
delay(5000);
//INICIAMOS EL OBJETO HTTP QUE POSTERIORMENTE ENVIARA EL MENSAJE
HTTPClient http;
//COLOCAMOS LA URL DEL SERVIDOR A DONDE SE ENVIARA EL MENSAJE
http.begin(servidor.c_str());
//COLOCAMOS LA CABECERA DONDE INDICAMOS QUE SERA TIPO JSON
http.addHeader("Content-Type", "application/json");
//AGREGAMOS EL TOKEN EN LA CABECERA DE LOS DATOS A ENVIAR
http.addHeader("Authorization", token);
//ENVIAMOS LOS DATOS VIA POST
int httpPostCode = http.POST(payload);
//SI SE LOGRARON ENVIAR LOS DATOS
if (httpPostCode > 0) {
//RECIBIMOS LA RESPUESTA QUE NOS ENTREGA META
int httpResponseCode = http.GET();
//SI HAY RESPUESTA LA MOSTRAMOS
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
}
}
}