basicotantpserverdebugwebserial
Platform.ini
[env:nodemcuv2] platform = espressif8266 board = nodemcuv2 framework = arduino monitor_dtr = 0 monitor_rts = 0 monitor_speed = 115200 lib_deps = bblanchon/ArduinoJson @ ^7.2.1 256dpi/MQTT @ ^2.5.2 ayushsharma82/ElegantOTA @ ^3.1.1 majicdesigns/MD_MAX72XX @ ^3.5.1 majicdesigns/MD_Parola @ ^3.7.2 arduino-libraries/NTPClient@^3.2.1 pfeerick/elapsedMillis @ ^1.0.6 plageoj/UrlEncode @ ^1.0.1 build_flags=-DELEGANTOTA_USE_ASYNC_WEBSERVER=1
Main
#include <Arduino.h>
#if WEBDEBUG == 1
#define debug(x) WebSerial.print(x)
#define debugln(x) WebSerial.println(x)
#else
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#endif
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ElegantOTA.h>
// WiFi credentials
const char* ssid = "SSID";
const char* password = "Password";
const char *hostname = "Hostname";
String IPaddr;
// Time settings
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp.pagasa.dost.gov.ph", 28800, 60000); // Update every 60 seconds
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
AsyncEventSource events("/time");
// HTML content
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html>
<head>
<title>ESP8266 Time Server</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial; text-align: center; margin-top: 50px; }
.time { font-size: 48px; margin: 20px; }
.ota-link { margin-top: 30px; display: block; }
</style>
</head>
<body>
<h1>ESP8266 Time Server</h1>
<div class="time" id="time">--:--:--</div>
<a href="/update" class="ota-link">OTA Update</a>
<script>
const eventSource = new EventSource('/time');
eventSource.onmessage = function(e) {
document.getElementById('time').innerHTML = e.data;
};
</script>
</body>
</html>
)rawliteral";
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.hostname(hostname);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
IPaddr = WiFi.localIP().toString();
Serial.println("\nConnected to WiFi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Initialize NTP client
timeClient.begin();
timeClient.update();
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html);
});
// Handle Server-Sent Events (SSE) for time updates
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
Serial.printf("Client reconnected! Last message ID: %u\n", client->lastId());
}
client->send("hello!", NULL, millis(), 1000);
});
server.addHandler(&events);
// Start ElegantOTA
ElegantOTA.begin(&server);
// Start server
server.begin();
// Setup periodic time updates
timeClient.setUpdateInterval(60000); // Update NTP every 60 seconds
}
void loop() {
static unsigned long lastUpdate = 0;
timeClient.update();
// Send time update every second to connected clients
if (millis() - lastUpdate >= 1000) {
lastUpdate = millis();
String currentTime = timeClient.getFormattedTime();
events.send(currentTime.c_str(), "time");
debugln(currentTime);
}
// Handle OTA updates
ElegantOTA.loop();
}
basicotantpserverdebugwebserial.txt · Last modified: by 127.0.0.1
