server and get time

im running a wemos D1 R1 and wondering if someone can point me towards guidance on how o run a server and get the time from a server. i’m planning on controlling lights on a time basis or sunset/sunrise basis but want the details and override on my phone so need a server. I’d appreciate some help pointing me to previous answers, ( if you’d like to know my skill level, it took me 20 mins to discover how to post !!!)

this is the server code I’m working/basing project on.

i think code was from Wemos webserver example - esp8266 learning

#include <ESP8266WiFi.h>
 
const char* ssid = "VM1923921";
const char* password = "fg3yvYqwgztn";
 
int ledPin1 = D4;
int ledPin2 = D11;
int ledPin3 = D12;
int ledPin4 = D13;
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
 
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
}
 
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  // Match the request
 
  int value1 = LOW;
  if (request.indexOf("/LED1=ON") != -1) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    value1 = HIGH;
  } 
  if (request.indexOf("/LED1=OFF") != -1){
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    value1 = LOW;
  }

    int value2 = LOW;
  if (request.indexOf("/LED2=ON") != -1) {
    digitalWrite(ledPin2, HIGH);
    
    value2 = HIGH;
  } 
  if (request.indexOf("/LED2=OFF") != -1){
    digitalWrite(ledPin2, LOW);
   
    value2 = LOW;
  }
 
 
 
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  
  client.println("<body><h1>hello world</h1></body>");
  
    client.println("<h2>-------------------------<\h2>");


  
  client.print("Led pin1 is now: ");
 
  if(value1 == HIGH) {
    client.print("On");  
  } else {
    client.print("Off");


  }
  client.println("

");
  client.println("<h3>Click <a href=\"/LED1=ON\">here</a> turn the LED on pin 4 ON <\h3>
");
  client.println("Click <a href=\"/LED1=OFF\">here</a> turn the LED on pin 4 OFF
");
  client.println("</html>");

  client.println("

");
  client.println("Click <a href=\"/LED2=ON\">here</a> turn the LED2 on pin 11 ON
");
  client.println("Click <a href=\"/LED2=OFF\">here</a> turn the LED2 on pin 11  OFF
");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");

Hi,

If you want to update only one field on the page without reloading the entire page:

If you need to have the time count on the server:

If you want to have a more precise time, synchronized worldwide:

Here is a tutorial on getting NTP time on an ESP8266. (The Wemos is in the ESP family, so the code should run with no changes).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.