Added webserver logic for the mqtt configuration

This commit is contained in:
2026-03-25 23:10:29 +01:00
parent 2a587ad106
commit 12d6d0b1e7
5 changed files with 82 additions and 22 deletions

View File

@@ -25,6 +25,7 @@ add_custom_command(
-P ${CMAKE_CURRENT_SOURCE_DIR}/prepend_include.cmake -P ${CMAKE_CURRENT_SOURCE_DIR}/prepend_include.cmake
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${CMAKE_SOURCE_DIR}/fs/index.html DEPENDS ${CMAKE_SOURCE_DIR}/fs/index.html
DEPENDS ${CMAKE_SOURCE_DIR}/fs/config.html
) )

29
fs/config.html Normal file
View File

@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Konfiguration</title>
</head>
<body>
<h1>MQTT Konfiguration</h1>
<form action="/mqtt-config" method="post">
<label>MQTT Adresse:</label>
<input type="text" name="mqtt-address">
<br>
<label>MQTT User:</label>
<input type="text" name="mqtt-user">
<br>
<label>MQTT Passwort:</label>
<input type="password" name="mqtt-password">
<br>
<label>Messfrequenz (sekunden)</label>
<input type="number" name="measure-frequency">
<br>
<label>Pushfrequenz (sekunden)</label>
<input type="number" name="push-frequency">
<br>
<input type="submit" value="Speichern">
</form>
</body>
</html>

0
fs/live_stats.html Normal file
View File

View File

@@ -1,9 +1,31 @@
#include "lwip/apps/httpd.h" #include "lwip/apps/httpd.h"
#include "string.h" #include "string.h"
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
char saved_ssid[33]; char saved_ssid[33];
char saved_password[65]; 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, err_t httpd_post_begin(void *connection, const char *uri,
const char *http_request, u16_t http_request_len, 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) { if (strcmp(uri, "/config") == 0) {
strncpy(response_uri, "/index.html", response_uri_len); strncpy(response_uri, "/index.html", response_uri_len);
*post_auto_wnd = 1; *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_OK;
} }
return ERR_VAL; 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) { err_t httpd_post_receive_data(void *connection, struct pbuf *p) {
post_buffer_len = post_buffer_len =
pbuf_copy_partial(p, post_buffer, sizeof(post_buffer) - 1, 0); 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, void httpd_post_finished(void *connection, char *response_uri,
u16_t response_uri_len) { u16_t response_uri_len) {
if (post_state == 0) {
strncpy(response_uri, "/index.html", response_uri_len); strncpy(response_uri, "/index.html", response_uri_len);
char *pos = strstr(post_buffer, "ssid="); parse_post("ssid=", '&', saved_ssid, 32);
pos = pos + 5; parse_post("password=", '\0', saved_password, 64);
char *end = strchr(pos, '&');
int len = end - pos;
if (len > 32) {
len = 32;
}
strncpy(saved_ssid, pos, len);
char *pos2 = strstr(post_buffer, "password=") + 9; } else if (post_state == 1) {
char *end2 = strchr(pos2, '\0'); char temp[16];
int len2 = end2 - pos2; strncpy(response_uri, "/config.html", response_uri_len);
if (len2 > 64) {
len2 = 64;
}
strncpy(saved_password, pos2, len2);
saved_ssid[len] = '\0'; parse_post("mqtt-address=", '&', saved_mqtt_address, 64);
saved_password[len2] = '\0'; 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);
}
} }

View File

@@ -7,6 +7,11 @@ extern "C" {
extern char saved_ssid[33]; extern char saved_ssid[33];
extern char saved_password[65]; 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 #ifdef __cplusplus
} }