Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <NTPClient.h>
  3. #include <WiFiUdp.h>
  4.  
  5. const char* ssid = "MyRicarico";
  6. const char* password = "12341234";
  7.  
  8. //Timezone 1 * 60 *60 = 3600
  9. const long utcOffsetInSeconds = 3600;
  10.  
  11. //Days
  12. char days[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  13.  
  14. //Initialize time variables
  15. String day = "Nan" ;
  16. int hour = 0;
  17. int minutes = 0;
  18. int seconds = 0;
  19.  
  20. // Define NTP Client to get time
  21. WiFiUDP ntpUDP;
  22. NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
  23.  
  24. //Port 80
  25. WiFiServer server(80);
  26.  
  27. // Var HTTP request
  28. String header;
  29.  
  30.  
  31. String reservedTime = "Nan";
  32. String reserved = "false";
  33. //
  34. unsigned long currentTime = millis();
  35.  
  36. unsigned long previousTime = 0;
  37.  
  38. const long timeoutTime = 2000;
  39.  
  40. void setup() {
  41. Serial.begin(115200);
  42.  
  43.  
  44. // Connect to Wi-Fi network with SSID and password
  45. Serial.print("Connecting to ");
  46. Serial.println(ssid);
  47. WiFi.begin(ssid, password);
  48. while (WiFi.status() != WL_CONNECTED) {
  49. delay(500);
  50. Serial.print(".");
  51. }
  52. // Print local IP address and start web server
  53. Serial.println("");
  54. Serial.println("WiFi connected.");
  55. Serial.println("IP address: ");
  56. Serial.println(WiFi.localIP());
  57.  
  58. //Initialize Web Server and Time client
  59. server.begin();
  60. timeClient.begin();
  61. updateTime();
  62. }
  63.  
  64. //Sync time
  65. void updateTime(){
  66. timeClient.update();
  67. day = days[timeClient.getDay()];
  68. hour = timeClient.getHours();
  69. minutes = timeClient.getMinutes();
  70. seconds = timeClient.getSeconds();
  71. }
  72. //clock
  73. void systemclock() {
  74. seconds++;
  75. if(seconds>=60)
  76. {
  77. seconds=0;
  78. minutes++;
  79. }
  80. if(minutes>=60)
  81. {
  82. seconds=0;
  83. minutes=0;
  84. hour++;
  85. }
  86. if(hour>=25)
  87. {
  88. seconds=0;
  89. minutes=0;
  90. hour=1;
  91.  
  92. }
  93. delay(1000);
  94. }
  95.  
  96.  
  97. void loop(){
  98. systemclock();
  99.  
  100. WiFiClient client = server.available(); // Listen for incoming clients
  101. if (client) {
  102. Serial.println("New Client.");
  103. String currentLine = "";
  104. currentTime = millis();
  105. previousTime = currentTime;
  106. while (client.connected() && currentTime - previousTime <= timeoutTime) {
  107. currentTime = millis();
  108. if (client.available()) {
  109. char c = client.read(); // read a byte, then
  110. Serial.write(c); // print it out the serial monitor
  111. header += c;
  112. if (c == '\n') {
  113.  
  114.  
  115. if (currentLine.length() == 0) {
  116.  
  117. client.println("HTTP/1.1 200 OK");
  118. client.println("Content-type:text/html");
  119. client.println("Connection: close");
  120. client.println();
  121.  
  122.  
  123. if (header.indexOf("GET /5/reserved") >= 0) {
  124. reservedTime = String(hour)+ ":" +String(minutes)+ ":" +String(seconds);
  125. reserved = "true";
  126. Serial.println("reserved: " + reservedTime);
  127. }
  128. if (header.indexOf("GET /5/nonReserved") >= 0) {
  129. reservedTime = "Nan";
  130. reserved = "false";
  131. Serial.println("reserved: " + reservedTime);
  132. }
  133.  
  134.  
  135. // Display the HTML web page
  136. client.println("<!DOCTYPE html><html>");
  137. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  138. client.println("<link rel=\"icon\" href=\"data:,\">");
  139.  
  140.  
  141. client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
  142. client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
  143. client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
  144. client.println(".button2 {background-color: #77878A;}</style></head>");
  145.  
  146. // Web Page Heading
  147. client.println("<body><h1>ESP8266 Web Server</h1>");
  148.  
  149. //timeDisplay
  150. client.println("<h1>Time & Day</h1><h3>"+String(day)+"</h3><h3>"+String(hour)+ ":" +String(minutes)+ ":" +String(seconds)+"</h3>");
  151.  
  152.  
  153. client.println("<p>Prenotazione</p>");
  154. if (reserved=="false") {
  155. client.println("<p><a href=\"/5/reserved\"><button class=\"button\">Prenota</button></a></p><h3>"+String(reservedTime)+"</h3>");
  156. } else {
  157. client.println("<p><a href=\"/5/nonReserved\"><button class=\"button button2\">Annulla</button></a></p>");
  158. }
  159.  
  160. client.println("</body></html>");
  161.  
  162. //<h3>"+String(reservedTime)+"</h3>");
  163. client.println();
  164.  
  165. break;
  166. } else {
  167. currentLine = "";
  168. }
  169. } else if (c != '\r') {
  170. currentLine += c;
  171. }
  172. }
  173. }
  174. // Clear the header variable
  175. header = "";
  176. // Close the connection
  177. client.stop();
  178. Serial.println("Client disconnected.");
  179. Serial.println("");
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement