From 9d98a3972486e048c24fbe7fd5c658848b940947 Mon Sep 17 00:00:00 2001 From: Markus Ransberger Date: Tue, 5 Sep 2023 01:23:18 +0200 Subject: [PATCH] Add possibility to reset wifi credentials. --- include/wordclock_constants.h | 6 +- include/wordclock_esp8266.h | 1 + res/webserver/index.html | 1107 +++++++++++++++++---------------- src/wordclock_esp8266.cpp | 52 +- 4 files changed, 601 insertions(+), 565 deletions(-) diff --git a/include/wordclock_constants.h b/include/wordclock_constants.h index 644b771..8297cc8 100644 --- a/include/wordclock_constants.h +++ b/include/wordclock_constants.h @@ -49,8 +49,9 @@ typedef enum #define PERIOD_TIME_VISU_UPDATE_US (1 * 1000 * 1000) // 1s #define TIMEOUT_LEDDIRECT_US (5 * 1000 * 1000) // 5s -#define SHORT_PRESS_US (100 * 1000) // 100ms -#define LONG_PRESS_US (2 * 1000 * 1000) // 2s +#define SHORT_PRESS_US (100 * 1000) // 100ms +#define LONG_PRESS_US (2 * 1000 * 1000) // 2s +#define VERY_LONG_PRESS_US (10 * 1000 * 1000) // 10s // Current limit #define CURRENT_LIMIT_LED 2500 // limit the total current consumed by LEDs (mA) @@ -65,5 +66,4 @@ typedef enum #define MATRIX_WIDTH 11 #define MATRIX_HEIGHT 11 - #endif /* WORDCLOCK_CONSTANTS_H */ diff --git a/include/wordclock_esp8266.h b/include/wordclock_esp8266.h index 43ba150..2a4eed0 100644 --- a/include/wordclock_esp8266.h +++ b/include/wordclock_esp8266.h @@ -43,6 +43,7 @@ void handle_led_direct(void); void load_main_color(void); void ntp_time_update(uint32 *last_ntp_update_us); void on_state_entry(uint8_t state); +void reset_wifi_credentials(void); void send_heartbeat(void); void set_main_color(uint8_t red, uint8_t green, uint8_t blue); void set_night_mode(bool on); diff --git a/res/webserver/index.html b/res/webserver/index.html index 4e0f31f..3c0018f 100644 --- a/res/webserver/index.html +++ b/res/webserver/index.html @@ -2,626 +2,635 @@ - - - - WORDCLOCK 2.0 - + .save-button { + height: 30px; + margin: 5px; + background-color: rgba(147, 147, 158, 0.4); + } + + WORDCLOCK 2.0 + -
+
-

WORDCLOCK 2.0

+

WORDCLOCK 2.0

