PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

NodeMCU ESP8266 NIST Time Clock Plus Web Server

Go down

NodeMCU ESP8266 NIST Time Clock Plus Web Server Empty NodeMCU ESP8266 NIST Time Clock Plus Web Server

Post by jamied_uk 15th January 2017, 20:39

Code:


/*
   First Get library from https://github.com/SensorsIot/NTPtimeESP
   and upload library to arduino ide also you will need ESP8266WiFi library and esp8266WebServer libraries
  for wifi access!

   This sketch shows an example of sending a reading to data.sparkfun.com once per day.
   It uses the Sparkfun testing stream so the only customizing required is the WiFi SSID
   and password.
   The Harringay Maker Space
   License: Apache License v2
*/
#include <NTPtimeESP.h>
#include <ESP8266WiFi.h>
// Time Set
// http://www.pool.ntp.org/zone/gb
NTPtime NTPch("uk.pool.ntp.org"); // ch.pool.ntp.org

/*
 * The structure contains following fields:
 * struct strDateTime
{
  byte hour;
  byte minute;
  byte second;
  int year;
  byte month;
  byte day;
  byte dayofWeek;
  boolean valid;
};
 */
strDateTime dateTime;

int ledPin = 13; // GPIO13
int ledPin2 = 16; // GPIO16 onboard led
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  // Set Switch
  delay(10);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, HIGH);
  // ready...
  const char* ssid = "YOUR AP HERE";
  const char* password = "YOUR AP PASSWORD HERE";
  Serial.println();
  Serial.println("Booted");

  // 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 to connect: ");
  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 value = HIGH;
  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
    value = LOW;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin2, HIGH);
    value = HIGH;
  }
 
// Set ledPin according to the request
//digitalWrite(ledPin, value);
 
  // 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.print("Led pin is now: ");
 
  if(value == LOW) {
    client.print("On");
  } else {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />"); 
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
  // first parameter: Time zone in floating point (for India); second parameter: 1
  // for European summer time; 2 for US daylight saving time (not implemented yet)
  dateTime = NTPch.getNTPtime(1.0, 1);
 // Format
 // NTPch.printDateTime(dateTime);

  byte actualHour = dateTime.hour;
  byte actualMinute = dateTime.minute;
  byte actualsecond = dateTime.second;
  byte actualdayofWeek = dateTime.dayofWeek;
  byte actualday =dateTime.day;
  byte actualMonth = dateTime.month;
  int actualyear = dateTime.year;
  Serial.println("");
  Serial.print(dateTime.hour-1);
  Serial.print(":");
  Serial.print(dateTime.minute);
  Serial.print(":");
  Serial.print(dateTime.second);
  Serial.println("");
  Serial.print(dateTime.day);
  Serial.print(" ");
  Serial.print(dateTime.month);
  Serial.print(" ");
  Serial.print(dateTime.year);
  Serial.println("");
 
  delay(500);
}


Last edited by jamied_uk on 15th January 2017, 20:57; edited 2 times in total
jamied_uk
jamied_uk
Admin

Posts : 2943
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

NodeMCU ESP8266 NIST Time Clock Plus Web Server Empty Re: NodeMCU ESP8266 NIST Time Clock Plus Web Server

Post by jamied_uk 15th January 2017, 20:45

Another snippet for / instead of spaces



Code:
  Serial.println("");
  Serial.print(dateTime.hour-1);
  Serial.print(":");
  Serial.print(dateTime.minute);
  Serial.print(":");
  Serial.print(dateTime.second);
  Serial.println("");
  Serial.print(dateTime.day);
  Serial.print("/");
  Serial.print(dateTime.month);
  Serial.print("/");
  Serial.print(dateTime.year);
  Serial.println("");
jamied_uk
jamied_uk
Admin

Posts : 2943
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

NodeMCU ESP8266 NIST Time Clock Plus Web Server Empty Re: NodeMCU ESP8266 NIST Time Clock Plus Web Server

Post by jamied_uk 15th January 2017, 20:53

Add a title to your web server

Code:
  client.println("<title>Wireless Switch</title>");
jamied_uk
jamied_uk
Admin

Posts : 2943
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

NodeMCU ESP8266 NIST Time Clock Plus Web Server Empty Re: NodeMCU ESP8266 NIST Time Clock Plus Web Server

Post by jamied_uk 15th January 2017, 21:59

Pin Out Diagrams

instructables.com/id/Programming-ESP8266-ESP-12E-NodeMCU-Using-Arduino-

Time Alarms
pjrc.com/teensy/td_libs_TimeAlarms.html

Touch Screen Clock
bitquill.net/blog/esp8266-light-controller-clock


Last edited by jamied_uk on 15th January 2017, 23:02; edited 1 time in total
jamied_uk
jamied_uk
Admin

Posts : 2943
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

NodeMCU ESP8266 NIST Time Clock Plus Web Server Empty Re: NodeMCU ESP8266 NIST Time Clock Plus Web Server

Post by jamied_uk 15th January 2017, 22:12

More

jamied_uk
jamied_uk
Admin

Posts : 2943
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

NodeMCU ESP8266 NIST Time Clock Plus Web Server Empty Re: NodeMCU ESP8266 NIST Time Clock Plus Web Server

