#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int pin = 15;
String phoneNumber = "+6281315877267";
String apiKey = "3297288";
String urlEncode(String str) {
String encodedString = "";
char c;
char code0;
char code1;
char code2;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == ' ') {
encodedString += '+';
} else if (isalnum(c)) {
encodedString += c;
} else {
code1 = (c & 0xf) + '0';
if ((c & 0xf) > 9) {
code1 = (c & 0xf) - 10 + 'A';
}
c = (c >> 4) & 0xf;
code0 = c + '0';
if (c > 9) {
code0 = c - 10 + 'A';
}
code2 = '\0';
encodedString += '%';
encodedString += code0;
encodedString += code1;
}
yield();
}
return encodedString;
}
void sendMessage(String message) {
HTTPClient http;
String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
http.begin(url);
int httpResponseCode = http.GET(); // Ubah POST menjadi GET untuk CallMeBot
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();
}
void setup() {
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());
pinMode(pin, INPUT); // Atur mode pin untuk PIR sensor
}
void loop() {
int IP = digitalRead(pin);
delay(100);
if (IP == 1) {
sendMessage("LAPOR KOMANDAN DANI! Ada Orang Yang Sangat Mencurigakan.");
delay(1000);
} else {
delay(1000);
}
}