From 12d6d0b1e7f2276dfcd46e0b0bd1e98c1ba5a10d Mon Sep 17 00:00:00 2001 From: rasmus Date: Wed, 25 Mar 2026 23:10:29 +0100 Subject: [PATCH] Added webserver logic for the mqtt configuration --- CMakeLists.txt | 1 + fs/config.html | 29 +++++++++++++++++++ fs/live_stats.html | 0 src/webserver.c | 69 +++++++++++++++++++++++++++++++--------------- src/webserver.h | 5 ++++ 5 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 fs/config.html create mode 100644 fs/live_stats.html diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bf97cb..ba0865a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ add_custom_command( -P ${CMAKE_CURRENT_SOURCE_DIR}/prepend_include.cmake WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${CMAKE_SOURCE_DIR}/fs/index.html + DEPENDS ${CMAKE_SOURCE_DIR}/fs/config.html ) diff --git a/fs/config.html b/fs/config.html new file mode 100644 index 0000000..fc279e7 --- /dev/null +++ b/fs/config.html @@ -0,0 +1,29 @@ + + + + + + Konfiguration + + +

MQTT Konfiguration

+
+ + +
+ + +
+ + +
+ + +
+ + +
+ +
+ + diff --git a/fs/live_stats.html b/fs/live_stats.html new file mode 100644 index 0000000..e69de29 diff --git a/src/webserver.c b/src/webserver.c index b635197..cf937b8 100644 --- a/src/webserver.c +++ b/src/webserver.c @@ -1,9 +1,31 @@ #include "lwip/apps/httpd.h" #include "string.h" #include +#include char saved_ssid[33]; char saved_password[65]; +char saved_mqtt_address[65]; +char saved_mqtt_user[33]; +char saved_mqtt_password[65]; +int saved_measure_frequency; +int saved_post_frequency; + +static int post_state; + +static char post_buffer[400]; +static uint16_t post_buffer_len = 0; + +void parse_post(const char *key, char delimiter, char *dest, int max_len) { + char *pos = strstr(post_buffer, key) + strlen(key); + char *end = strchr(pos, delimiter); + int len = end - pos; + if (len > max_len) { + len = max_len; + } + strncpy(dest, pos, len); + dest[len] = '\0'; +} err_t httpd_post_begin(void *connection, const char *uri, const char *http_request, u16_t http_request_len, @@ -12,14 +34,18 @@ err_t httpd_post_begin(void *connection, const char *uri, if (strcmp(uri, "/config") == 0) { strncpy(response_uri, "/index.html", response_uri_len); *post_auto_wnd = 1; + post_state = 0; + return ERR_OK; + } + if (strcmp(uri, "/mqtt-config") == 0) { + post_state = 1; + strncpy(response_uri, "/config.html", response_uri_len); + *post_auto_wnd = 1; return ERR_OK; } return ERR_VAL; } -static char post_buffer[128]; -static uint16_t post_buffer_len = 0; - err_t httpd_post_receive_data(void *connection, struct pbuf *p) { post_buffer_len = pbuf_copy_partial(p, post_buffer, sizeof(post_buffer) - 1, 0); @@ -30,25 +56,24 @@ err_t httpd_post_receive_data(void *connection, struct pbuf *p) { void httpd_post_finished(void *connection, char *response_uri, u16_t response_uri_len) { - strncpy(response_uri, "/index.html", response_uri_len); + if (post_state == 0) { + strncpy(response_uri, "/index.html", response_uri_len); - char *pos = strstr(post_buffer, "ssid="); - pos = pos + 5; - char *end = strchr(pos, '&'); - int len = end - pos; - if (len > 32) { - len = 32; + parse_post("ssid=", '&', saved_ssid, 32); + parse_post("password=", '\0', saved_password, 64); + + } else if (post_state == 1) { + char temp[16]; + strncpy(response_uri, "/config.html", response_uri_len); + + parse_post("mqtt-address=", '&', saved_mqtt_address, 64); + parse_post("mqtt-user=", '&', saved_mqtt_user, 32); + parse_post("mqtt-password=", '&', saved_mqtt_password, 64); + + parse_post("measure-frequency=", '&', temp, 15); + saved_measure_frequency = atoi(temp); + + parse_post("push-frequency=", '\0', temp, 15); + saved_post_frequency = atoi(temp); } - strncpy(saved_ssid, pos, len); - - char *pos2 = strstr(post_buffer, "password=") + 9; - char *end2 = strchr(pos2, '\0'); - int len2 = end2 - pos2; - if (len2 > 64) { - len2 = 64; - } - strncpy(saved_password, pos2, len2); - - saved_ssid[len] = '\0'; - saved_password[len2] = '\0'; } diff --git a/src/webserver.h b/src/webserver.h index cc7b752..b329e85 100644 --- a/src/webserver.h +++ b/src/webserver.h @@ -7,6 +7,11 @@ extern "C" { extern char saved_ssid[33]; extern char saved_password[65]; +extern char saved_mqtt_address[65]; +extern char saved_mqtt_user[33]; +extern char saved_mqtt_password[65]; +extern int saved_measure_frequency; +extern int saved_post_frequency; #ifdef __cplusplus }