123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
-
- boolean firstStart = true;
- strDateTime DateTime;
- WiFiUDP UDPNTPClient;
- unsigned long UnixTimestamp = 0;
- boolean Refresh = false;
- int cNTP_Update = 0;
- Ticker tkSecond;
- byte Minute_Old = 100;
- #include "web_Root.h"
- #include "web_Admin.h"
- #include "web_Script.js.h"
- #include "web_Style.css.h"
- #include "web_NTPSettings.h"
- #include "web_Information.h"
- #include "web_General.h"
- #include "web_NetworkConfiguration.h"
- const int NTP_PACKET_SIZE = 48;
- byte packetBuffer[ NTP_PACKET_SIZE];
- void NTPRefresh()
- {
-
- if (WiFi.status() == WL_CONNECTED)
- {
- IPAddress timeServerIP;
- WiFi.hostByName(config.ntpServerName.c_str(), timeServerIP);
-
- Serial.println("sending NTP packet...");
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
- packetBuffer[0] = 0b11100011;
- packetBuffer[1] = 0;
- packetBuffer[2] = 6;
- packetBuffer[3] = 0xEC;
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
- UDPNTPClient.beginPacket(timeServerIP, 123);
- UDPNTPClient.write(packetBuffer, NTP_PACKET_SIZE);
- UDPNTPClient.endPacket();
- delay(1000);
-
- int cb = UDPNTPClient.parsePacket();
- if (!cb) {
- Serial.println("NTP no packet yet");
- }
- else
- {
- Serial.print("NTP packet received, length=");
- Serial.println(cb);
- UDPNTPClient.read(packetBuffer, NTP_PACKET_SIZE);
- unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
- unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
- unsigned long secsSince1900 = highWord << 16 | lowWord;
- const unsigned long seventyYears = 2208988800UL;
- unsigned long epoch = secsSince1900 - seventyYears;
- UnixTimestamp = epoch;
- }
- }
- }
- void Second_Tick()
- {
- strDateTime tempDateTime;
- AdminTimeOutCounter++;
- cNTP_Update++;
- UnixTimestamp++;
- ConvertUnixTimeStamp(UnixTimestamp + (config.timezone * 360) , &tempDateTime);
- if (config.daylight)
- if (summertime(tempDateTime.year,tempDateTime.month,tempDateTime.day,tempDateTime.hour,0))
- {
- ConvertUnixTimeStamp(UnixTimestamp + (config.timezone * 360) + 3600, &DateTime);
- }
- else
- {
- DateTime = tempDateTime;
- }
- else
- {
- DateTime = tempDateTime;
- }
- Refresh = true;
- }
- void setupWebserver(){
- server.on ( "/", []() {server.send ( 200, "text/html", reinterpret_cast<const __FlashStringHelper *>(web_AdminMainPage) ); } );
- server.on ( "/admin.html", []() {Serial.println("admin.html"); server.send ( 200, "text/html", reinterpret_cast<const __FlashStringHelper *>(web_AdminMainPage) ); } );
- server.on ( "/config.html", send_network_configuration_html );
- server.on ( "/info.html", []() { Serial.println("info.html"); server.send ( 200, "text/html", reinterpret_cast<const __FlashStringHelper *>(web_Information) ); } );
- server.on ( "/ntp.html", send_NTP_configuration_html );
- server.on ( "/general.html", send_general_html );
- server.on ( "/style.css", []() { Serial.println("style.css"); server.send ( 200, "text/plain", reinterpret_cast<const __FlashStringHelper *>(web_Style_css ));} );
- server.on ( "/microajax.js", []() { Serial.println("microajax.js"); server.send ( 200, "text/plain", reinterpret_cast<const __FlashStringHelper *>(web_microajax_js )); } );
- server.on ( "/admin/values", send_network_configuration_values_html );
- server.on ( "/admin/connectionstate", send_connection_state_values_html );
- server.on ( "/admin/infovalues", send_information_values_html );
- server.on ( "/admin/ntpvalues", send_NTP_configuration_values_html );
- server.on ( "/admin/generalvalues", send_general_configuration_values_html);
-
- server.on ( "/admin/devicename", send_devicename_value_html);
-
- server.onNotFound ( []() {
- Serial.println("Page Not Found");
- server.send ( 400, "text/html", "Page not Found" );
- } );
- server.begin();
- Serial.println( "HTTP server started" );
- tkSecond.attach(1, Second_Tick);
- UDPNTPClient.begin(2390);
- }
- void loopWebserver(){
- server.handleClient();
- if (config.Update_Time_Via_NTP_Every > 0 ) {
- if (cNTP_Update > 5 && firstStart) {
- NTPRefresh();
- cNTP_Update = 0;
- firstStart = false;
- }
- else if ( cNTP_Update > (config.Update_Time_Via_NTP_Every * 60) ) {
- NTPRefresh();
- cNTP_Update = 0;
- }
- }
- if (DateTime.minute != Minute_Old) {
- Minute_Old = DateTime.minute;
- if (config.AutoTurnOn)
- if (DateTime.hour == config.TurnOnHour && DateTime.minute == config.TurnOnMinute) {
- Serial.println("SwitchON");
- }
- }
- Minute_Old = DateTime.minute;
- if (config.AutoTurnOff) {
- if (DateTime.hour == config.TurnOffHour && DateTime.minute == config.TurnOffMinute) {
- Serial.println("SwitchOff");
- }
- }
- }
-
|