Try to (re)connect to WiFi every 5 minutes. (Only when not playing at the moment.)

This commit is contained in:
Fabian Schlenz 2019-11-29 05:45:21 +01:00
parent 63b9616677
commit 196021bef5
3 changed files with 51 additions and 35 deletions

View File

@ -35,6 +35,7 @@ private:
unsigned long _last_rfid_scan_at = 0; unsigned long _last_rfid_scan_at = 0;
unsigned long _last_position_info_at = 0; unsigned long _last_position_info_at = 0;
unsigned long _last_update_check_at = 0; unsigned long _last_update_check_at = 0;
unsigned long _last_wifi_try_at = 0;
String _serial_buffer = String(); String _serial_buffer = String();
String _cmd_queue = ""; String _cmd_queue = "";
void _execute_command_ls(String path); void _execute_command_ls(String path);

View File

@ -1,4 +1,5 @@
#include "controller.h" #include "controller.h"
#include "main.h"
#include "spi_master.h" #include "spi_master.h"
#include "config.h" #include "config.h"
#include "playlist.h" #include "playlist.h"
@ -53,12 +54,18 @@ void Controller::loop() {
} }
#ifdef OTA_UPDATE_URL #ifdef OTA_UPDATE_URL
if (!player->is_playing() && _last_update_check_at<millis() && _last_update_check_at + OTA_CHECK_INTERVAL < millis()) { if (!player->is_playing() && _last_update_check_at < now && _last_update_check_at + OTA_CHECK_INTERVAL < now) {
Updater::run(); Updater::run();
} else { } else {
_last_update_check_at = millis(); _last_update_check_at = now;
} }
#endif #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"); TRACE("Controller::loop() done.\n");
} }

View File

@ -4,6 +4,7 @@
#include <WiFi.h> #include <WiFi.h>
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include <SPIFFS.h> #include <SPIFFS.h>
#include "main.h"
#include "config.h" #include "config.h"
#include "controller.h" #include "controller.h"
#include "player.h" #include "player.h"
@ -32,6 +33,43 @@ bool connect_to_wifi(String ssid, String pass) {
return true; 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: <SSID>=<PASS>. 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() { void setup() {
delay(500); delay(500);
Serial.begin(74880); Serial.begin(74880);
@ -81,41 +119,11 @@ void setup() {
controller = new Controller(player, pm); controller = new Controller(player, pm);
INFO("Player and controller initialized.\n"); INFO("Player and controller initialized.\n");
bool connected = false; wifi_connect();
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: <SSID>=<PASS>. 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");
}
MDNS.begin("esmp3"); MDNS.begin("esmp3");