Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 28 additions & 12 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, false);
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 All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
51 changes: 33 additions & 18 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -414,7 +429,7 @@ void SS2K::restartWifi() {
stopWifi();
delay(100);
startWifi();
httpServer.start();
startNetworkServices();
}

void SS2K::moveStepper() {
Expand Down
2 changes: 1 addition & 1 deletion src/UdpAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ void UdpAppender::Log(const char *message) {
this->udp.write((uint8_t *)message, strlen(message));
this->udp.endPacket();
}
}
}
3 changes: 1 addition & 2 deletions src/WebsocketAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,4 +80,4 @@ void WebSocketAppender::CheckConnectedClients() {
delete client;
}
}
}
}