-
-
- - -
-
- - -
-
- - -
-
SAVE
-
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
SAVE
+
-
-
-
- MODE -
-
-
-
-
-
-
-
-
-
-
- -
- -
-
-
- -
- -
+
+
+
+ MODE +
+
+
+
+
+
+
+
+
+
+
+ +
+ +
+
+
+ +
+ +
-
+
- + - - - - + console.log(cmdstr); + sendCommand(cmdstr); + toggleSettings(); + } + + function toggleSettings() { + var container = document.getElementById("settings-container"); + if (container.classList.contains("show")) { + container.classList.remove("show"); + } + else { + container.classList.add("show"); + } + } + + \ No newline at end of file diff --git a/src/wordclock_esp8266.cpp b/src/wordclock_esp8266.cpp index c450711..067d850 100644 --- a/src/wordclock_esp8266.cpp +++ b/src/wordclock_esp8266.cpp @@ -94,6 +94,7 @@ static Tetris tetris = Tetris(&led_matrix, &logger); static bool night_mode = false; // stores state of nightmode static bool state_auto_change = false; // stores state of automatic state change +static bool reset_wifi_creds = false; // used to reset stored wifi credentials static float filter_factor = DEFAULT_SMOOTHING_FACTOR; // stores smoothing factor for led transition static uint32_t main_color_clock = colors_24bit[2]; // color of the clock and digital clock static uint32_t main_color_snake = colors_24bit[1]; // color of the random snake animation @@ -146,11 +147,11 @@ void setup() led_matrix.draw_on_matrix_instant(); /* Use WiFiMaanger for handling initial Wifi setup */ - WiFiManager wifiManager; // Local intialization. Once its business is done, there is no need to keep it around + WiFiManager wifi_manager; // Local intialization. Once its business is done, there is no need to keep it around /* fetches ssid and pass from eeprom and tries to connect. if it does not connect it starts an access point with * the specified name and goes into a blocking loop awaiting configuration. */ - wifiManager.autoConnect(AP_SSID); + wifi_manager.autoConnect(AP_SSID); // If you get here you have connected to the WiFi Serial.printf("Connected, IP address: "); @@ -302,6 +303,7 @@ void loop() { send_heartbeat(); // send heartbeat update last_heartbeat_us = system_get_time(); + delay(10); } if (!night_mode && ((current_time_us - last_animation_step_us) > PERIODS[state_auto_change][current_state]) && ((current_time_us - last_led_direct_us) > TIMEOUT_LEDDIRECT_US)) @@ -455,8 +457,8 @@ void update_matrix() */ void send_heartbeat() { - logger.log_string("Heartbeat, current state: " + state_names[current_state] + ", counter: " + - heartbeat_counter + ", on-time: " + (float)(heartbeat_counter) / 3600.0 + "h\n"); + logger.log_string("Current state: " + state_names[current_state] + ", counter: " + + heartbeat_counter + ", on-time: " + (float)(heartbeat_counter) / 3600.0 + "h\n"); heartbeat_counter++; } @@ -700,7 +702,13 @@ void handle_button() if (button_pressed == false && last_button_state == true) { // button press ended - if ((system_get_time() - button_press_start) > LONG_PRESS_US) + if ((system_get_time() - button_press_start) > VERY_LONG_PRESS_US) + { + // longpress -> reset wifi creds and restart ESP + logger.log_string("Button press ended - very long press -> Reset Wifi creds."); + reset_wifi_credentials(); + } + else if ((system_get_time() - button_press_start) > LONG_PRESS_US) { // longpress -> nightmode logger.log_string("Button press ended - long press"); @@ -828,16 +836,18 @@ void handle_command() } else if (webserver.argName(0).equals("setting")) { - String time_str = webserver.arg(0) + "-"; - logger.log_string("Nightmode setting change via Webserver to: " + time_str); - night_mode_times.start_hour = split(time_str, '-', 0).toInt(); - night_mode_times.start_min = split(time_str, '-', 1).toInt(); - night_mode_times.end_hour = split(time_str, '-', 2).toInt(); - night_mode_times.end_min = split(time_str, '-', 3).toInt(); - brightness = split(time_str, '-', 4).toInt(); + String cmd_str = webserver.arg(0) + "-"; + logger.log_string("Nightmode setting change via Webserver to: " + cmd_str); + night_mode_times.start_hour = split(cmd_str, '-', 0).toInt(); + night_mode_times.start_min = split(cmd_str, '-', 1).toInt(); + night_mode_times.end_hour = split(cmd_str, '-', 2).toInt(); + night_mode_times.end_min = split(cmd_str, '-', 3).toInt(); + brightness = split(cmd_str, '-', 4).toInt(); + reset_wifi_creds = split(cmd_str, '-', 5).toInt() > 0 ? true : false; + if (brightness < 10) { - brightness = 10; + brightness = 10; // minimum brightness } if (night_mode_times.start_hour < 0 || night_mode_times.start_hour > 23) { @@ -855,6 +865,10 @@ void handle_command() { night_mode_times.end_min = NIGHTMODE_END_MIN; } + if (reset_wifi_creds == true) + { + reset_wifi_credentials(); // this function will not return + } EEPROM_write_to_address(ADR_NM_START_H, night_mode_times.start_hour); EEPROM_write_to_address(ADR_NM_START_M, night_mode_times.start_min); @@ -1040,3 +1054,15 @@ String leading_zero2digit(int value) msg += String(value); return msg; } + +/** + * @brief Reset Wifi credentials and restart ESP. This function will not return. + * + */ +void reset_wifi_credentials() +{ + WiFiManager wifi_manager; + wifi_manager.resetSettings(); + delay(200); + ESP.restart(); +}