web.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // The Webserver
  2. /* NTP things */
  3. boolean firstStart = true; // On firststart = true, NTP will try to get a valid time
  4. strDateTime DateTime; // Global DateTime structure, will be refreshed every Second
  5. WiFiUDP UDPNTPClient; // NTP Client
  6. unsigned long UnixTimestamp = 0; // GLOBALTIME ( Will be set by NTP)
  7. boolean Refresh = false; // For Main Loop, to refresh things like GPIO / WS2812
  8. int cNTP_Update = 0; // Counter for Updating the time via NTP
  9. Ticker tkSecond; // Second - Timer for Updating Datetime Structure
  10. byte Minute_Old = 100; // Helpvariable for checking, when a new Minute comes up (for Auto Turn On / Off)
  11. /*
  12. Include the HTML, STYLE and Script "Pages"
  13. */
  14. #include "web_Root.h"
  15. #include "web_Admin.h"
  16. #include "web_Script.js.h"
  17. #include "web_Style.css.h"
  18. #include "web_NTPSettings.h"
  19. #include "web_Information.h"
  20. #include "web_General.h"
  21. #include "web_NetworkConfiguration.h"
  22. /*
  23. **
  24. ** NTP
  25. **
  26. */
  27. const int NTP_PACKET_SIZE = 48;
  28. byte packetBuffer[ NTP_PACKET_SIZE];
  29. void NTPRefresh()
  30. {
  31. if (WiFi.status() == WL_CONNECTED)
  32. {
  33. IPAddress timeServerIP;
  34. WiFi.hostByName(config.ntpServerName.c_str(), timeServerIP);
  35. //sendNTPpacket(timeServerIP); // send an NTP packet to a time server
  36. Serial.println("sending NTP packet...");
  37. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  38. packetBuffer[0] = 0b11100011; // LI, Version, Mode
  39. packetBuffer[1] = 0; // Stratum, or type of clock
  40. packetBuffer[2] = 6; // Polling Interval
  41. packetBuffer[3] = 0xEC; // Peer Clock Precision
  42. packetBuffer[12] = 49;
  43. packetBuffer[13] = 0x4E;
  44. packetBuffer[14] = 49;
  45. packetBuffer[15] = 52;
  46. UDPNTPClient.beginPacket(timeServerIP, 123);
  47. UDPNTPClient.write(packetBuffer, NTP_PACKET_SIZE);
  48. UDPNTPClient.endPacket();
  49. delay(1000);
  50. int cb = UDPNTPClient.parsePacket();
  51. if (!cb) {
  52. Serial.println("NTP no packet yet");
  53. }
  54. else
  55. {
  56. Serial.print("NTP packet received, length=");
  57. Serial.println(cb);
  58. UDPNTPClient.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
  59. unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  60. unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  61. unsigned long secsSince1900 = highWord << 16 | lowWord;
  62. const unsigned long seventyYears = 2208988800UL;
  63. unsigned long epoch = secsSince1900 - seventyYears;
  64. UnixTimestamp = epoch;
  65. }
  66. }
  67. }
  68. void Second_Tick()
  69. {
  70. strDateTime tempDateTime;
  71. AdminTimeOutCounter++;
  72. cNTP_Update++;
  73. UnixTimestamp++;
  74. ConvertUnixTimeStamp(UnixTimestamp + (config.timezone * 360) , &tempDateTime);
  75. if (config.daylight) // Sommerzeit beachten
  76. if (summertime(tempDateTime.year,tempDateTime.month,tempDateTime.day,tempDateTime.hour,0))
  77. {
  78. ConvertUnixTimeStamp(UnixTimestamp + (config.timezone * 360) + 3600, &DateTime);
  79. }
  80. else
  81. {
  82. DateTime = tempDateTime;
  83. }
  84. else
  85. {
  86. DateTime = tempDateTime;
  87. }
  88. Refresh = true;
  89. }
  90. void setupWebserver(){
  91. server.on ( "/", []() {server.send ( 200, "text/html", reinterpret_cast<const __FlashStringHelper *>(web_AdminMainPage) ); } );
  92. server.on ( "/admin.html", []() {Serial.println("admin.html"); server.send ( 200, "text/html", reinterpret_cast<const __FlashStringHelper *>(web_AdminMainPage) ); } );
  93. server.on ( "/config.html", send_network_configuration_html );
  94. server.on ( "/info.html", []() { Serial.println("info.html"); server.send ( 200, "text/html", reinterpret_cast<const __FlashStringHelper *>(web_Information) ); } );
  95. server.on ( "/ntp.html", send_NTP_configuration_html );
  96. server.on ( "/general.html", send_general_html );
  97. server.on ( "/style.css", []() { Serial.println("style.css"); server.send ( 200, "text/plain", reinterpret_cast<const __FlashStringHelper *>(web_Style_css ));} );
  98. server.on ( "/microajax.js", []() { Serial.println("microajax.js"); server.send ( 200, "text/plain", reinterpret_cast<const __FlashStringHelper *>(web_microajax_js )); } );
  99. server.on ( "/admin/values", send_network_configuration_values_html );
  100. server.on ( "/admin/connectionstate", send_connection_state_values_html );
  101. server.on ( "/admin/infovalues", send_information_values_html );
  102. server.on ( "/admin/ntpvalues", send_NTP_configuration_values_html );
  103. server.on ( "/admin/generalvalues", send_general_configuration_values_html);
  104. //server.on ( "/admin/devicename", [] () {Serial.println("devicename"); send_devicename_value_html;});
  105. server.on ( "/admin/devicename", send_devicename_value_html);
  106. /* old led management
  107. server.on ( "/", []() {digitalWrite(Blue_Led, LOW); Serial.println("admin.html"); server.send ( 200, "text/html", web_AdminMainPage ); digitalWrite(Blue_Led, HIGH); } );
  108. server.on ( "/admin.html", []() {digitalWrite(Blue_Led, LOW); Serial.println("admin.html"); server.send ( 200, "text/html", web_AdminMainPage ); digitalWrite(Blue_Led, HIGH); } );
  109. server.on ( "/config.html", send_network_configuration_html );
  110. server.on ( "/info.html", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("info.html"); server.send ( 200, "text/html", web_Information ); digitalWrite(Blue_Led, HIGH) ; } );
  111. server.on ( "/ntp.html", send_NTP_configuration_html );
  112. server.on ( "/general.html", send_general_html );
  113. server.on ( "/style.css", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("style.css"); server.send ( 200, "text/plain", web_Style_css ); digitalWrite(Blue_Led, HIGH) ; } );
  114. server.on ( "/microajax.js", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("microajax.js"); server.send ( 200, "text/plain", web_microajax_js ); digitalWrite(Blue_Led, HIGH); } );
  115. server.on ( "/admin/values", send_network_configuration_values_html );
  116. server.on ( "/admin/connectionstate", send_connection_state_values_html );
  117. server.on ( "/admin/infovalues", send_information_values_html );
  118. server.on ( "/admin/ntpvalues", send_NTP_configuration_values_html );
  119. server.on ( "/admin/generalvalues", send_general_configuration_values_html);
  120. server.on ( "/admin/devicename", send_devicename_value_html);
  121. */
  122. server.onNotFound ( []() {
  123. Serial.println("Page Not Found");
  124. server.send ( 400, "text/html", "Page not Found" );
  125. } );
  126. server.begin();
  127. Serial.println( "HTTP server started" );
  128. tkSecond.attach(1, Second_Tick);
  129. UDPNTPClient.begin(2390); // Port for NTP receive
  130. }
  131. void loopWebserver(){
  132. // if (AdminEnabled) {
  133. // if (AdminTimeOutCounter > AdminTimeOut) {
  134. // AdminEnabled = false;
  135. // Serial.println("Admin Mode disabled!");
  136. // WiFi.mode(WIFI_STA);
  137. // }
  138. // }
  139. server.handleClient();
  140. // if (loop_counter == 10) {
  141. // loop_counter = 0;
  142. // server.handleClient();
  143. // Red_Led_State = !Red_Led_State;
  144. // digitalWrite(Red_Led, Red_Led_State);
  145. // delay(10);
  146. // }
  147. //
  148. if (config.Update_Time_Via_NTP_Every > 0 ) {
  149. if (cNTP_Update > 5 && firstStart) {
  150. NTPRefresh();
  151. cNTP_Update = 0;
  152. firstStart = false;
  153. }
  154. else if ( cNTP_Update > (config.Update_Time_Via_NTP_Every * 60) ) {
  155. NTPRefresh();
  156. cNTP_Update = 0;
  157. }
  158. }
  159. if (DateTime.minute != Minute_Old) {
  160. Minute_Old = DateTime.minute;
  161. if (config.AutoTurnOn)
  162. if (DateTime.hour == config.TurnOnHour && DateTime.minute == config.TurnOnMinute) {
  163. Serial.println("SwitchON");
  164. }
  165. }
  166. Minute_Old = DateTime.minute;
  167. if (config.AutoTurnOff) {
  168. if (DateTime.hour == config.TurnOffHour && DateTime.minute == config.TurnOffMinute) {
  169. Serial.println("SwitchOff");
  170. }
  171. }
  172. }