Post by jamied_uk 15th January 2017, 23:03

Now for adding more advanced features like alarm setting!


Code:
  client.println("<a href=\"/ALARM=AL10PM\"\"><button>Turn On Alarm For 22:00</button></a>");
  client.println("<a href=\"/ALARM=AL10AM\"\"><button>Turn On Alarm For 10:00</button></a>");

Code:
 int AL1 = 000;
  if (request.indexOf("/ALARM=AL10PM") != -1)  {
    // set alarm ALARM=AL10PM
    int AL1 = 22;
  }

  if (request.indexOf("/ALARM=AL10AM") != -1)  {
    // set alarm here!
    int AL1 = 10;
    
  }
jamied_uk
jamied_uk
Admin

Posts : 2943
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

NodeMCU ESP8266 NIST Time Clock Plus Web Server Empty Re: NodeMCU ESP8266 NIST Time Clock Plus Web Server

Post by jamied_uk 16th January 2017, 00:24

Putting It All Together (Unfinished Example).

Code:

/*
   This sketch shows an example of sending a reading to data.sparkfun.com once per day.
   It uses the Sparkfun testing stream so the only customizing required is the WiFi SSID
   and password.
   The Harringay Maker Space
   License: Apache License v2
*/
#include <NTPtimeESP.h>
#include <ESP8266WiFi.h>
// Time Set
// http://www.pool.ntp.org/zone/gb
NTPtime NTPch("uk.pool.ntp.org"); // ch.pool.ntp.org

/*
 * The structure contains following fields:
 * struct strDateTime
{
  byte hour;
  byte minute;
  byte second;
  int year;
  byte month;
  byte day;
  byte dayofWeek;
  boolean valid;
};
 */
strDateTime dateTime;

int ledPin = 13; // GPIO13
int ledPin2 = 16; // GPIO16 onboard led
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  // Set Switch
  delay(10);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, HIGH);
  // ready...
  const char* ssid = "YOUR AP ID";
  const char* password = "YOUR AP PASSWORD";
  Serial.println();
  Serial.println("Booted");

  // 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 to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
}

void loop() {
  int AL1 = 555;
  if (AL1==NULL)  {
  int AL1 = 555;
  }
 // 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 value = HIGH;
  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
    value = LOW;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin2, HIGH);
    value = HIGH;
  }
 
  if (request.indexOf("/ALARM=AL10PM") != -1)  {
    // set alarm ALARM=AL10PM
    int AL1 = 22;
  }

  if (request.indexOf("/ALARM=AL10AM") != -1)  {
    // set alarm here!
    int AL1 = 10;
   
  }
// Set ledPin according to the request
//digitalWrite(ledPin, value);
 
  // 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.print("Led Pin Is Now: ");
 
  if(value == LOW) {
    client.print("On");
  } else {
    client.print("Off");
  }
    client.print(" ");

  // ALARM SETTING!
  if(AL1 == 22) {
    client.print("Alarm Set To 22:00");
    Serial.println(AL1);
  } else if (AL1 == 10){
    client.print("Alarm Set To 10:00");
    Serial.println(AL1);
  }
   else if (AL1 = '555'){
    client.print("Alarm Not Set!");
    Serial.println("Alarm Not Set!");

  }
  client.println("<br><br>");
  client.println("<title>Wireless Switch</title>");
  client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");
  client.println("<br><p>");
  client.println("<form action=\"#\">");
  client.println("Alarm Setting:<br><input type=\"text\" name=\"alarm1\" value=\"hh:mm\">");
  client.println("<input type=\"submit\" value=\"Submit\"></form>");

  client.println("<a href=\"/ALARM=AL10PM\"\"><button>Turn On Alarm For 22:00</button></a>");
  client.println("<a href=\"/ALARM=AL10AM\"\"><button>Turn On Alarm For 10:00</button></a>");

  client.println("</html>");
 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
  // first parameter: Time zone in floating point (for India); second parameter: 1
  // for European summer time; 2 for US daylight saving time (not implemented yet)
  dateTime = NTPch.getNTPtime(1.0, 1);
 // Format
 // NTPch.printDateTime(dateTime);

  byte actualHour = dateTime.hour;
  byte actualMinute = dateTime.minute;
  byte actualsecond = dateTime.second;
  byte actualdayofWeek = dateTime.dayofWeek;
  byte actualday =dateTime.day;
  byte actualMonth = dateTime.month;
  int actualyear = dateTime.year;
  Serial.println("");
  Serial.print(dateTime.hour-1);
  Serial.print(":");
  Serial.print(dateTime.minute);
  Serial.print(":");
  Serial.print(dateTime.second);
  Serial.println("");
  Serial.print(dateTime.day);
  Serial.print("/");
  Serial.print(dateTime.month);
  Serial.print("/");
  Serial.print(dateTime.year);
  Serial.println("");
  http://Serial.println("Alarm Set For ");
  http://Serial.print(AL1);

  delay(500);
}
jamied_uk
jamied_uk
Admin

Posts : 2943
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

NodeMCU ESP8266 NIST Time Clock Plus Web Server Empty Re: NodeMCU ESP8266 NIST Time Clock Plus Web Server

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum