From 504e0c382564158f58dccd7910faecedb72980c6 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sun, 10 Apr 2022 17:56:23 +0200 Subject: [PATCH] Implement WiFi connection timeout --- modules/control/Firmware/src/main.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/modules/control/Firmware/src/main.cpp b/modules/control/Firmware/src/main.cpp index b085407..662f3ec 100644 --- a/modules/control/Firmware/src/main.cpp +++ b/modules/control/Firmware/src/main.cpp @@ -9,7 +9,8 @@ #include #include -int STEPS = 5; +const int STEPS = 5; +const int WIFI_TIMEOUT = 20; BluetoothSerial bt; AsyncWebServer server(80); @@ -40,29 +41,41 @@ void registerTasks() xTaskCreate(websocketTask, "websocketTask", 10000, NULL, 1, NULL); } -void connectWifi() +void connectWifi(int timeout) { + int secondsPassed = 0; WiFi.begin(WIFI_SSID, WIFI_PW); Serial.print("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { - Serial.print("."); - delay(1000); + if (secondsPassed < timeout) + { + Serial.print("."); + delay(1000); + secondsPassed++; + } + else + { + Serial.println(""); + Serial.println("WiFi timed out"); + return; + } } + Serial.println(""); Serial.println(WiFi.localIP()); } void setup() { - int bjtCount = 4; + const int bjtCount = 4; const int bjtPin[bjtCount] = {SIG1A, SIG1B, SIG2A, SIG2B}; computer = new SerialCommunicator(Serial, 9600, 5, 50); phone = new BluetoothCommunicator(bt, 5, 50); light = new LightController(bjtPin, bjtCount); websocket = new WebsocketCommunicator(ws, server, 50); - connectWifi(); + connectWifi(WIFI_TIMEOUT); server.begin(); registerTasks(); }