-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeleBotCommunicationSLA.ino
160 lines (141 loc) · 5.74 KB
/
TeleBotCommunicationSLA.ino
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <WiFi.h>
#include <WebServer.h>
#include <UniversalTelegramBot.h>
#define BOT_TOKEN "7500118477:AAG8R4YHWyMsQrKS0rjxlP0TOTDAzbkguS8" // Your bot token
#define CHAT_ID "1644763236" // Your chat ID
WebServer server(80);
WiFiClient wifiClient; // Create a WiFiClient instance
UniversalTelegramBot bot(BOT_TOKEN, wifiClient); // Pass the WiFiClient instance
bool faceDetectionEnabled = false;
bool irSensorEnabled = true;
bool ledState = false;
bool manualControl = false;
const int ledPin = 4;
const int irSensorPin = 14;
const int buzzerPin = 5;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(irSensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
WiFi.begin("IDEA_WIFI_2", "ideawifi2");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, handleRoot);
server.on("/face", HTTP_GET, handleFaceDetectionControl);
server.on("/ir", HTTP_GET, handleIrSensorControl);
server.on("/led", HTTP_GET, handleLedControl);
server.on("/status", HTTP_GET, handleStatusCheck);
server.begin();
}
void loop() {
server.handleClient();
checkTelegramMessages(); // Check for incoming Telegram messages
updateLedState();
}
void checkTelegramMessages() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("New message received");
handleTelegramCommand(bot.messages[0].text);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
}
void handleTelegramCommand(String command) {
if (command == "/start") {
bot.sendMessage(CHAT_ID, "Welcome to Smart Lighting Automation! Use /help to see commands.", "");
} else if (command == "/help") {
bot.sendMessage(CHAT_ID, "Available commands:\n"
"/face_on - Enable Face Detection\n"
"/face_off - Disable Face Detection\n"
"/ir_on - Enable Motion Sensor\n"
"/ir_off - Disable Motion Sensor\n"
"/led_on - Turn LED On\n"
"/led_off - Turn LED Off\n"
"/status - Check Status", "");
} else if (command == "/face_on") {
faceDetectionEnabled = true;
bot.sendMessage(CHAT_ID, "Face Detection Enabled", "");
} else if (command == "/face_off") {
faceDetectionEnabled = false;
bot.sendMessage(CHAT_ID, "Face Detection Disabled", "");
} else if (command == "/ir_on") {
irSensorEnabled = true;
bot.sendMessage(CHAT_ID, "Motion Sensor Enabled", "");
} else if (command == "/ir_off") {
irSensorEnabled = false;
bot.sendMessage(CHAT_ID, "Motion Sensor Disabled", "");
} else if (command == "/led_on") {
ledState = true;
manualControl = true;
updateLedState();
bot.sendMessage(CHAT_ID, "LED Turned On", "");
} else if (command == "/led_off") {
ledState = false;
manualControl = true;
updateLedState();
bot.sendMessage(CHAT_ID, "LED Turned Off", "");
} else if (command == "/status") {
String status = "Face Detection: " + String(faceDetectionEnabled ? "Enabled" : "Disabled") +
"\nMotion Sensor: " + String(irSensorEnabled ? "Enabled" : "Disabled") +
"\nLED: " + String(ledState ? "On" : "Off");
bot.sendMessage(CHAT_ID, status, "");
}
}
void updateLedState() {
bool irSensorState = digitalRead(irSensorPin);
if (manualControl) {
digitalWrite(ledPin, ledState ? HIGH : LOW);
} else if (faceDetectionEnabled) {
// Here you would integrate your face detection logic
digitalWrite(ledPin, HIGH); // Example: turn LED on if face detection is enabled
} else if (irSensorEnabled) {
digitalWrite(ledPin, irSensorState == LOW ? HIGH : LOW);
} else {
digitalWrite(ledPin, LOW);
}
}
void handleRoot() {
String html = "<!DOCTYPE html><html><head><title>Smart Lighting Automation</title>";
html += "<style>body { font-family: Arial; background: #f0f0f0; }</style></head><body>";
html += "<h1>Smart Lighting Automation</h1>";
html += "<a href=\"/face?enable=1\">Enable Face Detection</a><br>";
html += "<a href=\"/face?enable=0\">Disable Face Detection</a><br>";
html += "<a href=\"/ir?enable=1\">Enable Motion Sensor</a><br>";
html += "<a href=\"/ir?enable=0\">Disable Motion Sensor</a><br>";
html += "<a href=\"/led?state=1\">Turn LED On</a><br>";
html += "<a href=\"/led?state=0\">Turn LED Off</a><br>";
html += "<a href=\"/status\">Check Status</a>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleFaceDetectionControl() {
if (server.hasArg("enable")) {
faceDetectionEnabled = server.arg("enable") == "1";
}
server.send(200, "text/plain", faceDetectionEnabled ? "Face Detection Enabled" : "Face Detection Disabled");
}
void handleIrSensorControl() {
if (server.hasArg("enable")) {
irSensorEnabled = server.arg("enable") == "1";
}
server.send(200, "text/plain", irSensorEnabled ? "Motion Sensor Enabled" : "Motion Sensor Disabled");
}
void handleLedControl() {
if (server.hasArg("state")) {
ledState = server.arg("state") == "1";
manualControl = true;
updateLedState();
}
server.send(200, "text/plain", ledState ? "LED On" : "LED Off");
}
void handleStatusCheck() {
String status = "Face Detection is " + String(faceDetectionEnabled ? "Enabled" : "Disabled") + "\n";
status += "Motion Sensor is " + String(irSensorEnabled ? "Enabled" : "Disabled");
server.send(200, "text/plain", status);
}