Skip to content

Commit 4cce7df

Browse files
authored
Merge pull request #46 from tronbyt/config_portal_AP_timeout
add wifi_shutdown_ap timer for 2 minute timeout
2 parents 1625b52 + 76fe611 commit 4cce7df

File tree

3 files changed

+81
-41
lines changed

3 files changed

+81
-41
lines changed

src/main.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,28 @@ void app_main(void) {
202202
}
203203
}
204204

205-
ESP_LOGI(TAG, "WiFi Connected, continuing main task thread . . . ");
205+
// Create a timer to auto-shutdown the AP after 5 minutes
206+
TimerHandle_t ap_shutdown_timer = xTimerCreate(
207+
"ap_shutdown_timer",
208+
pdMS_TO_TICKS(2 * 60 * 1000), // 2 minutes in milliseconds
209+
pdFALSE, // One-shot timer
210+
NULL, // No timer ID
211+
wifi_shutdown_ap // Callback function (now with correct signature)
212+
);
213+
214+
if (ap_shutdown_timer != NULL) {
215+
// Timer created successfully, now try to start it
216+
BaseType_t timer_started = xTimerStart(ap_shutdown_timer, 0);
217+
if (timer_started == pdPASS) {
218+
ESP_LOGI(TAG, "AP will automatically shut down in 2 minutes");
219+
} else {
220+
ESP_LOGE(TAG, "Failed to start AP shutdown timer");
221+
// Clean up the timer if we couldn't start it
222+
xTimerDelete(ap_shutdown_timer, 0);
223+
}
224+
} else {
225+
ESP_LOGE(TAG, "Failed to create AP shutdown timer");
226+
}
206227

207228
// Get the image URL from WiFi manager
208229
const char* image_url = wifi_get_image_url();

src/wifi.c

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -62,44 +62,49 @@ static void (*s_connect_callback)(void) = NULL;
6262
static void (*s_disconnect_callback)(void) = NULL;
6363

6464
// HTML for the configuration page
65-
static const char *s_html_page = "<!DOCTYPE html>"
66-
"<html>"
67-
"<head>"
68-
"<title>Tronbyt WiFi Setup</title>"
69-
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
70-
"<style>"
71-
"body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }"
72-
"h1 { color: #333; }"
73-
".form-container { max-width: 400px; margin: 0 auto; }"
74-
".form-group { margin-bottom: 15px; }"
75-
"label { display: block; margin-bottom: 5px; font-weight: bold; }"
76-
"input[type='text'], input[type='password'] { width: 100%; padding: 8px; box-sizing: border-box; }"
77-
"button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; cursor: pointer; }"
78-
"button:hover { background-color: #45a049; }"
79-
".networks { margin-top: 20px; }"
80-
"</style>"
81-
"</head>"
82-
"<body>"
83-
"<div class='form-container'>"
84-
"<h1>Tronbyt WiFi Setup</h1>"
85-
"<form action='/save' method='post' enctype='application/x-www-form-urlencoded'>"
86-
"<div class='form-group'>"
87-
"<label for='ssid'>WiFi Network Name:</label>"
88-
"<input type='text' id='ssid' name='ssid' maxlength='32' required>"
89-
"</div>"
90-
"<div class='form-group'>"
91-
"<label for='password'>WiFi Password:</label>"
92-
"<input type='text' id='password' name='password' maxlength='64'>"
93-
"</div>"
94-
"<div class='form-group'>"
95-
"<label for='image_url'>Image URL:</label>"
96-
"<input type='text' id='image_url' name='image_url' maxlength='256'>"
97-
"</div>"
98-
"<button type='submit'>Save and Connect</button>"
99-
"</form>"
100-
"</div>"
101-
"</body>"
102-
"</html>";
65+
static const char *s_html_page =
66+
"<!DOCTYPE html>"
67+
"<html>"
68+
"<head>"
69+
"<title>Tronbyt WiFi Setup</title>"
70+
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
71+
"<style>"
72+
"body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }"
73+
"h1 { color: #333; }"
74+
".form-container { max-width: 400px; margin: 0 auto; }"
75+
".form-group { margin-bottom: 15px; }"
76+
"label { display: block; margin-bottom: 5px; font-weight: bold; }"
77+
"input[type='text'], input[type='password'] { width: 100%; padding: 8px; "
78+
"box-sizing: border-box; }"
79+
"button { background-color: #4CAF50; color: white; padding: 10px 15px; "
80+
"border: none; cursor: pointer; }"
81+
"button:hover { background-color: #45a049; }"
82+
".networks { margin-top: 20px; }"
83+
"</style>"
84+
"</head>"
85+
"<body>"
86+
"<div class='form-container'>"
87+
"<h1>Tronbyt WiFi Setup</h1>"
88+
"<form action='/save' method='post' "
89+
"enctype='application/x-www-form-urlencoded'>"
90+
"<div class='form-group'>"
91+
"<label for='ssid'>WiFi Network Name:</label>"
92+
"<input type='text' id='ssid' name='ssid' maxlength='32' required>"
93+
"</div>"
94+
"<div class='form-group'>"
95+
"<label for='password'>WiFi Password:</label>"
96+
"<input type='text' id='password' name='password' maxlength='64'>"
97+
"</div>"
98+
"<div class='form-group'>"
99+
"<label for='image_url'>Image URL:</label>"
100+
"<input type='text' id='image_url' name='image_url' maxlength='256'>"
101+
"( If modifying Image URL reboot Tronbyt after saving. )"
102+
"</div>"
103+
"<button type='submit'>Save and Connect</button>"
104+
"</form>"
105+
"</div>"
106+
"</body>"
107+
"</html>";
103108

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

304+
// Shutdown the AP by switching wifi mode to STA
305+
void wifi_shutdown_ap(TimerHandle_t xTimer) {
306+
ESP_LOGI(TAG, "Shutting down config portal");
307+
stop_webserver();
308+
esp_wifi_set_mode(WIFI_MODE_STA);
309+
}
310+
298311
// Shutdown WiFi
299312
void wifi_shutdown() {
300313
// Stop the web server if it's running
@@ -737,8 +750,8 @@ static void url_decode(char *str) {
737750
*dst = (char)strtol(hex, NULL, 16);
738751
src += 3;
739752
} else if (*src == '+') {
740-
*dst = ' ';
741-
src++;
753+
*dst = ' ';
754+
src++;
742755
} else {
743756
*dst = *src;
744757
src++;

src/wifi.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
*/
1515
int wifi_initialize(const char *ssid, const char *password);
1616

17+
18+
/**
19+
* @brief Shutdown WiFi Config Portal
20+
*/
21+
void wifi_shutdown_ap(TimerHandle_t xTimer);
22+
1723
/**
1824
* @brief Shutdown WiFi
1925
*/

0 commit comments

Comments
 (0)