global.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef GLOBAL_H
  2. #define GLOBAL_H
  3. /* DEFAULT ACCESS POINT CONFIGURATION */
  4. #define ACCESS_POINT_NAME "ESP"
  5. #define ACCESS_POINT_PASSWORD "12345678"
  6. boolean AdminEnabled = false; // Enable Admin Mode for a given Time
  7. int AdminTimeOutCounter = 0; // Counter for Disabling the AdminMode
  8. #define AdminTimeOut 60 // Defines the Time in Seconds, when the Admin-Mode will be disabled
  9. /* UDP CONFIGURATION */
  10. int UKI_UDP_In_Port = 9000; //udp port input for ESP
  11. IPAddress UKI_UDP_Master_IP(192, 168, 0, 41); //default udp address to send to. Will automatically change to the ip sending something to udp in
  12. int GSR_sensor;
  13. int loop_counter;
  14. int counter;
  15. void ConfigureWifi()
  16. {
  17. Serial.println("Configuring Wifi");
  18. WiFi.begin (config.ssid.c_str(), config.password.c_str());
  19. if (!config.dhcp)
  20. {
  21. WiFi.config(IPAddress(config.IP[0],config.IP[1],config.IP[2],config.IP[3] ), IPAddress(config.Gateway[0],config.Gateway[1],config.Gateway[2],config.Gateway[3] ) , IPAddress(config.Netmask[0],config.Netmask[1],config.Netmask[2],config.Netmask[3] ));
  22. }
  23. }
  24. void setupWifi() {
  25. //if no config in eeprom, start as default access point
  26. if (!ReadConfig()) {
  27. WiFi.mode(WIFI_AP);
  28. WiFi.softAP( ACCESS_POINT_NAME , ACCESS_POINT_PASSWORD);
  29. Serial.println("Starting default access point (SSID:ESP, pwd:12345678)");
  30. Serial.println(WiFi.softAPIP());
  31. }
  32. //if config in eeprom, try and connect to wifi, if fail after timeout start access point with config.name SSID
  33. else {
  34. WiFi.mode(WIFI_STA);
  35. ConfigureWifi();
  36. Serial.println("Connecting to configured wifi network");
  37. while (WiFi.status() != 3) {
  38. counter++;
  39. Serial.print(".");
  40. Serial.println(WiFi.status());
  41. if (counter > 50) {
  42. Serial.println("failed");
  43. Serial.println("Starting as access point, SSID:"+ (String)config.DeviceName.c_str()+" pwd:" + (String)ACCESS_POINT_PASSWORD);
  44. WiFi.mode(WIFI_AP);
  45. //WiFi.softAP( ACCESS_POINT_NAME , ACCESS_POINT_PASSWORD);
  46. WiFi.softAP( config.DeviceName.c_str() , ACCESS_POINT_PASSWORD);
  47. Serial.println(WiFi.softAPIP());
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. void setupWifi_old(){
  54. if (!ReadConfig()) {
  55. // DEFAULT CONFIG
  56. config.ssid = "Freebox-6F7B3C";
  57. config.password = "accessorem6-gignendi7-insultare!";
  58. config.dhcp = true;
  59. config.IP[0] = 192; config.IP[1] = 168; config.IP[2] = 0; config.IP[3] = 100;
  60. config.Netmask[0] = 255; config.Netmask[1] = 255; config.Netmask[2] = 255; config.Netmask[3] = 0;
  61. config.Gateway[0] = 192; config.Gateway[1] = 168; config.Gateway[2] = 0; config.Gateway[3] = 254;
  62. config.ntpServerName = "0.de.pool.ntp.org";
  63. config.Update_Time_Via_NTP_Every = 0;
  64. config.timezone = -10;
  65. config.daylight = true;
  66. config.DeviceName = "UKI_ESP_default";
  67. config.AutoTurnOff = false;
  68. config.AutoTurnOn = false;
  69. config.TurnOffHour = 0;
  70. config.TurnOffMinute = 0;
  71. config.TurnOnHour = 0;
  72. config.TurnOnMinute = 0;
  73. WriteConfig();
  74. Serial.println("General config applied");
  75. }
  76. if (AdminEnabled) {
  77. WiFi.mode(WIFI_AP_STA);
  78. //WiFi.softAP( ACCESS_POINT_NAME , ACCESS_POINT_PASSWORD);
  79. WiFi.softAP( config.DeviceName.c_str() , ACCESS_POINT_PASSWORD);
  80. }
  81. else { WiFi.mode(WIFI_STA); }
  82. ConfigureWifi();
  83. while (WiFi.status() != 3) {
  84. Serial.println(WiFi.status());
  85. }
  86. // Serial.print(".");
  87. // digitalWrite(Blue_Led, Blue_Led_State);
  88. // Blue_Led_State = !Blue_Led_State;
  89. // yield();
  90. // }
  91. Serial.println(WiFi.status());
  92. /* A MODIFIER : pour le moment si AdminEnabled, lance pendant AdminTimeOut un AP en parallèle de la connection configurée
  93. * devrait être remplacé par - tente de se connecter au wifi configuré, si ok on continue normalement
  94. * - 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
  95. * - normalement redémarre après reconfiguration, donc ok
  96. * - eventuellement ajouter dans loop un redemarrage en cas de perte de connection
  97. */
  98. }
  99. #endif