Skip to content

add wifi_shutdown_ap timer for 2 minute timeout #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 17, 2025
Merged
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
23 changes: 22 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,28 @@ void app_main(void) {
}
}

ESP_LOGI(TAG, "WiFi Connected, continuing main task thread . . . ");
// Create a timer to auto-shutdown the AP after 5 minutes
TimerHandle_t ap_shutdown_timer = xTimerCreate(
"ap_shutdown_timer",
pdMS_TO_TICKS(2 * 60 * 1000), // 2 minutes in milliseconds
pdFALSE, // One-shot timer
NULL, // No timer ID
wifi_shutdown_ap // Callback function (now with correct signature)
);

if (ap_shutdown_timer != NULL) {
// Timer created successfully, now try to start it
BaseType_t timer_started = xTimerStart(ap_shutdown_timer, 0);
if (timer_started == pdPASS) {
ESP_LOGI(TAG, "AP will automatically shut down in 2 minutes");
} else {
ESP_LOGE(TAG, "Failed to start AP shutdown timer");
// Clean up the timer if we couldn't start it
xTimerDelete(ap_shutdown_timer, 0);
}
} else {
ESP_LOGE(TAG, "Failed to create AP shutdown timer");
}

// Get the image URL from WiFi manager
const char* image_url = wifi_get_image_url();
Expand Down
93 changes: 53 additions & 40 deletions src/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,44 +62,49 @@ static void (*s_connect_callback)(void) = NULL;
static void (*s_disconnect_callback)(void) = NULL;

// HTML for the configuration page
static const char *s_html_page = "<!DOCTYPE html>"
"<html>"
"<head>"
"<title>Tronbyt WiFi Setup</title>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"<style>"
"body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }"
"h1 { color: #333; }"
".form-container { max-width: 400px; margin: 0 auto; }"
".form-group { margin-bottom: 15px; }"
"label { display: block; margin-bottom: 5px; font-weight: bold; }"
"input[type='text'], input[type='password'] { width: 100%; padding: 8px; box-sizing: border-box; }"
"button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; cursor: pointer; }"
"button:hover { background-color: #45a049; }"
".networks { margin-top: 20px; }"
"</style>"
"</head>"
"<body>"
"<div class='form-container'>"
"<h1>Tronbyt WiFi Setup</h1>"
"<form action='/save' method='post' enctype='application/x-www-form-urlencoded'>"
"<div class='form-group'>"
"<label for='ssid'>WiFi Network Name:</label>"
"<input type='text' id='ssid' name='ssid' maxlength='32' required>"
"</div>"
"<div class='form-group'>"
"<label for='password'>WiFi Password:</label>"
"<input type='text' id='password' name='password' maxlength='64'>"
"</div>"
"<div class='form-group'>"
"<label for='image_url'>Image URL:</label>"
"<input type='text' id='image_url' name='image_url' maxlength='256'>"
"</div>"
"<button type='submit'>Save and Connect</button>"
"</form>"
"</div>"
"</body>"
"</html>";
static const char *s_html_page =
"<!DOCTYPE html>"
"<html>"
"<head>"
"<title>Tronbyt WiFi Setup</title>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"<style>"
"body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }"
"h1 { color: #333; }"
".form-container { max-width: 400px; margin: 0 auto; }"
".form-group { margin-bottom: 15px; }"
"label { display: block; margin-bottom: 5px; font-weight: bold; }"
"input[type='text'], input[type='password'] { width: 100%; padding: 8px; "
"box-sizing: border-box; }"
"button { background-color: #4CAF50; color: white; padding: 10px 15px; "
"border: none; cursor: pointer; }"
"button:hover { background-color: #45a049; }"
".networks { margin-top: 20px; }"
"</style>"
"</head>"
"<body>"
"<div class='form-container'>"
"<h1>Tronbyt WiFi Setup</h1>"
"<form action='/save' method='post' "
"enctype='application/x-www-form-urlencoded'>"
"<div class='form-group'>"
"<label for='ssid'>WiFi Network Name:</label>"
"<input type='text' id='ssid' name='ssid' maxlength='32' required>"
"</div>"
"<div class='form-group'>"
"<label for='password'>WiFi Password:</label>"
"<input type='text' id='password' name='password' maxlength='64'>"
"</div>"
"<div class='form-group'>"
"<label for='image_url'>Image URL:</label>"
"<input type='text' id='image_url' name='image_url' maxlength='256'>"
"( If modifying Image URL reboot Tronbyt after saving. )"
"</div>"
"<button type='submit'>Save and Connect</button>"
"</form>"
"</div>"
"</body>"
"</html>";

// Success page HTML
static const char *s_success_html = "<!DOCTYPE html>"
Expand All @@ -117,6 +122,7 @@ static const char *s_success_html = "<!DOCTYPE html>"
"<h1>Configuration Saved!</h1>"
"<p>WiFi credentials and image URL have been saved.</p>"
"<p>The device will now attempt to connect to the WiFi network.</p>"
"<p>If you modified a previously saved Image URL please manually reboot your tron for the changes to take effect."
"<p>You can close this page.</p>"
"</body>"
"</html>";
Expand Down Expand Up @@ -295,6 +301,13 @@ static bool has_saved_config = false;
return 0;
}

// Shutdown the AP by switching wifi mode to STA
void wifi_shutdown_ap(TimerHandle_t xTimer) {
ESP_LOGI(TAG, "Shutting down config portal");
stop_webserver();
esp_wifi_set_mode(WIFI_MODE_STA);
}

// Shutdown WiFi
void wifi_shutdown() {
// Stop the web server if it's running
Expand Down Expand Up @@ -737,8 +750,8 @@ static void url_decode(char *str) {
*dst = (char)strtol(hex, NULL, 16);
src += 3;
} else if (*src == '+') {
*dst = ' ';
src++;
*dst = ' ';
src++;
} else {
*dst = *src;
src++;
Expand Down
6 changes: 6 additions & 0 deletions src/wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
*/
int wifi_initialize(const char *ssid, const char *password);


/**
* @brief Shutdown WiFi Config Portal
*/
void wifi_shutdown_ap(TimerHandle_t xTimer);

/**
* @brief Shutdown WiFi
*/
Expand Down