feature/netman #1

Merged
tomislav merged 12 commits from feature/netman into master 2024-11-05 19:43:41 +00:00
2 changed files with 76 additions and 13 deletions
Showing only changes of commit 3d43fa7a84 - Show all commits

View File

@ -7,7 +7,7 @@
#include <map>
// Constants for the HTML pages and config file
static const String beginHtml = "<!DOCTYPE html><html lang='en'><head><title>SmartCube Configure</title><style>body{font-family:Arial,sans-serif;background-color:#f4f4f9;color:#333;display:flex;align-items:center;justify-content:center;height:100vh;margin:0}.container{width:300px;padding:20px;background:#fff;border-radius:10px;box-shadow:0 4px 8px rgba(0,0,0,0.2)}h2{margin-top:0;text-align:center;color:#0073e6}label{display:block;margin-bottom:5px;font-weight:bold}input[type='text'],input[type='password']{width:100%;padding:8px;margin-bottom:15px;border:1px solid #ccc;border-radius:4px}button{width:100%;padding:10px;background-color:#0073e6;color:white;border:none;border-radius:4px;cursor:pointer;font-size:16px}button:hover{background-color:#005bb5}table{width:100%;margin-top:20px}td{padding:5px;text-align:left}</style></head><body><div class='container'><h2>Configure AP</h2><table><tbody><tr><td><label for='ssid'>SSID</label></td><td><input id='ssid' type='text' placeholder='Enter SSID'/></td></tr><tr><td><label for='pass'>Password</label></td><td><input id='pass' type='password' placeholder='Enter Password'/></td></tr><tr><td colspan='2'><button onclick=\"location.href = '/add?ssid=' + encodeURIComponent(document.getElementById('ssid').value) + '&pass=' + encodeURIComponent(document.getElementById('pass').value);\">Add Network</button></td></tr></tbody></table><br/><table><tbody>";
static const String beginHtml = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1.0'><title>SmartCube Configure</title><style>body{font-family:Arial,sans-serif;background-color:#f4f4f9;color:#333;display:flex;align-items:center;justify-content:center;height:100vh;margin:0}.container{width:100%;max-width:300px;padding:20px;background:#fff;border-radius:10px;box-shadow:0 4px 8px rgba(0,0,0,0.2);box-sizing:border-box}h2{margin-top:0;text-align:center;color:#0073e6}label{display:block;margin-bottom:5px;font-weight:bold}input[type='text'],input[type='password']{width:100%;padding:8px;margin-bottom:15px;border:1px solid #ccc;border-radius:4px;box-sizing:border-box}button{width:100%;padding:10px;background-color:#0073e6;color:white;border:none;border-radius:4px;cursor:pointer;font-size:16px}button:hover{background-color:#005bb5}table{width:100%;margin-top:20px}td{padding:5px;text-align:left}</style></head><body><div class='container'><h2>Configure AP</h2><table><tbody><tr><td><label for='ssid'>SSID</label></td><td><input id='ssid' type='text' placeholder='Enter SSID'/></td></tr><tr><td><label for='pass'>Password</label></td><td><input id='pass' type='password' placeholder='Enter Password'/></td></tr><tr><td colspan='2'><button onclick=\"location.href = '/add?ssid=' + encodeURIComponent(document.getElementById('ssid').value) + '&pass=' + encodeURIComponent(document.getElementById('pass').value);\">Add Network</button></td></tr></tbody></table><br/><table><tbody>";
static const String endHtml = "</tbody></table></body></html>";
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", "<html><body><h2>Connection failed. Please try again.</h2></body></html>");
}
}
// 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 += "<h3>Saved Networks</h3>";
for (const auto& item : _ssids) {
result += "<tr><td><button onclick=\"location.href='/remove?ssid=' + escape('" + item.first + "') + '&pass=' + escape('" + item.second + "') \">&times;</button></td><td>" + item.first + "</td><td>-</td><td>" + item.second + "</td></tr>";
}
// Display available WiFi networks
result += "</tbody></table><h3>Available Networks</h3><table><tbody>";
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 += "<tr><td><button onclick=\"location.href='/select?ssid=" + urlEncode(ssid) + "'\">" + ssid + "</button></td><td>" + (openNetwork ? "(Open)" : "(Secured)") + "</td><td>" + String(rssi) + " dBm</td></tr>";
}
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 = "<!DOCTYPE html><html lang='en'><head><title>Connect to " + ssid + "</title></head><body>";
selectPage += "<h2>Connect to " + ssid + "</h2>";
selectPage += "<form action='/add' method='get'>";
selectPage += "<input type='hidden' name='ssid' value='" + ssid + "'>";
selectPage += "<label>Password:</label><input type='password' name='pass'><br><br>";
selectPage += "<button type='submit'>Connect</button>";
selectPage += "</form></body></html>";
server->send(200, "text/html", selectPage);
}

View File

@ -138,6 +138,7 @@ bool fetchPrintingStatus(Adafruit_SSD1306& display) {
display.fillRect(0, 26, displayWidth, 11, BLACK);
// Check if its time to scroll based on the delay
if(fileNameWidth > 22) {
if (millis() - lastScrollTime > scrollDelay) {
// Update scroll position based on the current direction
scrollPos += scrollDirection;
@ -150,7 +151,7 @@ bool fetchPrintingStatus(Adafruit_SSD1306& display) {
scrollDirection = 1; // Start moving right
}
}
}
// Draw the file name with current scroll position
display.setCursor(scrollPos, 27);
display.print(fileName);