-
Notifications
You must be signed in to change notification settings - Fork 0
/
telebell.ino
307 lines (247 loc) · 8.72 KB
/
telebell.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include <Crypto.h>
#include <SHA256.h>
#include <Arduino.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <FS.h>
// Define constants for EEPROM addresses
#define SSID_ADDRESS 0
#define SALT_ADDRESS (SSID_ADDRESS + 32)
#define PASSWORD_HASH_ADDRESS (SALT_ADDRESS + 16)
#define BOT_TOKEN_ADDRESS (PASSWORD_HASH_ADDRESS + 32)
#define CHAT_ID_ADDRESS (BOT_TOKEN_ADDRESS + 32)
#define USERNAME_ADDRESS (CHAT_ID_ADDRESS + 32)
#define PASSWORD_ADDRESS (USERNAME_ADDRESS + 16)
#define NTP_SERVER1_ADDRESS (PASSWORD_ADDRESS + 16)
#define NTP_SERVER2_ADDRESS (NTP_SERVER1_ADDRESS + 32)
#define FLAG_ADDRESS (NTP_SERVER2_ADDRESS + 32)
#define CONFIG_SSID_NAME "Campainha Config"
#define MAGIC_NUMBER 0xDEADBEEF
const int eepromSize = 512;
// Declarations for GPIO pins
const int shortcutPin1 = 4; // Assign the appropriate GPIO pin number
const int shortcutPin2 = 5; // Assign the appropriate GPIO pin number
const int PORT = 80; // Change as needed
struct FileData {
const char* path;
const char* contentType;
};
// Define the files and their content types
FileData files[] = {
{ "/index.html", "text/html" },
{ "/script.js", "application/javascript" },
{ "/styles.css", "text/css" }
};
struct Config {
char ssid[32];
char password[16];
char botToken[32];
char chatId[32];
char username[16];
char guiPassword[16];
char ntpServer1[32];
char ntpServer2[32];
unsigned long updateInterval; // Added this line
unsigned long intervalUnit; // Added this line
};
ESP8266WebServer server(PORT);
Config initConf;
WiFiUDP ntpUDP;
unsigned long updateInterval;
unsigned long intervalUnit;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 0, 0);
void sendMessageToTelegram(const char *message) {
// Read the bot token and chat ID from the EEPROM
char botToken[32];
char chatId[32];
EEPROM.get(BOT_TOKEN_ADDRESS, botToken);
EEPROM.get(CHAT_ID_ADDRESS, chatId);
// Construct the URL
String url = "https://api.telegram.org/bot" + String(botToken) + "/sendMessage?chat_id=" + String(chatId) + "&text=" + String(message);
// Send the request
WiFiClient client;
HTTPClient http;
http.begin(client, url.c_str());
int httpCode = http.GET();
// Check the response code
if (httpCode > 0) {
Serial.println("Message sent successfully");
} else {
Serial.println("Error sending message");
}
// Close the connection
http.end();
}
void serveFile(const char* path, const char* contentType) {
File file = SPIFFS.open(path, "r");
if (!file) {
server.send(500, "text/plain", "Internal Server Error");
return;
}
server.streamFile(file, contentType);
file.close();
}
void updateNtpServers(const char *ntpServer1, const char *ntpServer2) {
EEPROM.put(NTP_SERVER1_ADDRESS, ntpServer1);
EEPROM.put(NTP_SERVER2_ADDRESS, ntpServer2);
EEPROM.commit();
// Update NTP client with new servers
timeClient = NTPClient(ntpUDP, ntpServer1, 0, updateInterval * intervalUnit);
timeClient.begin();
timeClient.forceUpdate();
}
void connectToWiFi(Config &conf) {
Serial.println("Starting initial setup...");
WiFi.begin(conf.ssid, conf.password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi. IP address: " + WiFi.localIP().toString());
}
void saveConfig(Config &config) {
EEPROM.put(SSID_ADDRESS, config.ssid);
EEPROM.put(NTP_SERVER1_ADDRESS, config.ntpServer1);
EEPROM.put(NTP_SERVER2_ADDRESS, config.ntpServer2);
// Generate a random salt for each password
uint8_t salt[32];
for (int i = 0; i < sizeof(salt); i++) {
salt[i] = random(256);
}
EEPROM.put(SALT_ADDRESS, salt);
// Hash the password with SHA-256 and store the hash
SHA256 sha256;
sha256.update(salt, sizeof(salt));
sha256.update((uint8_t *)config.password, strlen(config.password));
uint8_t hash[32];
sha256.finalize(hash, sizeof(hash));
EEPROM.put(PASSWORD_HASH_ADDRESS, hash);
// Store the bot token and chat ID
EEPROM.put(BOT_TOKEN_ADDRESS, config.botToken);
EEPROM.put(CHAT_ID_ADDRESS, config.chatId);
// Store the flag
EEPROM.put(FLAG_ADDRESS, MAGIC_NUMBER);
EEPROM.commit();
}
bool loadConfig(Config &conf) {
EEPROM.get(SSID_ADDRESS, conf.ssid);
EEPROM.get(NTP_SERVER1_ADDRESS, conf.ntpServer1);
EEPROM.get(NTP_SERVER2_ADDRESS, conf.ntpServer2);
uint8_t salt[32];
EEPROM.get(SALT_ADDRESS, salt);
uint8_t hash[32];
EEPROM.get(PASSWORD_HASH_ADDRESS, hash);
SHA256 sha256;
sha256.update(salt, sizeof(salt));
sha256.update((uint8_t *)conf.password, strlen(conf.password));
uint8_t newHash[32];
sha256.finalize(newHash, sizeof(newHash));
if (memcmp(hash, newHash, sizeof(hash)) != 0) {
return false;
}
// Load the bot token and chat ID
EEPROM.get(BOT_TOKEN_ADDRESS, conf.botToken);
EEPROM.get(CHAT_ID_ADDRESS, conf.chatId);
// Load the flag
uint32_t magicNumber = EEPROM.get(FLAG_ADDRESS, magicNumber);
return (magicNumber == MAGIC_NUMBER);
}
bool checkEepromFlag() {
EEPROM.begin(eepromSize);
uint32_t magicNumber = EEPROM.get(FLAG_ADDRESS, magicNumber);
return (magicNumber == MAGIC_NUMBER);
}
void initSetup() {
if (!loadConfig(initConf) || !checkEepromFlag()) {
Serial.println("Starting initial setup");
// Start the configuration network
WiFi.softAP(CONFIG_SSID_NAME, "");
WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0));
EEPROM.begin(eepromSize);
loadConfig(initConf);
if (checkEepromFlag()) {
server.on("/", HTTP_GET, handleRoot);
server.on("/setup", HTTP_POST, handleSave);
server.begin();
Serial.println("Web server started for setup");
} else {
Serial.println("Normal mode. Web server not started.");
}
}
}
void handleRoot() {
serveFile("/index.html", "text/html");
serveFile("/script.js", "application/javascript");
serveFile("/styles.css", "text/css");
}
void handleSave() {
bool isCheckboxChecked = server.hasArg("checkBox") && server.arg("checkBox") == "on";
String ssid = server.arg("ssid");
String password = server.arg("password");
String botToken = server.arg("botToken");
String chatId = server.arg("chatId");
String ntpServer1 = server.arg("ntpServer1");
String ntpServer2 = server.arg("ntpServer2");
String updateIntervalStr = server.arg("updateInterval");
unsigned long updateInterval = updateIntervalStr.toInt(); // Convert string to unsigned long
if (isCheckboxChecked) {
// The checkbox is checked, get values from input fields
String updateNtp1 = server.arg("updateNtp1");
String updateNtp2 = server.arg("updateNtp2");
unsigned long intervalUnit = server.arg("intervalUnit").toInt(); // Convert string to unsigned long
// Update NTP servers and interval
updateNtpServers(updateNtp1.c_str(), updateNtp2.c_str());
initConf.intervalUnit = intervalUnit;
server.send(200, "text/plain", "Settings and NTP servers updated. Please reboot the device.");
} else {
// The checkbox is not checked, send default content to the client
String defaultDateTime = "<script>getCurrentDateTime()</script>";
server.send(200, "text/plain", "Settings saved. Please reboot the device.|" + defaultDateTime);
}
// Update configuration
strncpy(initConf.ssid, ssid.c_str(), sizeof(initConf.ssid));
strncpy(initConf.password, password.c_str(), sizeof(initConf.password));
strncpy(initConf.botToken, botToken.c_str(), sizeof(initConf.botToken));
strncpy(initConf.chatId, chatId.c_str(), sizeof(initConf.chatId));
strncpy(initConf.ntpServer1, ntpServer1.c_str(), sizeof(initConf.ntpServer1));
strncpy(initConf.ntpServer2, ntpServer2.c_str(), sizeof(initConf.ntpServer2));
// Save configuration to file
saveConfig(initConf);
}
void setup() {
Serial.begin(115200);
// Mount the file system
if (SPIFFS.begin()) {
Serial.println("File system mounted successfully");
} else {
Serial.println("An Error has occurred while mounting SPIFFS");
}
randomSeed(analogRead(0));
EEPROM.begin(eepromSize);
bool flag = checkEepromFlag();
if (!flag) {
Serial.println("EEPROM flag is not set. Setting initial value.");
EEPROM.put(FLAG_ADDRESS, MAGIC_NUMBER);
EEPROM.commit();
}
pinMode(shortcutPin1, INPUT_PULLUP);
pinMode(shortcutPin2, INPUT_PULLUP);
initSetup();
timeClient.begin(); // Initialize the NTP client
}
void loop() {
server.handleClient();
if (!checkEepromFlag()) {
connectToWiFi(initConf);
timeClient.update(); // Update the NTP client
}
if (digitalRead(shortcutPin1) == LOW && digitalRead(shortcutPin2) == LOW) {
Serial.println("Shortcut buttons pressed. Sending message.");
sendMessageToTelegram("Hello from the ESP8266!");
}
}