basicotantpserverdebugwebserial
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| basicotantpserverdebugwebserial [2025/03/01 00:07] – created jwan | basicotantpserverdebugwebserial [2025/10/10 10:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| Platform.ini | Platform.ini | ||
| - | [code] | + | <code> |
| [env: | [env: | ||
| platform = espressif8266 | platform = espressif8266 | ||
| Line 19: | Line 19: | ||
| plageoj/ | plageoj/ | ||
| build_flags=-DELEGANTOTA_USE_ASYNC_WEBSERVER=1 | build_flags=-DELEGANTOTA_USE_ASYNC_WEBSERVER=1 | ||
| - | [/code] | + | </code> |
| + | |||
| + | Main | ||
| + | < | ||
| + | #include < | ||
| + | #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 < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // WiFi credentials | ||
| + | const char* ssid = " | ||
| + | const char* password = " | ||
| + | const char *hostname = " | ||
| + | String IPaddr; | ||
| + | |||
| + | // Time settings | ||
| + | WiFiUDP ntpUDP; | ||
| + | NTPClient timeClient(ntpUDP, | ||
| + | |||
| + | // Create AsyncWebServer object on port 80 | ||
| + | AsyncWebServer server(80); | ||
| + | AsyncEventSource events("/ | ||
| + | |||
| + | // HTML content | ||
| + | const char index_html[] PROGMEM = R" | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <meta charset=" | ||
| + | <meta name=" | ||
| + | < | ||
| + | body { font-family: | ||
| + | .time { font-size: 48px; margin: 20px; } | ||
| + | .ota-link { margin-top: 30px; display: block; } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | <div class=" | ||
| + | <a href="/ | ||
| + | |||
| + | < | ||
| + | const eventSource = new EventSource('/ | ||
| + | eventSource.onmessage = function(e) { | ||
| + | document.getElementById(' | ||
| + | }; | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | )rawliteral"; | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(115200); | ||
| + | |||
| + | // Connect to WiFi | ||
| + | WiFi.hostname(hostname); | ||
| + | WiFi.begin(ssid, | ||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | delay(500); | ||
| + | Serial.print(" | ||
| + | } | ||
| + | IPaddr = WiFi.localIP().toString(); | ||
| + | Serial.println(" | ||
| + | Serial.print(" | ||
| + | Serial.println(WiFi.localIP()); | ||
| + | |||
| + | // Initialize NTP client | ||
| + | timeClient.begin(); | ||
| + | timeClient.update(); | ||
| + | |||
| + | // Route for root / web page | ||
| + | server.on("/", | ||
| + | request-> | ||
| + | }); | ||
| + | |||
| + | // Handle Server-Sent Events (SSE) for time updates | ||
| + | events.onConnect([](AsyncEventSourceClient *client){ | ||
| + | if(client-> | ||
| + | Serial.printf(" | ||
| + | } | ||
| + | client-> | ||
| + | }); | ||
| + | server.addHandler(& | ||
| + | |||
| + | // Start ElegantOTA | ||
| + | ElegantOTA.begin(& | ||
| + | |||
| + | // Start server | ||
| + | server.begin(); | ||
| + | |||
| + | // Setup periodic time updates | ||
| + | timeClient.setUpdateInterval(60000); | ||
| + | } | ||
| + | |||
| + | 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(), | ||
| + | debugln(currentTime); | ||
| + | } | ||
| + | |||
| + | // Handle OTA updates | ||
| + | ElegantOTA.loop(); | ||
| + | } | ||
| + | </ | ||
basicotantpserverdebugwebserial.1740787677.txt.gz · Last modified: (external edit)
