From 2de5d25cea8a82b7652b42fc5d441ac5f5167e19 Mon Sep 17 00:00:00 2001 From: rasmus Date: Thu, 26 Mar 2026 16:55:27 +0100 Subject: [PATCH] added mqtt connection logik --- CMakeLists.txt | 1 + fs/index.html | 32 ++++------ fs/{config.html => mqtt_config.html} | 3 + fs/wlan_config.html | 20 ++++++ src/main.cpp | 96 +++++++++++++++++++++------- src/webserver.c | 4 ++ src/webserver.h | 1 + 7 files changed, 116 insertions(+), 41 deletions(-) rename fs/{config.html => mqtt_config.html} (90%) create mode 100644 fs/wlan_config.html diff --git a/CMakeLists.txt b/CMakeLists.txt index ba0865a..3ba7e46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ target_link_libraries(sensor-pico dhcp_server pico_lwip pico_lwip_http + pico_lwip_mqtt ) # Erzeugt .uf2 Datei zum Flashen diff --git a/fs/index.html b/fs/index.html index bbf8d63..8a85291 100644 --- a/fs/index.html +++ b/fs/index.html @@ -1,20 +1,16 @@ - - - - - Konfiguration - - -

WLAN Einstellungen

-
- - -
- - -
- -
- + + + + Config + + +

Sensor Pico

+
+ Live Daten +
+ WLAN Config +
+ MQTT und Messungs Config + diff --git a/fs/config.html b/fs/mqtt_config.html similarity index 90% rename from fs/config.html rename to fs/mqtt_config.html index fc279e7..ebc6a08 100644 --- a/fs/config.html +++ b/fs/mqtt_config.html @@ -11,6 +11,9 @@
+ + +

diff --git a/fs/wlan_config.html b/fs/wlan_config.html new file mode 100644 index 0000000..bbf8d63 --- /dev/null +++ b/fs/wlan_config.html @@ -0,0 +1,20 @@ + + + + + + Konfiguration + + +

WLAN Einstellungen

+
+ + +
+ + +
+ +
+ + diff --git a/src/main.cpp b/src/main.cpp index 1691641..f95399a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include "dhcp_server.h" #include "hardware/i2c.h" #include "lwip/apps/httpd.h" +#include "lwip/apps/mqtt.h" #include "pico/cyw43_arch.h" #include "pico/stdlib.h" #include "webserver.h" @@ -21,39 +22,88 @@ void ap_init() { dhcp_server_init(&dhcp_server, &gw, &mask); } -int main() { +void reset_mqtt_config() { + memset(saved_mqtt_address, 0, sizeof(saved_mqtt_address)); + memset(saved_mqtt_user, 0, sizeof(saved_mqtt_user)); + memset(saved_mqtt_password, 0, sizeof(saved_mqtt_password)); + saved_measure_frequency = 0; + saved_post_frequency = 0; +} + +int connect_to_wifi() { int ret{}; + memset(saved_ssid, 0, sizeof(saved_ssid)); + memset(saved_password, 0, sizeof(saved_password)); + ap_init(); + while (saved_ssid[0] == '\0') { + cyw43_arch_poll(); + sleep_ms(100); + } + cyw43_arch_disable_ap_mode(); + sleep_ms(500); + cyw43_arch_enable_sta_mode(); + + ret = cyw43_arch_wifi_connect_timeout_ms(saved_ssid, saved_password, + CYW43_AUTH_WPA2_MIXED_PSK, 10000); + if (ret == 0) { + return 0; + } + return -1; +} + +void mqtt_cb(mqtt_client_t *client, void *arg, + mqtt_connection_status_t status) { + int *mqtt_status{static_cast(arg)}; + *mqtt_status = (status == MQTT_CONNECT_ACCEPTED) ? 0 : 1; +} + +int connect_to_mqtt() { + static int mqtt_status{-1}; + ip_addr_t broker_ip; + static mqtt_client_t *client{}; + static mqtt_connect_client_info_t info{}; + + if (!ipaddr_aton(saved_mqtt_address, &broker_ip)) { + return -1; + } + if (!client) { + client = mqtt_client_new(); + } + info.client_id = "sensor-pico"; + info.client_user = saved_mqtt_user; + info.client_pass = saved_mqtt_password; + + mqtt_client_connect(client, &broker_ip, saved_mqtt_port, mqtt_cb, + &mqtt_status, &info); + while (mqtt_status == -1) { + cyw43_arch_poll(); + sleep_ms(100); + } + return mqtt_status; +} + +int main() { + int mqtt_ret{-1}; + int wifi_status{1}; stdio_init_all(); sleep_ms(3000); cyw43_arch_init(); httpd_init(); + while (wifi_status != 0) { + wifi_status = (connect_to_wifi() == 0) ? 0 : 1; + } + while (true) { - memset(saved_ssid, 0, sizeof(saved_ssid)); - memset(saved_password, 0, sizeof(saved_password)); - ap_init(); - printf("Return Code: %d\n", ret); - - while (saved_ssid[0] == '\0') { + while (saved_mqtt_address[0] == '\0') { cyw43_arch_poll(); - sleep_ms(100); + sleep_ms(200); } - cyw43_arch_disable_ap_mode(); - cyw43_arch_enable_sta_mode(); - - ret = cyw43_arch_wifi_connect_timeout_ms(saved_ssid, saved_password, - CYW43_AUTH_WPA2_MIXED_PSK, 10000); - if (ret == 0) { + mqtt_ret = connect_to_mqtt(); + if (mqtt_ret) { break; - } - - while (true) { - cyw43_arch_poll(); - cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); - sleep_ms(100); - cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); - sleep_ms(100); + } else { + reset_mqtt_config(); } } } - diff --git a/src/webserver.c b/src/webserver.c index cf937b8..81d7c1f 100644 --- a/src/webserver.c +++ b/src/webserver.c @@ -10,6 +10,7 @@ char saved_mqtt_user[33]; char saved_mqtt_password[65]; int saved_measure_frequency; int saved_post_frequency; +int saved_mqtt_port; static int post_state; @@ -70,6 +71,9 @@ void httpd_post_finished(void *connection, char *response_uri, parse_post("mqtt-user=", '&', saved_mqtt_user, 32); parse_post("mqtt-password=", '&', saved_mqtt_password, 64); + parse_post("mqtt-port=", '&', temp, 15); + saved_mqtt_port = atoi(temp); + parse_post("measure-frequency=", '&', temp, 15); saved_measure_frequency = atoi(temp); diff --git a/src/webserver.h b/src/webserver.h index b329e85..cd09fde 100644 --- a/src/webserver.h +++ b/src/webserver.h @@ -12,6 +12,7 @@ extern char saved_mqtt_user[33]; extern char saved_mqtt_password[65]; extern int saved_measure_frequency; extern int saved_post_frequency; +extern int saved_mqtt_port; #ifdef __cplusplus }