-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ino
More file actions
99 lines (75 loc) · 2.17 KB
/
code.ino
File metadata and controls
99 lines (75 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ESP32Servo.h>
// WiFi credentials
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
// Telegram Bot Token
#define BOT_TOKEN "YOUR_BOT_TOKEN"
// Your Telegram Chat ID
#define CHAT_ID "YOUR_CHAT_ID"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
// Servo
Servo lockServo;
int servoPin = 13;
// Lock states
bool isLocked = true;
void setup() {
Serial.begin(115200);
lockServo.attach(servoPin);
lockDoor();
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
client.setInsecure(); // skip certificate validation
bot.sendMessage(CHAT_ID, "🔐 Lock System Online!", "");
}
void loop() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
delay(1000);
}
void handleMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String text = bot.messages[i].text;
String chat_id = bot.messages[i].chat_id;
if (text == "/start") {
String welcome = "🔐 Smart Lock System\n\n";
welcome += "/lock - Lock Door\n";
welcome += "/unlock - Unlock Door\n";
welcome += "/status - Check Status";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/lock") {
lockDoor();
bot.sendMessage(chat_id, "🔒 Door Locked", "");
}
if (text == "/unlock") {
unlockDoor();
bot.sendMessage(chat_id, "🔓 Door Unlocked", "");
}
if (text == "/status") {
if (isLocked)
bot.sendMessage(chat_id, "Status: 🔒 Locked", "");
else
bot.sendMessage(chat_id, "Status: 🔓 Unlocked", "");
}
}
}
void lockDoor() {
lockServo.write(0); // adjust angle
isLocked = true;
}
void unlockDoor() {
lockServo.write(90); // adjust angle
isLocked = false;
}