diff --git a/include/controller.h b/include/controller.h index b16931a..3989caf 100644 --- a/include/controller.h +++ b/include/controller.h @@ -35,6 +35,7 @@ private: unsigned long _last_rfid_scan_at = 0; unsigned long _last_position_info_at = 0; unsigned long _last_update_check_at = 0; + unsigned long _last_wifi_try_at = 0; String _serial_buffer = String(); String _cmd_queue = ""; void _execute_command_ls(String path); diff --git a/src/controller.cpp b/src/controller.cpp index 18e702d..f2c37df 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -1,4 +1,5 @@ #include "controller.h" +#include "main.h" #include "spi_master.h" #include "config.h" #include "playlist.h" @@ -53,12 +54,18 @@ void Controller::loop() { } #ifdef OTA_UPDATE_URL - if (!player->is_playing() && _last_update_check_atis_playing() && _last_update_check_at < now && _last_update_check_at + OTA_CHECK_INTERVAL < now) { Updater::run(); } else { - _last_update_check_at = millis(); + _last_update_check_at = now; } #endif + + if (!player->is_playing() && !WiFi.isConnected() && _last_wifi_try_at < now && _last_wifi_try_at + 5*60*1000 < now) { + wifi_connect(); + } else { + _last_wifi_try_at = now; + } TRACE("Controller::loop() done.\n"); } diff --git a/src/main.cpp b/src/main.cpp index e95d555..5233578 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include "main.h" #include "config.h" #include "controller.h" #include "player.h" @@ -32,6 +33,43 @@ bool connect_to_wifi(String ssid, String pass) { return true; } +void wifi_connect() { + bool connected = false; + INFO("Connecting to WiFi...\n"); + SPIMaster::select_sd(); + if (SD.exists("/_wifis.txt")) { + DEBUG("Reading /_wifis.txt\n"); + File f = SD.open("/_wifis.txt", "r"); + while (String line = f.readStringUntil('\n')) { + if (line.length()==0 || line.startsWith("#") || line.indexOf('=')==-1) { + continue; + } + String ssid = line.substring(0, line.indexOf('=')); + String pass = line.substring(line.indexOf('=')+1); + connected = connect_to_wifi(ssid, pass); + if (connected) break; + } + f.close(); + } else { + File f = SD.open("/_wifis.txt", "w"); + f.print("# WiFi definitions. Syntax: =. Lines starting with # are ignored. Example:\n# My WiFi=VerySecretPassword\n"); + f.close(); + } + SPIMaster::select_sd(false); + + if (!connected) { + #if defined(WIFI_SSID) and defined(WIFI_PASS) + DEBUG("Trying hardcoded WiFi data...\n"); + connected = connect_to_wifi(WIFI_SSID, WIFI_PASS); + #else + DEBUG("No hardcoded WiFi data set.\n"); + #endif + } + if (!connected) { + INFO("No WiFi connection!\n"); + } +} + void setup() { delay(500); Serial.begin(74880); @@ -81,41 +119,11 @@ void setup() { controller = new Controller(player, pm); INFO("Player and controller initialized.\n"); - bool connected = false; - INFO("Connecting to WiFi...\n"); - SPIMaster::select_sd(); - if (SD.exists("/_wifis.txt")) { - DEBUG("Reading /_wifis.txt\n"); - File f = SD.open("/_wifis.txt", "r"); - while (String line = f.readStringUntil('\n')) { - if (line.length()==0 || line.startsWith("#") || line.indexOf('=')==-1) { - continue; - } - String ssid = line.substring(0, line.indexOf('=')); - String pass = line.substring(line.indexOf('=')+1); - connected = connect_to_wifi(ssid, pass); - if (connected) break; - } - f.close(); - } else { - File f = SD.open("/_wifis.txt", "w"); - f.print("# WiFi definitions. Syntax: =. Lines starting with # are ignored. Example:\n# My WiFi=VerySecretPassword\n"); - f.close(); - } - SPIMaster::select_sd(false); + wifi_connect(); + + - if (!connected) { - #if defined(WIFI_SSID) and defined(WIFI_PASS) - DEBUG("Trying hardcoded WiFi data...\n"); - connected = connect_to_wifi(WIFI_SSID, WIFI_PASS); - #else - DEBUG("No hardcoded WiFi data set.\n"); - #endif - } - if (!connected) { - INFO("No WiFi connection!\n"); - } MDNS.begin("esmp3");