Add possibility to reset wifi credentials.

This commit is contained in:
2023-09-05 01:23:18 +02:00
parent f402d31be5
commit 9d98a39724
4 changed files with 601 additions and 565 deletions

View File

@@ -51,6 +51,7 @@ typedef enum
#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 */

View File

@@ -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);

View File

@@ -19,8 +19,8 @@
color: white;
background: linear-gradient(90deg, #ffa9a9, #64a1e0, #ffa9a9, #64a1e0);
background-size: 400% 400%;
-webkit-animation: gradientBG 180s ease infinite forwards;
animation: gradientBG 180s ease infinite forwards;
-webkit-animation: gradientBG 360s ease infinite forwards;
animation: gradientBG 360s ease infinite forwards;
font-weight: lighter;
}
@@ -251,7 +251,7 @@
}
.show {
height: 150px;
height: 200px;
transition: height 1s;
}
@@ -284,13 +284,17 @@
<input type="range" id="brightness" name="volume" min="10" max="255">
</div>
<div class="number-container">
<label for="nm_start" style="align-self: flex-start">Nightmode start time: </label>
<label for="nm_start" style="align-self: flex-start">Nightmode Start Time: </label>
<input type="time" id="nm_start" name="nm_start" min="00:00" max="23:59">
</div>
<div class="number-container">
<label for="nm_end" style="align-self: flex-start">Nightmode end time: </label>
<label for="nm_end" style="align-self: flex-start">Nightmode End Time: </label>
<input type="time" id="nm_end" name="nm_end" min="00:00" max="23:59">
</div>
<div class="checkbox-container">
<label for="reset_wifi" style="align-self: flex-start">Reset Wifi Credentials</label>
<input name="reset_wifi" id="reset_wifi" type="checkbox" class="toggle">
</div>
<div class="buttonClass save-button" onclick="saveSettings()">SAVE</div>
</div>
@@ -600,12 +604,17 @@
var nmStart = document.getElementById("nm_start");
var nmEnd = document.getElementById("nm_end");
var brightnessElmt = document.getElementById("brightness");
var resetWifi = document.getElementById("reset_wifi");
var cmdstr = "./cmd?setting=";
cmdstr += nmStart.value.replace(":", "-");
cmdstr += "-";
cmdstr += nmEnd.value.replace(":", "-");
cmdstr += "-";
cmdstr += brightnessElmt.value;
cmdstr += "-";
cmdstr += Number(resetWifi.checked);
console.log(cmdstr);
sendCommand(cmdstr);
toggleSettings();

View File

@@ -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,7 +457,7 @@ void update_matrix()
*/
void send_heartbeat()
{
logger.log_string("Heartbeat, current state: " + state_names[current_state] + ", counter: " +
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();
}