Added webserver logic for the mqtt configuration
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
|
||||
|
||||
29
fs/config.html
Normal file
29
fs/config.html
Normal 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
0
fs/live_stats.html
Normal file
@@ -1,9 +1,31 @@
|
||||
#include "lwip/apps/httpd.h"
|
||||
#include "string.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
strncpy(saved_ssid, pos, len);
|
||||
parse_post("ssid=", '&', saved_ssid, 32);
|
||||
parse_post("password=", '\0', saved_password, 64);
|
||||
|
||||
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);
|
||||
} else if (post_state == 1) {
|
||||
char temp[16];
|
||||
strncpy(response_uri, "/config.html", response_uri_len);
|
||||
|
||||
saved_ssid[len] = '\0';
|
||||
saved_password[len2] = '\0';
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user