-
Notifications
You must be signed in to change notification settings - Fork 7
/
wol-bot.ino
144 lines (113 loc) · 3.96 KB
/
wol-bot.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
#include "M5Atom.h"
#include "WiFiMulti.h"
#include "WiFiClientSecure.h"
#include "WiFiUDP.h"
#include "WakeOnLan.h"
#include "UniversalTelegramBot.h"
#include "ArduinoJson.h"
// LED Colors
#define WHITE 0xFFFFFF
#define RED 0x800000
#define GREEN 0x008000
#define PURPLE 0x800080
#define AMBER 0xFF4000
#define OFF 0x000000
// Telegram Bot Token
#define BOT_TOKEN "0000000000:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define ALLOWED_ID "000000000"
// WiFi configuration
#define WIFI_SSID ""
#define WIFI_PASS ""
// MAC address of the target device
#define MAC_ADDR "FF:FF:FF:FF:FF:FF"
WiFiMulti wifiMulti;
WiFiClientSecure secured_client;
WiFiUDP UDP;
WakeOnLan WOL(UDP);
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done
void sendWOL() {
M5.dis.drawpix(0, WHITE);
WOL.sendMagicPacket(MAC_ADDR); // send WOL on default port (9)
delay(300);
M5.dis.drawpix(0, AMBER);
}
void handleNewMessages(int numNewMessages) {
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++) {
Serial.println(bot.messages[i].from_id);
if (bot.messages[i].from_id != ALLOWED_ID) continue;
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
if (text == "/wol") {
sendWOL();
bot.sendMessage(chat_id, "Magic Packet sent!", "");
} else if (text == "/ledon") {
M5.dis.drawpix(0, AMBER); // turn the LED on
bot.sendMessage(chat_id, "Led is ON.", "");
} else if (text == "/ledoff") {
M5.dis.drawpix(0, OFF); // turn the LED off)
bot.sendMessage(chat_id, "Led is OFF.", "");
} else if (text == "/ping") {
bot.sendMessage(chat_id, "Pong.", "");
} else if (text == "/start") {
String welcome = "Welcome to **WoL Bot**, " + from_name + ".\n";
welcome += "Use is restricted to the bot owner.\n\n";
welcome += "/ledon : Switch the Led ON\n";
welcome += "/ledoff : Switch the Led OFF\n";
welcome += "/wol : Send the Magic Packet\n";
welcome += "/ping : Check the bot status\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
/* =================================== SETUP =================================== */
void setup(){
// Configure WiFI
wifiMulti.addAP(WIFI_SSID, WIFI_PASS);
// Clear the serial port buffer and set the serial port baud rate to 115200.
// Do not Initialize I2C. Initialize the LED matrix.
M5.begin(true, false, true); delay(25);
M5.dis.drawpix(0, RED); delay(100);
// Add root certificate for api.telegram.org
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
Serial.print("Connecting to WiFI...");
while ((wifiMulti.run() != WL_CONNECTED)) { // wait for WiFi connection.
delay(500);
Serial.print(".");
}
Serial.println("OK");
// Attention: 255.255.255.255 is denied in some networks
WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());
M5.dis.drawpix(0, GREEN); delay(100);
Serial.print("Retrieving time...");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600) {
Serial.print(".");
delay(150);
now = time(nullptr);
}
Serial.println(now);
M5.dis.drawpix(0, AMBER); delay(100);
}
/* =================================== LOOP =================================== */
void loop() {
// Send WoL on button press
if (M5.Btn.wasPressed()) sendWOL();
if (millis() - bot_lasttime > BOT_MTBS) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("Response received");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
}
delay(10);
M5.update();
}