Skip to content

Add wifi hostname change and persistence between reboot - #171

Open
vcarriere wants to merge 1 commit into
geo-tp:pioarduinofrom
vcarriere:hostname-change
Open

Add wifi hostname change and persistence between reboot#171
vcarriere wants to merge 1 commit into
geo-tp:pioarduinofrom
vcarriere:hostname-change

Conversation

@vcarriere

Copy link
Copy Markdown

Added the hostname handler in Wifi controller
Added logic in main.cpp to fetch hostname from NVS and apply it before the wifi is used
Added a argtransformer function to check if the string is alphanumeric only
Added the help description
Added globalstate for hostname

@vcarriere

Copy link
Copy Markdown
Author

Ok, I resolved all my previous issues and I think this is a clean way.

@geo-tp

geo-tp commented Aug 25, 2026

Copy link
Copy Markdown
Owner

Thanks for the contrib,

Could you first clarify the intended use case: setting a persistent hostname before connecting in Wi-Fi Client/Web UI mode, changing it at runtime, or both ?

void WifiController::handleHostname(const TerminalCommand &cmd)
{
    const std::string hostname = cmd.getSubcommand();

    if (hostname.empty()) {
        terminalView.println("Usage: hostname <name>");
        return;
    }

    if (hostname.length() > 32) {
        terminalView.println("Invalid hostname. Maximum 32 characters.");
        return;
    }

    if (hostname.front() == '-' || hostname.back() == '-') {
        terminalView.println("Invalid hostname. Cannot start or end with '-'.");
        return;
    }

    for (char c : hostname) {
        if (!(argTransformer.isValidAlphanumeric(std::string(1, c)) || c == '-')) {
            terminalView.println("Invalid hostname. Use letters, numbers, and '-' only, ");
            return;
        }
    }



    // Save hostname to NVS and set it
    nvsService.open();
    nvsService.saveString(GlobalState::getInstance().getNvsHostnameField(), hostname);
    nvsService.close();
    WiFi.setHostname(hostname.c_str());

    terminalView.println("WiFi: Hostname set to: " + wifiService.getHostname());
    terminalView.println("\n [⚠️  WARNING] ");
    terminalView.println(" Restart your device for hostname change to take effect.*\n");
}

WifiController should not call the Arduino WiFi API directly or know which library implements Wi-Fi. It should only interact with IWifiService

Also, it would be better to handle the hostname parsing and validation with something like argTransformer.isValidHostname`, even if the current way (validating in the controller) is also acceptable.

One last question: would it make more sense to configure the hostname when entering Wi-Fi mode or through config cmd, rather than exposing a standalone hostname command?

Since the hostname must be applied before connecting, changing it while using the Web CLI or when already connected would most likely require a reconnection to take effect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants