From 3d43fa7a84805a718fb54fc573d01835cb309d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Kopi=C4=87?= Date: Tue, 5 Nov 2024 19:33:37 +0100 Subject: [PATCH] Add scaning wifi, make config page responsive --- src/netman.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++--- src/octoprint.h | 21 +++++++-------- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/netman.h b/src/netman.h index 4c767e0..dc6f336 100644 --- a/src/netman.h +++ b/src/netman.h @@ -7,7 +7,7 @@ #include // Constants for the HTML pages and config file -static const String beginHtml = "SmartCube Configure

Configure AP


"; +static const String beginHtml = "SmartCube Configure

Configure AP


"; static const String endHtml = "
"; static const String configFile = "/netman"; @@ -40,6 +40,7 @@ private: void handleRoot(); void handleAdd(); void handleRemove(); + void handleSelect(); }; // Initialization @@ -183,6 +184,7 @@ void netman::createAP() { server->on("/", std::bind(&netman::handleRoot, this)); server->on("/add", std::bind(&netman::handleAdd, this)); server->on("/remove", std::bind(&netman::handleRemove, this)); + server->on("/select", std::bind(&netman::handleSelect, this)); server->begin(); @@ -204,10 +206,20 @@ bool netman::redirectToIp() { return true; } -// Add SSID to config and save +// Modify the addSsid function to take parameters from the `select` page void netman::addSsid(String ssid, String password) { _ssids[ssid] = password; writeConfig(); + + // Attempt to connect to the selected network + if (tryConnectToSsid(ssid.c_str(), password.c_str())) { + // Redirect to the main page on success + server->sendHeader("Location", "/", true); + server->send(302, "text/plain", ""); + } else { + // Show error message if connection failed + server->send(200, "text/html", "

Connection failed. Please try again.

"); + } } // Remove SSID from config @@ -256,13 +268,47 @@ void netman::reset() { _ssids.clear(); } -// Web handlers for adding/removing SSIDs +String urlEncode(const String &str) { + String encoded = ""; + char c; + for (size_t i = 0; i < str.length(); i++) { + c = str.charAt(i); + if (isalnum(c)) { + encoded += c; + } else { + encoded += '%'; + char buf[3]; + snprintf(buf, sizeof(buf), "%02X", c); + encoded += buf; + } + } + return encoded; +} + void netman::handleRoot() { if (redirectToIp()) return; + + // Scan for available networks + int n = WiFi.scanNetworks(); String result = beginHtml; + + // Add stored SSIDs to the page + result += "

Saved Networks

"; for (const auto& item : _ssids) { result += "" + item.first + "-" + item.second + ""; } + + // Display available WiFi networks + result += "

Available Networks

"; + for (int i = 0; i < n; i++) { + // Get SSID and signal strength + String ssid = WiFi.SSID(i); + int rssi = WiFi.RSSI(i); + bool openNetwork = (WiFi.encryptionType(i) == ENC_TYPE_NONE); + + // Show network with button to select + result += ""; + } result += endHtml; server->send(200, "text/html", result); } @@ -278,3 +324,19 @@ void netman::handleRemove() { removeSsid(server->arg("ssid"), server->arg("pass")); handleRoot(); } + +// Add SSID to config and save +void netman::handleSelect() { + String ssid = server->arg("ssid"); + + // Display prompt for password if network is secured + String selectPage = "Connect to " + ssid + ""; + selectPage += "

Connect to " + ssid + "

"; + selectPage += ""; + selectPage += ""; + selectPage += "

"; + selectPage += ""; + selectPage += ""; + + server->send(200, "text/html", selectPage); +} \ No newline at end of file diff --git a/src/octoprint.h b/src/octoprint.h index 45c97dc..a0d23c9 100644 --- a/src/octoprint.h +++ b/src/octoprint.h @@ -138,19 +138,20 @@ bool fetchPrintingStatus(Adafruit_SSD1306& display) { display.fillRect(0, 26, displayWidth, 11, BLACK); // Check if it’s time to scroll based on the delay - if (millis() - lastScrollTime > scrollDelay) { - // Update scroll position based on the current direction - scrollPos += scrollDirection; - lastScrollTime = millis(); + if(fileNameWidth > 22) { + if (millis() - lastScrollTime > scrollDelay) { + // Update scroll position based on the current direction + scrollPos += scrollDirection; + lastScrollTime = millis(); - // Reverse direction if the text reaches either edge - if (scrollPos > 0) { // Reached the left edge - scrollDirection = -1; // Start moving left - } else if (scrollPos < -fileNameWidth + displayWidth) { // Reached the right edge - scrollDirection = 1; // Start moving right + // Reverse direction if the text reaches either edge + if (scrollPos > 0) { // Reached the left edge + scrollDirection = -1; // Start moving left + } else if (scrollPos < -fileNameWidth + displayWidth) { // Reached the right edge + scrollDirection = 1; // Start moving right + } } } - // Draw the file name with current scroll position display.setCursor(scrollPos, 27); display.print(fileName);
" + (openNetwork ? "(Open)" : "(Secured)") + "" + String(rssi) + " dBm