Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/HTTP_Server_Basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class HTTP_Server {
};

// wifi Function
bool isWifiDisabled();
void startWifi();
void stopWifi();

Expand Down
5 changes: 5 additions & 0 deletions src/DirConManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ static char uuidListBuffer[128] = "";
static size_t uuidListLength = 0;

bool DirConManager::start() {
if (isWifiDisabled()) {
SS2K_LOG(DIRCON_LOG_TAG, "DirCon service not started - WiFi disabled");
return false;
}

if (!started) {
// Initialize buffers
for (int i = 0; i < DIRCON_MAX_CLIENTS; i++) {
Expand Down
49 changes: 45 additions & 4 deletions src/HTTP_Server_Basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, true);
WiFi.setAutoReconnect(false);
WiFi.mode(WIFI_MODE_NULL);
httpServer.internetConnection = false;
}

void _staSetup() {
WiFi.setHostname(userConfig->getDeviceName());
WiFi.mode(WIFI_STA);
Expand All @@ -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;
}
Comment on lines +73 to +78
Comment on lines +73 to +78

// Trying Station mode first:
if (strcmp(userConfig->getSsid(), DEVICE_NAME) != 0) {
SS2K_LOG(HTTP_SERVER_LOG_TAG, "Connecting to: %s", userConfig->getSsid());
Expand Down Expand Up @@ -134,13 +155,21 @@ 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() {
// Don't start HTTP server if WiFi is disabled
if (isWifiDisabled()) {
SS2K_LOG(HTTP_SERVER_LOG_TAG, "HTTP server not started - WiFi disabled");
return;
}

server.enableCORS(true);
server.onNotFound(handleIndexFile);

Expand Down Expand Up @@ -413,6 +442,11 @@ void HTTP_Server::start() {
}

void HTTP_Server::webClientUpdate() {
// Don't handle web clients if WiFi is disabled
if (isWifiDisabled()) {
return;
}

static unsigned long int _webClientTimer = millis();
if (millis() - _webClientTimer > WEBSERVER_DELAY) {
_webClientTimer = millis();
Expand Down Expand Up @@ -647,6 +681,13 @@ void HTTP_Server::stop() {
// 70:94:DE:DD:E6:C4:69:48:3A:92:70:A1:48:56:78:2D:18:64:E0:B7

void HTTP_Server::FirmwareUpdate() {
// Skip firmware update if WiFi is disabled
if (isWifiDisabled()) {
SS2K_LOG(HTTP_SERVER_LOG_TAG, "Firmware update skipped - WiFi disabled");
httpServer.internetConnection = false;
return;
}

HTTPClient http;
WiFiClientSecure localClient;
localClient.setCACert(rootCACertificate);
Expand Down
18 changes: 11 additions & 7 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,22 @@ 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");
if (!isWifiDisabled()) {
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");
}
}

#ifdef TEST_PTAB4PWR
Expand Down
4 changes: 2 additions & 2 deletions src/UdpAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
void UdpAppender::Initialize() {}

void UdpAppender::Log(const char *message) {
if (WiFi.status() == WL_CONNECTED && userConfig->getUdpLogEnabled()) {
if (!isWifiDisabled() && WiFi.status() == WL_CONNECTED && userConfig->getUdpLogEnabled()) {
this->udp.beginPacket("255.255.255.255", this->port);
this->udp.write((uint8_t *)message, strlen(message));
this->udp.endPacket();
}
}
}
17 changes: 15 additions & 2 deletions src/WebsocketAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@

// see: https://github.com/gilmaimon/ArduinoWebsockets
#include "WebsocketAppender.h"
#include "HTTP_Server_Basic.h"

WebSocketAppender::WebSocketAppender() {
for (uint8_t index = 0; index < maxClients; index++) {
_clients[index] = NULL;
}
}

void WebSocketAppender::Initialize() { _webSocketsServer.listen(WebSocketAppender::port); }
void WebSocketAppender::Initialize() {
if (!isWifiDisabled()) {
_webSocketsServer.listen(WebSocketAppender::port);
}
}
void WebSocketAppender::Loop() {
if (isWifiDisabled()) {
return;
}

// CheckConnectedClients();
if (WiFi.status() == WL_CONNECTED && GetClientsCount() < maxClients) {
if (_webSocketsServer.poll() == false) {
Expand All @@ -29,6 +38,10 @@ void WebSocketAppender::Loop() {
}

void WebSocketAppender::Log(const char* message) {
if (isWifiDisabled()) {
return;
}

// Serial.println("Log websocket.");
// Serial.printf("%d clients connected.\n", GetClientsCount());

Expand Down Expand Up @@ -81,4 +94,4 @@ void WebSocketAppender::CheckConnectedClients() {
delete client;
}
}
}
}