#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <time.h>
// WiFi for Wokwi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Telegram details
String botToken = "8747387449:AAEAs61LU8PLJMRp4czMEVkzsIEbadWXHaU";
String chatID = "7025624835";
// Door sensor pin
const int doorPin = 4;
// Location
String projectLocation = "Wokwi Online Simulation";
// Time settings (India)
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 19800;
const int daylightOffset_sec = 0;
// Previous state
int lastDoorState;
// URL encode function
String urlEncode(String str) {
String encoded = "";
char c, code0, code1;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (isalnum((unsigned char)c)) encoded += c;
else if (c == ' ') encoded += "%20";
else if (c == '\n') encoded += "%0A";
else if (c == '*') encoded += "%2A";
else {
encoded += '%';
code0 = (c >> 4) & 0xF;
code1 = c & 0xF;
code0 = code0 > 9 ? code0 + 'A' - 10 : code0 + '0';
code1 = code1 > 9 ? code1 + 'A' - 10 : code1 + '0';
encoded += code0;
encoded += code1;
}
}
return encoded;
}
// Get date, time, location
String getDateTimeLocation() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return "š
Date: Not Available\nā° Time: Not Available\nš Location: " + projectLocation;
}
char dateStr[20];
char timeStr[20];
strftime(dateStr, sizeof(dateStr), "%d-%m-%Y", &timeinfo);
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo);
String result = "";
result += "š
Date: " + String(dateStr) + "\n";
result += "ā° Time: " + String(timeStr) + "\n";
result += "š Location: " + projectLocation;
return result;
}
// Send Telegram message
void sendTelegram(String text) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected");
return;
}
WiFiClientSecure client;
client.setInsecure();
HTTPClient https;
String message = text + "\n\n" + getDateTimeLocation();
String url = "https://api.telegram.org/bot" + botToken +
"/sendMessage?chat_id=" + chatID +
"&parse_mode=Markdown" +
"&text=" + urlEncode(message);
Serial.println("Sending...");
Serial.println(message);
https.begin(client, url);
int httpCode = https.GET();
Serial.print("HTTP Response: ");
Serial.println(httpCode);
String response = https.getString();
Serial.println(response);
https.end();
Serial.println("----------------------");
}
void setup() {
Serial.begin(115200);
pinMode(doorPin, INPUT_PULLUP);
// š„ FIX: initialize state correctly
lastDoorState = digitalRead(doorPin);
Serial.println("Connecting WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
Serial.println(WiFi.localIP());
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
delay(2000);
sendTelegram("*š SYSTEM STARTED*");
}
void loop() {
int currentState = digitalRead(doorPin);
Serial.print("State: ");
Serial.println(currentState);
// šŖ Door Opened
if (currentState == LOW && lastDoorState == HIGH) {
Serial.println("Door Opened detected");
sendTelegram("*šØ DOOR OPENED šØ*");
delay(400); // debounce
}
// šŖ Door Closed
if (currentState == HIGH && lastDoorState == LOW) {
Serial.println("Door Closed detected");
sendTelegram("*ā
DOOR CLOSED*");
delay(400); // debounce
}
lastDoorState = currentState;
}