 
                    	
					
                    Enviar un mensaje de WhatsApp desde ESP32
En este tutorial vamos Enviar un mensaje de WhatsApp desde ESP32 cuando se detecte un movimiento utilizando WhatsApp Cloud API para lograr este objetivo debemos seguir estos pasos:
- Crea una cuenta en Facebook for Developers y solicita acceso a WhatsApp Cloud API.
- Generamos certificados y obteniendo credenciales de autenticación.
- Creamos el circuito del ESP32 y un sensor de movimiento PIR.
- Agregamos el código a nuestro ESP32.
- Utiliza el ESP32 para conectarte a Internet, ya sea mediante Wi-Fi o datos móviles.
- Ingresa los datos de autentificación de WhatsApp en ESP32 y enviar Un whatsApp
 
Crear una cuenta en Facebook for Developers.

O podemos aceder a este link Todas las apps – Meta for Developers (facebook.com)





Generamos certificados y obteniendo credenciales de autenticación.






Creamos el circuito del ESP32 y un sensor de movimiento PIR.
Requerimientos:
- ESP32
- PIR sensor de movimiento.
- Cables.

Agregamos el código a nuestro ESP32.
//Enviar un mensaje de WhatsApp desde ESP32
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//COLOCAMOS EL TOKEN QUE NOS ENTREGA META
String token="Bearer EAAIao2QuMP4BAEg4Hs76IvHZAeBuZCYP9lCnipU1hlxDmTcw77orMZCaawAoAusMNvvDlETU5d1uxiwjvB72j2DW6UOJqxIxZCofP6apbuitVtZBjueB4HZBXZBhIY64JN75tWFVY1UQbBo9gZAZBWl776gC3khAPcgO4tiykQ1CPpZBXTBCEeRZB49dSUgZB2rYW2OYr7NWKgzEIgZDZD";
//COLOCAMOS LA URL A DONDE SE ENVIAN LOS MENSAJES DE WHATSAPP
String servidor = "https://graph.facebook.com/v16.0/111290641852610/messages";
//CREAMOS UNA JSON DONDE SE COLOCA EL NUMERO DE TELEFONO Y EL MENSAJE
String payload = "{\"messaging_product\":\"whatsapp\",\"to\":\"527121122441\",\"type\":\"text\",\"text\": {\"body\": \"Movimiento detectado\"}}";
//PIN DEL SENSOR DE MOVIMIENTO
const int pinSensorMov = 15;
//ESTADO DEL SENSOR
int estadoActual=LOW;
void setup() {
  Serial.begin(9600);
  //ACTIVAMOS EL PIN SEL SENSOR DE MOVIMIENTO
  pinMode(pinSensorMov, INPUT);
  //COLOCAMOS USUARIO Y CONTRASEÑA DE NUESTRA RED WIFI
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Se ha conectado al wifi con la ip: ");
  Serial.println(WiFi.localIP());
}
void loop() {
  //DETECTAMOS EL ESTADO DEL DETECTO DE OVIMIENTO
  estadoActual = digitalRead(pinSensorMov);
  //SI SE DETECTA MOVIMIENTO
  if (estadoActual == HIGH) {
    Serial.println("Hay Movimiento");
    delay(5000);
    if(WiFi.status()== WL_CONNECTED){
      //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);
        }
        else {
          Serial.print("Error code: ");
          Serial.println(httpResponseCode);
        }
      }
      http.end();
    }
    else {
      Serial.println("WiFi Desconectado");
    }
  }
  //SI NO SE DETECTA MOVIMIENTO
  else{
    Serial.println("No hay Movimiento");
  }
  delay(1000);
}
Cirduito y codigo: Enviar WhatsApp con e ESP32 – Wokwi Arduino and ESP32 Simulator

