functions.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. void ledBlink (int Led, int blink_qty, int blink_time) {
  2. for (int i = 0 ; i < blink_qty ; i++) {
  3. digitalWrite(Led, LOW) ;
  4. delay(blink_time);
  5. digitalWrite(Led, HIGH);
  6. delay(blink_time);
  7. }
  8. void startESP() {
  9. EEPROM.begin(512);
  10. Serial.begin(115200);
  11. pinMode(Blue_Led, OUTPUT);
  12. digitalWrite(Blue_Led, HIGH);
  13. pinMode(Red_Led, OUTPUT);
  14. digitalWrite(Red_Led, LOW);//red led on
  15. delay(500);
  16. Serial.println("Starting ES8266");
  17. }
  18. }
  19. void setupWifi(){
  20. if (!ReadConfig()) {
  21. // DEFAULT CONFIG
  22. config.ssid = "Freebox-6F7B3C";
  23. config.password = "accessorem6-gignendi7-insultare!";
  24. config.dhcp = true;
  25. config.IP[0] = 192; config.IP[1] = 168; config.IP[2] = 0; config.IP[3] = 100;
  26. config.Netmask[0] = 255; config.Netmask[1] = 255; config.Netmask[2] = 255; config.Netmask[3] = 0;
  27. config.Gateway[0] = 192; config.Gateway[1] = 168; config.Gateway[2] = 0; config.Gateway[3] = 254;
  28. config.ntpServerName = "0.de.pool.ntp.org";
  29. config.Update_Time_Via_NTP_Every = 0;
  30. config.timezone = -10;
  31. config.daylight = true;
  32. config.DeviceName = "UKI_ESP_default";
  33. config.AutoTurnOff = false;
  34. config.AutoTurnOn = false;
  35. config.TurnOffHour = 0;
  36. config.TurnOffMinute = 0;
  37. config.TurnOnHour = 0;
  38. config.TurnOnMinute = 0;
  39. WriteConfig();
  40. Serial.println("General config applied");
  41. }
  42. if (AdminEnabled) {
  43. WiFi.mode(WIFI_AP_STA);
  44. //WiFi.softAP( ACCESS_POINT_NAME , ACCESS_POINT_PASSWORD);
  45. WiFi.softAP( config.DeviceName.c_str() , ACCESS_POINT_PASSWORD);
  46. }
  47. else { WiFi.mode(WIFI_STA); }
  48. ConfigureWifi();
  49. while (WiFi.status() != 3) {
  50. Serial.println(WiFi.status());
  51. }
  52. // Serial.print(".");
  53. // digitalWrite(Blue_Led, Blue_Led_State);
  54. // Blue_Led_State = !Blue_Led_State;
  55. // yield();
  56. // }
  57. Serial.println(WiFi.status());
  58. /* A MODIFIER : pour le moment si AdminEnabled, lance pendant AdminTimeOut un AP en parallèle de la connection configurée
  59. * devrait être remplacé par - tente de se connecter au wifi configuré, si ok on continue normalement
  60. * - si n'arrive pas à se connecter au wifi configuré, passe en mode AP avec webserver pour reconfigurer le wifi / ou demarrer en mode AP si bouton utilisateur appuyé pendant démarragae
  61. * - normalement redémarre après reconfiguration, donc ok
  62. * - eventuellement ajouter dans loop un redemarrage en cas de perte de connection
  63. */
  64. }
  65. void setupWebserver(){
  66. server.on ( "/", []() {digitalWrite(Blue_Led, LOW); Serial.println("admin.html"); server.send ( 200, "text/html", PAGE_AdminMainPage ); digitalWrite(Blue_Led, HIGH); } );
  67. server.on ( "/admin.html", []() {digitalWrite(Blue_Led, LOW); Serial.println("admin.html"); server.send ( 200, "text/html", PAGE_AdminMainPage ); digitalWrite(Blue_Led, HIGH); } );
  68. server.on ( "/config.html", send_network_configuration_html );
  69. server.on ( "/info.html", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("info.html"); server.send ( 200, "text/html", PAGE_Information ); digitalWrite(Blue_Led, HIGH) ; } );
  70. server.on ( "/ntp.html", send_NTP_configuration_html );
  71. server.on ( "/general.html", send_general_html );
  72. server.on ( "/style.css", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("style.css"); server.send ( 200, "text/plain", PAGE_Style_css ); digitalWrite(Blue_Led, HIGH) ; } );
  73. server.on ( "/microajax.js", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("microajax.js"); server.send ( 200, "text/plain", PAGE_microajax_js ); digitalWrite(Blue_Led, HIGH); } );
  74. server.on ( "/admin/values", send_network_configuration_values_html );
  75. server.on ( "/admin/connectionstate", send_connection_state_values_html );
  76. server.on ( "/admin/infovalues", send_information_values_html );
  77. server.on ( "/admin/ntpvalues", send_NTP_configuration_values_html );
  78. server.on ( "/admin/generalvalues", send_general_configuration_values_html);
  79. server.on ( "/admin/devicename", send_devicename_value_html);
  80. server.onNotFound ( []() {
  81. Serial.println("Page Not Found");
  82. server.send ( 400, "text/html", "Page not Found" );
  83. ledBlink(Blue_Led, 10, 100);
  84. } );
  85. server.begin();
  86. Serial.println( "HTTP server started" );
  87. tkSecond.attach(1, Second_Tick);
  88. UDPNTPClient.begin(2390); // Port for NTP receive
  89. }
  90. void loopWebserver(){
  91. if (AdminEnabled) {
  92. if (AdminTimeOutCounter > AdminTimeOut) {
  93. AdminEnabled = false;
  94. Serial.println("Admin Mode disabled!");
  95. WiFi.mode(WIFI_STA);
  96. }
  97. }
  98. if (config.Update_Time_Via_NTP_Every > 0 ) {
  99. if (cNTP_Update > 5 && firstStart) {
  100. NTPRefresh();
  101. cNTP_Update = 0;
  102. firstStart = false;
  103. }
  104. else if ( cNTP_Update > (config.Update_Time_Via_NTP_Every * 60) ) {
  105. NTPRefresh();
  106. cNTP_Update = 0;
  107. }
  108. }
  109. if (DateTime.minute != Minute_Old) {
  110. Minute_Old = DateTime.minute;
  111. if (config.AutoTurnOn)
  112. if (DateTime.hour == config.TurnOnHour && DateTime.minute == config.TurnOnMinute) {
  113. Serial.println("SwitchON");
  114. }
  115. }
  116. Minute_Old = DateTime.minute;
  117. if (config.AutoTurnOff) {
  118. if (DateTime.hour == config.TurnOffHour && DateTime.minute == config.TurnOffMinute) {
  119. Serial.println("SwitchOff");
  120. }
  121. }
  122. }
  123. void setupOTA(){
  124. ArduinoOTA.onStart([]() {
  125. Serial.println("Start");
  126. digitalWrite(Red_Led, HIGH);
  127. digitalWrite(Blue_Led, LOW);
  128. delay(1000);
  129. digitalWrite(Blue_Led, HIGH);
  130. delay(500);
  131. });
  132. ArduinoOTA.onEnd([]() {
  133. Serial.println("End");
  134. digitalWrite(Blue_Led, HIGH);
  135. delay(1000);
  136. ledBlink(Blue_Led, 3, 100);
  137. });
  138. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  139. Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
  140. digitalWrite(Blue_Led, Blue_Led_State);
  141. Blue_Led_State = !Blue_Led_State;
  142. });
  143. ArduinoOTA.onError([](ota_error_t error) {
  144. Serial.printf("Error[%u]: ", error);
  145. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  146. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  147. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  148. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  149. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  150. digitalWrite(Blue_Led, HIGH);
  151. });
  152. ArduinoOTA.begin();
  153. }
  154. void loopHandles(){
  155. if (loop_counter == 10) {
  156. loop_counter = 0;
  157. server.handleClient();
  158. Red_Led_State = !Red_Led_State;
  159. digitalWrite(Red_Led, Red_Led_State);
  160. delay(10);
  161. }
  162. if (loop_counter == 5) {
  163. ArduinoOTA.handle();
  164. delay(10);
  165. }
  166. loop_counter += 1;
  167. if (Refresh) {
  168. Refresh = false;
  169. ///Serial.println("Refreshing...");
  170. //Serial.printf("FreeMem:%d %d:%d:%d %d.%d.%d \n",ESP.getFreeHeap() , DateTime.hour,DateTime.minute, DateTime.second, DateTime.year, DateTime.month, DateTime.day);
  171. }
  172. }