User Tools

Site Tools


basicotantpserverdebugwebserial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
basicotantpserverdebugwebserial [2025/03/01 00:07] – created jwanbasicotantpserverdebugwebserial [2025/10/10 10:15] (current) – external edit 127.0.0.1
Line 1: Line 1:
 Platform.ini Platform.ini
  
-[code]+<code>
 [env:nodemcuv2] [env:nodemcuv2]
 platform = espressif8266 platform = espressif8266
Line 19: Line 19:
   plageoj/UrlEncode @ ^1.0.1   plageoj/UrlEncode @ ^1.0.1
 build_flags=-DELEGANTOTA_USE_ASYNC_WEBSERVER=1   build_flags=-DELEGANTOTA_USE_ASYNC_WEBSERVER=1  
-[/code]+</code> 
 + 
 +Main 
 +<code> 
 +#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(); 
 +} 
 +</code>
basicotantpserverdebugwebserial.1740787677.txt.gz · Last modified: (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki