#include <WiFi.h>
#include <HTTPClient.h>
char ssid[] = "Wokwi-GUEST"; // Your WiFi SSID
char pass[] = ""; // Your WiFi password
const char* phoneNumber = "6282359159821"; // Phone number to send WhatsApp message
const char* apiKey = "1680760"; // CallMeBot API key
int pirPin = 4; // PIR sensor pin
int motionCount = 0;
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(115200);
connectToWiFi();
}
void loop() {
checkMotion();
delay(100); // Small delay to avoid excessive checking
}
void connectToWiFi() {
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
}
void checkMotion() {
int motionState = digitalRead(pirPin);
if (motionState == HIGH) {
// Uncomment the following lines if you have an LED connected
// int ledPin = 13; // Example pin for LED
// digitalWrite(ledPin, HIGH); // Turn on LED
// digitalWrite(ledPin, LOW); // Turn off LED
motionCount++;
Serial.println("Motion detected!");
Serial.print("Motion count: ");
Serial.println(motionCount);
// Send WhatsApp message
String message = "Motion detected! Count: " + String(motionCount);
sendWhatsAppMessage(message);
}
}
void sendWhatsAppMessage(String message) {
String url = "https://api.callmebot.com/whatsapp.php?phone=" + String(phoneNumber) + "&apikey=" + String(apiKey) + "&text=" + message;
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
Serial.println("Message sent successfully");
} else {
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
http.end();
}