From aeacb8673aa1698bf10f2fbefe2820a09fc186c0 Mon Sep 17 00:00:00 2001 From: Eric Duminil <eric.duminil@gmail.com> Date: Mon, 14 Feb 2022 17:08:30 +0100 Subject: [PATCH] No need to ask for password twice --- ampel-firmware/web_server.cpp | 27 ++++++++++++++++----------- ampel-firmware/wifi_util.cpp | 6 +++++- ampel-firmware/wifi_util.h | 1 + 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/ampel-firmware/web_server.cpp b/ampel-firmware/web_server.cpp index 2c632da..7a9ef8e 100644 --- a/ampel-firmware/web_server.cpp +++ b/ampel-firmware/web_server.cpp @@ -169,9 +169,13 @@ namespace web_server { web_config::http.on("/delete_csv", HTTP_POST, handleDeleteCSV); } - // Allow access if http_user or http_password are empty, or if provided credentials match + /* + * Allow access if Ampel is in access point mode, + * if http_user or http_password are empty, + * or if provided credentials match + */ bool shouldBeAllowed() { - return strcmp(config::http_user, "") == 0 || strcmp(config::ap_password(), "") == 0 + return wifi::isAccessPoint() || strcmp(config::http_user, "") == 0 || strcmp(config::ap_password(), "") == 0 || web_config::http.authenticate(config::http_user, config::ap_password()); } @@ -196,8 +200,8 @@ namespace web_server { snprintf_P(content, sizeof(content), header_template, sensor::co2, config::ampel_name(), wifi::local_ip, csv_writer::filename); - Serial.print(F("INFO - Header size : ")); - Serial.print(strlen(content)); + Serial.print(F("INFO - Header size : ")); + Serial.print(strlen(content)); web_config::http.setContentLength(CONTENT_LENGTH_UNKNOWN); web_config::http.send_P(200, PSTR("text/html"), content); @@ -210,19 +214,20 @@ namespace web_server { lorawan::connected ? "Yes" : "No", config::lorawan_frequency_plan, lorawan::last_transmission, config::lorawan_sending_interval, #endif - config::temperature_offset, config::auto_calibrate_sensor ? "Yes" : "No", config::ampel_name(), config::ampel_name(), - wifi::local_ip, wifi::local_ip, ampel.macAddress, ESP.getFreeHeap(), esp_get_max_free_block_size(), - esp_get_heap_fragmentation(), ampel.max_loop_duration, ampel.board, ampel.sensorId, ampel.version, dd, hh, mm, ss); + config::temperature_offset, config::auto_calibrate_sensor ? "Yes" : "No", config::ampel_name(), + config::ampel_name(), wifi::local_ip, wifi::local_ip, ampel.macAddress, ESP.getFreeHeap(), + esp_get_max_free_block_size(), esp_get_heap_fragmentation(), ampel.max_loop_duration, ampel.board, + ampel.sensorId, ampel.version, dd, hh, mm, ss); - Serial.print(F(" - Body size : ")); - Serial.print(strlen(content)); + Serial.print(F(" - Body size : ")); + Serial.print(strlen(content)); web_config::http.sendContent(content); // Script snprintf_P(content, sizeof(content), script_template, csv_writer::filename, config::ampel_name()); - Serial.print(F(" - Script size : ")); - Serial.println(strlen(content)); + Serial.print(F(" - Script size : ")); + Serial.println(strlen(content)); web_config::http.sendContent(content); } diff --git a/ampel-firmware/wifi_util.cpp b/ampel-firmware/wifi_util.cpp index 2252e6f..da4ca65 100644 --- a/ampel-firmware/wifi_util.cpp +++ b/ampel-firmware/wifi_util.cpp @@ -19,6 +19,10 @@ namespace wifi { return WiFi.status() == WL_CONNECTED; } + bool isAccessPoint() { + return WiFi.getMode() == WIFI_AP; + } + /* * Connection attempt, called in blocking mode by setup(). This way, LED effects can be shown * without needing callbacks, but only during wifi_timeout seconds. @@ -31,7 +35,7 @@ namespace wifi { void tryConnection() { for (int i = 0; i <= config::wifi_timeout + 5; i++) { web_config::update(); - if (WiFi.getMode() == WIFI_AP){ + if (isAccessPoint()) { led_effects::alert(0x1cff68); } else if (connected()) { break; diff --git a/ampel-firmware/wifi_util.h b/ampel-firmware/wifi_util.h index 5b9f719..6732368 100644 --- a/ampel-firmware/wifi_util.h +++ b/ampel-firmware/wifi_util.h @@ -5,6 +5,7 @@ namespace wifi { extern char local_ip[16]; void defineCommands(); bool connected(); + bool isAccessPoint(); void tryConnection(); } -- GitLab