Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- const char* ssid = "MyRicarico";
- const char* password = "12341234";
- //Timezone 1 * 60 *60 = 3600
- const long utcOffsetInSeconds = 3600;
- //Days
- char days[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
- //Initialize time variables
- String day = "Nan" ;
- int hour = 0;
- int minutes = 0;
- int seconds = 0;
- // Define NTP Client to get time
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
- //Port 80
- WiFiServer server(80);
- // Var HTTP request
- String header;
- String reservedTime = "Nan";
- String reserved = "false";
- //
- unsigned long currentTime = millis();
- unsigned long previousTime = 0;
- const long timeoutTime = 2000;
- void setup() {
- Serial.begin(115200);
- // Connect to Wi-Fi network with SSID and password
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // Print local IP address and start web server
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- //Initialize Web Server and Time client
- server.begin();
- timeClient.begin();
- updateTime();
- }
- //Sync time
- void updateTime(){
- timeClient.update();
- day = days[timeClient.getDay()];
- hour = timeClient.getHours();
- minutes = timeClient.getMinutes();
- seconds = timeClient.getSeconds();
- }
- //clock
- void systemclock() {
- seconds++;
- if(seconds>=60)
- {
- seconds=0;
- minutes++;
- }
- if(minutes>=60)
- {
- seconds=0;
- minutes=0;
- hour++;
- }
- if(hour>=25)
- {
- seconds=0;
- minutes=0;
- hour=1;
- }
- delay(1000);
- }
- void loop(){
- systemclock();
- WiFiClient client = server.available(); // Listen for incoming clients
- if (client) {
- Serial.println("New Client.");
- String currentLine = "";
- currentTime = millis();
- previousTime = currentTime;
- while (client.connected() && currentTime - previousTime <= timeoutTime) {
- currentTime = millis();
- if (client.available()) {
- char c = client.read(); // read a byte, then
- Serial.write(c); // print it out the serial monitor
- header += c;
- if (c == '\n') {
- if (currentLine.length() == 0) {
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println("Connection: close");
- client.println();
- if (header.indexOf("GET /5/reserved") >= 0) {
- reservedTime = String(hour)+ ":" +String(minutes)+ ":" +String(seconds);
- reserved = "true";
- Serial.println("reserved: " + reservedTime);
- }
- if (header.indexOf("GET /5/nonReserved") >= 0) {
- reservedTime = "Nan";
- reserved = "false";
- Serial.println("reserved: " + reservedTime);
- }
- // Display the HTML web page
- client.println("<!DOCTYPE html><html>");
- client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
- client.println("<link rel=\"icon\" href=\"data:,\">");
- client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
- client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
- client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
- client.println(".button2 {background-color: #77878A;}</style></head>");
- // Web Page Heading
- client.println("<body><h1>ESP8266 Web Server</h1>");
- //timeDisplay
- client.println("<h1>Time & Day</h1><h3>"+String(day)+"</h3><h3>"+String(hour)+ ":" +String(minutes)+ ":" +String(seconds)+"</h3>");
- client.println("<p>Prenotazione</p>");
- if (reserved=="false") {
- client.println("<p><a href=\"/5/reserved\"><button class=\"button\">Prenota</button></a></p><h3>"+String(reservedTime)+"</h3>");
- } else {
- client.println("<p><a href=\"/5/nonReserved\"><button class=\"button button2\">Annulla</button></a></p>");
- }
- client.println("</body></html>");
- //<h3>"+String(reservedTime)+"</h3>");
- client.println();
- break;
- } else {
- currentLine = "";
- }
- } else if (c != '\r') {
- currentLine += c;
- }
- }
- }
- // Clear the header variable
- header = "";
- // Close the connection
- client.stop();
- Serial.println("Client disconnected.");
- Serial.println("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement