-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwol-tgbot-esp32.ino
186 lines (152 loc) · 4.79 KB
/
wol-tgbot-esp32.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "WiFiUDP.h"
#include "WakeOnLan.h"
#include "UniversalTelegramBot.h"
#include "ArduinoJson.h"
#include "esp_task_wdt.h"
// WiFi
const char WIFI_SSID[] PROGMEM = "XXXXXXXX";
const char WIFI_PASSWORD[] PROGMEM = "XXXXXXXX";
// Telegram Bot API
const char BOT_TOKEN[] PROGMEM = "0000000000:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const char BOT_ALLOWED_ID[] PROGMEM = "0000000000";
// MAC Address of WakeOnLan device
const char MAC_ADDR[] PROGMEM = "XX:XX:XX:XX:XX:XX";
// Constants
constexpr uint8_t WIFI_LED_PIN = 8; // Built-in LED on most ESP32 dev boards, change if using different pin
constexpr uint8_t WDT_TIMEOUT = 60;
constexpr uint16_t WIFI_RETRY_DELAY = 5000;
constexpr uint16_t WIFI_TIMEOUT = 20000;
constexpr uint16_t BOT_MTBS = 1000;
// Global variables (minimized)
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
WiFiUDP UDP;
WakeOnLan WOL(UDP);
uint32_t bot_lasttime = 0;
bool setupWiFi() {
Serial.print(F("[WiFi] Connecting to "));
Serial.println(WIFI_SSID);
digitalWrite(WIFI_LED_PIN, LOW);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
uint32_t startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < WIFI_TIMEOUT) {
Serial.print(".");
delay(500);
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println(F("\nFailed to connect to WiFi"));
return false;
}
Serial.print(F("\nWiFi connected. IP address: "));
Serial.println(WiFi.localIP());
digitalWrite(WIFI_LED_PIN, HIGH);
return true;
}
bool setupTime() {
Serial.print(F("Retrieving time: "));
configTime(0, 0, "time.cloudflare.com", "pool.ntp.org");
time_t now = time(nullptr);
uint32_t startTime = millis();
while (now < 24 * 3600 && millis() - startTime < 10000) {
delay(100);
Serial.print(".");
now = time(nullptr);
}
if (now < 24 * 3600) {
Serial.println(F("\nFailed to get NTP time"));
return false;
}
Serial.println(now);
return true;
}
void setupWDT() {
// Delete any existing watchdog timer first
esp_task_wdt_deinit();
// Initialize Watchdog with new config
esp_task_wdt_config_t wdt_config = {
.timeout_ms = WDT_TIMEOUT * 1000,
.idle_core_mask = (1 << 0),
.trigger_panic = true
};
esp_task_wdt_init(&wdt_config);
esp_task_wdt_add(NULL); // Add current thread to WDT watch
}
void sendWOL() {
WOL.sendMagicPacket(MAC_ADDR);
delay(300);
}
void sendSystemStatus(const String& chat_id) {
String status =
"📊 System Status\n"
"━━━━━━━━━\n";
status += "🗃️ Memory: " + String(ESP.getFreeHeap() / 1024.0, 1) + " / " + String(ESP.getHeapSize() / 1024.0, 1) + " KB\n";
status += "📡 RSSI: " + String(WiFi.RSSI()) + " dBm\n";
status += "🌐 IP: " + WiFi.localIP().toString() + "\n";
status += "📝 SSID: " + WiFi.SSID();
bot.sendMessage(chat_id, status, "");
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
esp_task_wdt_reset();
if (String(BOT_ALLOWED_ID) != bot.messages[i].from_id) continue;
const String& chat_id = bot.messages[i].chat_id;
const String& text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name.isEmpty()) {
from_name = F("Guest");
}
if (text == F("/wol")) {
sendWOL();
bot.sendMessage(chat_id, F("Magic Packet sent!"), "");
} else if (text == F("/ping")) {
bot.sendMessage(chat_id, F("Pong."), "");
} else if (text == F("/status")) {
sendSystemStatus(chat_id);
} else if (text == F("/start") || text == F("/help")) {
String welcome = "Welcome to **WoL Bot**, " + from_name + ".\n";
welcome += F("Use is restricted to the bot owner.\n\n");
welcome += F("/wol : Send the Magic Packet\n");
welcome += F("/status : Check the bot status\n");
welcome += F("/ping : Pong.");
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.println(F("Starting up..."));
pinMode(WIFI_LED_PIN, OUTPUT);
// Initialize Watchdog
setupWDT();
while (!setupWiFi()) {
delay(WIFI_RETRY_DELAY);
}
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (!setupTime()) {
delay(WIFI_RETRY_DELAY);
}
}
void loop() {
esp_task_wdt_reset();
if (millis() - bot_lasttime > BOT_MTBS) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println(F("WiFi connection lost. Reconnecting..."));
WiFi.disconnect();
if (!setupWiFi()) {
bot_lasttime = millis();
return;
}
}
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
if (numNewMessages) {
Serial.println(F("Response received"));
handleNewMessages(numNewMessages);
}
bot_lasttime = millis();
}
delay(10);
}