-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp.ino
93 lines (74 loc) · 2.25 KB
/
esp.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
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
// Replace with your network credentials
char* ssid = "MCU";
char* password = "00000000";
// Your NTP server settings
char* ntpServer = "pool.ntp.org";
long gmtOffset_sec = 19800; // Adjust to your timezone
int daylightOffset_sec = 19800; // Adjust to your timezone
// Create a WiFiUDP object to send and receive NTP time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServer, gmtOffset_sec, 600000);
WebServer server(80);
// HTML and JS files from SPIFFS
void serveFile(const char* path, const char* type) {
File file = SPIFFS.open(path, "r");
if (!file) {
server.send(404, "text/plain", "File Not Found");
return;
}
server.streamFile(file, type);
file.close();
}
void handleRoot() {
serveFile("/index.html", "text/html");
}
void handleStyles() {
serveFile("/styles.css", "text/css");
}
void handleScript() {
serveFile("/script.js", "application/javascript");
}
void handleTime() {
timeClient.update();
String formattedTime = timeClient.getFormattedTime();
server.send(200, "text/plain", formattedTime);
}
void setup() {
// Serial port for debugging purposes
Serial.begin(9600);
SPIFFS.begin() ? Serial.println("mounting SPIFFS.....") : Serial.println("Error while mounting SPIFFS");
// Set up Access Point
WiFi.softAP(ssid, password);
// Print the IP address
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.on("/", handleRoot);
server.on("/styles.css", handleStyles);
server.on("/script.js", handleScript);
server.on("/time", handleTime);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
static unsigned long lastCheck = 0;
if (millis() - lastCheck >= 600000) { // Check every minute *10
lastCheck = millis();
WiFi.disconnect();
WiFi.begin("CSGO", "sinzoff_");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
timeClient.begin();
timeClient.update();
}
}