#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// +international_country_code + phone number
// Portugal +351, example: +351912345678
String phoneNumber = "+593983388011";
String apiKey = "4631173";
int Pulsador=12;
int LED=2;
void sendMessage(String message){
// Data to send with HTTP POST
String url = "http://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
HTTPClient http;
http.begin(url);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send HTTP POST request
int httpResponseCode = http.POST(url);
if (httpResponseCode == 200){
Serial.print("Message sent successfully");
}
else{
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
void setup() {
pinMode(Pulsador, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
//Si la señal del pulsador es activa encendemos el led
if (digitalRead(Pulsador) == HIGH) {
digitalWrite(LED, HIGH);
sendMessage("Se ha encendido la luz ahora");
}
//de lo contrario apagamos el led
else {
digitalWrite(LED, LOW);
sendMessage("LUZ APAGADA");
}
delay(10);
}