diff --git a/include/HTTP_Server_Basic.h b/include/HTTP_Server_Basic.h index 4fb684a7..06a166da 100644 --- a/include/HTTP_Server_Basic.h +++ b/include/HTTP_Server_Basic.h @@ -31,6 +31,7 @@ class HTTP_Server { }; // wifi Function +bool isWifiDisabled(); void startWifi(); void stopWifi(); diff --git a/src/HTTP_Server_Basic.cpp b/src/HTTP_Server_Basic.cpp index b492d928..dbbf21cb 100644 --- a/src/HTTP_Server_Basic.cpp +++ b/src/HTTP_Server_Basic.cpp @@ -34,6 +34,20 @@ DNSServer dnsServer; HTTP_Server httpServer; WebServer server(80); +bool isWifiDisabled() { + return strcmp(userConfig->getSsid(), "") == 0 || strcmp(userConfig->getSsid(), "none") == 0; +} + +static void powerDownWifi() { + DirConManager::stop(); + dnsServer.stop(); + MDNS.end(); + WiFi.disconnect(true, false); + WiFi.setAutoReconnect(false); + WiFi.mode(WIFI_MODE_NULL); + httpServer.internetConnection = false; +} + void _staSetup() { WiFi.setHostname(userConfig->getDeviceName()); WiFi.mode(WIFI_STA); @@ -56,6 +70,13 @@ void _APSetup() { void startWifi() { int i = 0; + // Check if WiFi is disabled via special SSID values + if (isWifiDisabled()) { + SS2K_LOG(HTTP_SERVER_LOG_TAG, "WiFi disabled via SSID configuration: '%s'", userConfig->getSsid()); + powerDownWifi(); + return; + } + // Trying Station mode first: if (strcmp(userConfig->getSsid(), DEVICE_NAME) != 0) { SS2K_LOG(HTTP_SERVER_LOG_TAG, "Connecting to: %s", userConfig->getSsid()); @@ -66,7 +87,7 @@ void startWifi() { i++; if (i > WIFI_CONNECT_TIMEOUT) { SS2K_LOG(HTTP_SERVER_LOG_TAG, "Couldn't Connect. Switching to AP mode"); - WiFi.disconnect(true, true); + WiFi.disconnect(true, false); WiFi.setAutoReconnect(false); WiFi.mode(WIFI_MODE_NULL); delay(1000); @@ -110,13 +131,6 @@ void startWifi() { SS2K_LOG(HTTP_SERVER_LOG_TAG, "Connected to %s IP address: %s", userConfig->getSsid(), myIP.toString().c_str()); SS2K_LOG(HTTP_SERVER_LOG_TAG, "Open http://%s.local/", userConfig->getDeviceName()); - // Initialize DirCon MDNS service - if (DirConManager::start()) { - SS2K_LOG(HTTP_SERVER_LOG_TAG, "DirCon service started successfully"); - } else { - SS2K_LOG(HTTP_SERVER_LOG_TAG, "Error starting DirCon service"); - } - WiFi.setTxPower(WIFI_POWER_19_5dBm); if (WiFi.getMode() == WIFI_STA) { @@ -134,10 +148,12 @@ void startWifi() { } void stopWifi() { - SS2K_LOG(HTTP_SERVER_LOG_TAG, "Closing connection to: %s", userConfig->getSsid()); - // Stop DirCon service before disconnecting WiFi - DirConManager::stop(); - WiFi.disconnect(); + if (isWifiDisabled()) { + SS2K_LOG(HTTP_SERVER_LOG_TAG, "Stopping WiFi services while WiFi is disabled"); + } else { + SS2K_LOG(HTTP_SERVER_LOG_TAG, "Closing connection to: %s", userConfig->getSsid()); + } + powerDownWifi(); } void HTTP_Server::start() { diff --git a/src/Main.cpp b/src/Main.cpp index 9cf225e5..e77d4c27 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -52,6 +52,19 @@ RuntimeParameters *rtConfig = new RuntimeParameters; UdpAppender udpAppender; WebSocketAppender webSocketAppender; +static void startNetworkServices() { + if (!isWifiDisabled()) { + httpServer.start(); + + SS2K_LOG(MAIN_LOG_TAG, "Starting DirCon TCP service"); + if (DirConManager::start()) { + SS2K_LOG(MAIN_LOG_TAG, "DirCon TCP service started successfully"); + } else { + SS2K_LOG(MAIN_LOG_TAG, "Failed to start DirCon TCP service"); + } + } +} + ///////////// BEGIN SETUP ///////////// #ifndef UNIT_TEST @@ -116,7 +129,9 @@ extern "C" void app_main() { // HTTP setup because otherwise they use too much traffic and the device // fails to update which really sucks when it corrupts your settings. startWifi(); - httpServer.FirmwareUpdate(); + if (!isWifiDisabled()) { + httpServer.FirmwareUpdate(); + } pinMode(currentBoard.shiftUpPin, INPUT_PULLUP); // Push-Button with input Pullup pinMode(currentBoard.shiftDownPin, INPUT_PULLUP); // Push-Button with input Pullup @@ -139,19 +154,13 @@ extern "C" void app_main() { digitalWrite(LED_PIN, HIGH); // Configure and Initialize Logger - logHandler.addAppender(&webSocketAppender); - logHandler.addAppender(&udpAppender); + if (!isWifiDisabled()) { + logHandler.addAppender(&webSocketAppender); + logHandler.addAppender(&udpAppender); + } logHandler.initialize(); ss2k->startTasks(); - httpServer.start(); - - // Start DirCon TCP server for direct control over the bike trainer - SS2K_LOG(MAIN_LOG_TAG, "Starting DirCon TCP service"); - if (DirConManager::start()) { - SS2K_LOG(MAIN_LOG_TAG, "DirCon TCP service started successfully"); - } else { - SS2K_LOG(MAIN_LOG_TAG, "Failed to start DirCon TCP service"); - } + startNetworkServices(); #ifdef TEST_PTAB4PWR userConfig->setHMin(0); @@ -193,7 +202,9 @@ void SS2K::maintenanceLoop(void *pvParameters) { if ((millis() - bleTimer) > BLE_NOTIFY_DELAY) { BLECommunications(); logHandler.writeLogs(); - webSocketAppender.Loop(); + if (!isWifiDisabled()) { + webSocketAppender.Loop(); + } bleTimer = millis(); } // Don't do these if updating and in spindown mode. @@ -222,9 +233,11 @@ void SS2K::maintenanceLoop(void *pvParameters) { BLE_ss2kCustomCharacteristic::parseNemit(); // Update Zwift Gear UI if shift happened - httpServer.webClientUpdate(); - // Update DirCon protocol - DirConManager::update(); + if (!isWifiDisabled()) { + httpServer.webClientUpdate(); + // Update DirCon protocol + DirConManager::update(); + } // If we're in ERG mode, modify shift commands to inc/dec the target watts instead. // If we have a resistance bike attached, slow down when we're close to the limits. @@ -302,7 +315,9 @@ void SS2K::maintenanceLoop(void *pvParameters) { SS2K_LOG(MAIN_LOG_TAG, "Rebooting due to inactivity."); ss2k->rebootFlag = true; logHandler.writeLogs(); - webSocketAppender.Loop(); + if (!isWifiDisabled()) { + webSocketAppender.Loop(); + } } } else { @@ -414,7 +429,7 @@ void SS2K::restartWifi() { stopWifi(); delay(100); startWifi(); - httpServer.start(); + startNetworkServices(); } void SS2K::moveStepper() { diff --git a/src/UdpAppender.cpp b/src/UdpAppender.cpp index c27fe31f..91de7c71 100644 --- a/src/UdpAppender.cpp +++ b/src/UdpAppender.cpp @@ -16,4 +16,4 @@ void UdpAppender::Log(const char *message) { this->udp.write((uint8_t *)message, strlen(message)); this->udp.endPacket(); } -} \ No newline at end of file +} diff --git a/src/WebsocketAppender.cpp b/src/WebsocketAppender.cpp index bf715e1d..e0d2fc8d 100644 --- a/src/WebsocketAppender.cpp +++ b/src/WebsocketAppender.cpp @@ -7,7 +7,6 @@ // see: https://github.com/gilmaimon/ArduinoWebsockets #include "WebsocketAppender.h" - WebSocketAppender::WebSocketAppender() { for (uint8_t index = 0; index < maxClients; index++) { _clients[index] = NULL; @@ -81,4 +80,4 @@ void WebSocketAppender::CheckConnectedClients() { delete client; } } -} \ No newline at end of file +}