added mqtt connection logik
This commit is contained in:
@@ -51,6 +51,7 @@ target_link_libraries(sensor-pico
|
|||||||
dhcp_server
|
dhcp_server
|
||||||
pico_lwip
|
pico_lwip
|
||||||
pico_lwip_http
|
pico_lwip_http
|
||||||
|
pico_lwip_mqtt
|
||||||
)
|
)
|
||||||
|
|
||||||
# Erzeugt .uf2 Datei zum Flashen
|
# Erzeugt .uf2 Datei zum Flashen
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="UTF-8">
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
<title>Config</title>
|
||||||
<title>Konfiguration</title>
|
</head>
|
||||||
</head>
|
<body>
|
||||||
<body>
|
<h1>Sensor Pico</h1>
|
||||||
<h1>WLAN Einstellungen</h1>
|
|
||||||
<form action="/config" method="post">
|
|
||||||
<label>SSID:</label>
|
|
||||||
<input type="text" name="ssid">
|
|
||||||
<br>
|
<br>
|
||||||
<label>Passwort:</label>
|
<a href="live_stats.html">Live Daten</a>
|
||||||
<input type="password" name="password">
|
|
||||||
<br>
|
<br>
|
||||||
<input type="submit" value="Speichern">
|
<a href="wlan_config.html">WLAN Config</a>
|
||||||
</form>
|
<br>
|
||||||
</body>
|
<a href="mqtt_config.html">MQTT und Messungs Config</a>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -11,6 +11,9 @@
|
|||||||
<label>MQTT Adresse:</label>
|
<label>MQTT Adresse:</label>
|
||||||
<input type="text" name="mqtt-address">
|
<input type="text" name="mqtt-address">
|
||||||
<br>
|
<br>
|
||||||
|
<label>MQTT Port:</label>
|
||||||
|
<input type="number" name="mqtt-port">
|
||||||
|
<br>
|
||||||
<label>MQTT User:</label>
|
<label>MQTT User:</label>
|
||||||
<input type="text" name="mqtt-user">
|
<input type="text" name="mqtt-user">
|
||||||
<br>
|
<br>
|
||||||
20
fs/wlan_config.html
Normal file
20
fs/wlan_config.html
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<title>Konfiguration</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>WLAN Einstellungen</h1>
|
||||||
|
<form action="/config" method="post">
|
||||||
|
<label>SSID:</label>
|
||||||
|
<input type="text" name="ssid">
|
||||||
|
<br>
|
||||||
|
<label>Passwort:</label>
|
||||||
|
<input type="password" name="password">
|
||||||
|
<br>
|
||||||
|
<input type="submit" value="Speichern">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
80
src/main.cpp
80
src/main.cpp
@@ -2,6 +2,7 @@
|
|||||||
#include "dhcp_server.h"
|
#include "dhcp_server.h"
|
||||||
#include "hardware/i2c.h"
|
#include "hardware/i2c.h"
|
||||||
#include "lwip/apps/httpd.h"
|
#include "lwip/apps/httpd.h"
|
||||||
|
#include "lwip/apps/mqtt.h"
|
||||||
#include "pico/cyw43_arch.h"
|
#include "pico/cyw43_arch.h"
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
#include "webserver.h"
|
#include "webserver.h"
|
||||||
@@ -21,39 +22,88 @@ void ap_init() {
|
|||||||
dhcp_server_init(&dhcp_server, &gw, &mask);
|
dhcp_server_init(&dhcp_server, &gw, &mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
void reset_mqtt_config() {
|
||||||
int ret{};
|
memset(saved_mqtt_address, 0, sizeof(saved_mqtt_address));
|
||||||
stdio_init_all();
|
memset(saved_mqtt_user, 0, sizeof(saved_mqtt_user));
|
||||||
sleep_ms(3000);
|
memset(saved_mqtt_password, 0, sizeof(saved_mqtt_password));
|
||||||
cyw43_arch_init();
|
saved_measure_frequency = 0;
|
||||||
httpd_init();
|
saved_post_frequency = 0;
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
int connect_to_wifi() {
|
||||||
|
int ret{};
|
||||||
memset(saved_ssid, 0, sizeof(saved_ssid));
|
memset(saved_ssid, 0, sizeof(saved_ssid));
|
||||||
memset(saved_password, 0, sizeof(saved_password));
|
memset(saved_password, 0, sizeof(saved_password));
|
||||||
ap_init();
|
ap_init();
|
||||||
printf("Return Code: %d\n", ret);
|
|
||||||
|
|
||||||
while (saved_ssid[0] == '\0') {
|
while (saved_ssid[0] == '\0') {
|
||||||
cyw43_arch_poll();
|
cyw43_arch_poll();
|
||||||
sleep_ms(100);
|
sleep_ms(100);
|
||||||
}
|
}
|
||||||
cyw43_arch_disable_ap_mode();
|
cyw43_arch_disable_ap_mode();
|
||||||
|
sleep_ms(500);
|
||||||
cyw43_arch_enable_sta_mode();
|
cyw43_arch_enable_sta_mode();
|
||||||
|
|
||||||
ret = cyw43_arch_wifi_connect_timeout_ms(saved_ssid, saved_password,
|
ret = cyw43_arch_wifi_connect_timeout_ms(saved_ssid, saved_password,
|
||||||
CYW43_AUTH_WPA2_MIXED_PSK, 10000);
|
CYW43_AUTH_WPA2_MIXED_PSK, 10000);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
break;
|
return 0;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mqtt_cb(mqtt_client_t *client, void *arg,
|
||||||
|
mqtt_connection_status_t status) {
|
||||||
|
int *mqtt_status{static_cast<int *>(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) {
|
while (true) {
|
||||||
|
while (saved_mqtt_address[0] == '\0') {
|
||||||
cyw43_arch_poll();
|
cyw43_arch_poll();
|
||||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
sleep_ms(200);
|
||||||
sleep_ms(100);
|
}
|
||||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
|
mqtt_ret = connect_to_mqtt();
|
||||||
sleep_ms(100);
|
if (mqtt_ret) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
reset_mqtt_config();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ char saved_mqtt_user[33];
|
|||||||
char saved_mqtt_password[65];
|
char saved_mqtt_password[65];
|
||||||
int saved_measure_frequency;
|
int saved_measure_frequency;
|
||||||
int saved_post_frequency;
|
int saved_post_frequency;
|
||||||
|
int saved_mqtt_port;
|
||||||
|
|
||||||
static int post_state;
|
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-user=", '&', saved_mqtt_user, 32);
|
||||||
parse_post("mqtt-password=", '&', saved_mqtt_password, 64);
|
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);
|
parse_post("measure-frequency=", '&', temp, 15);
|
||||||
saved_measure_frequency = atoi(temp);
|
saved_measure_frequency = atoi(temp);
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ extern char saved_mqtt_user[33];
|
|||||||
extern char saved_mqtt_password[65];
|
extern char saved_mqtt_password[65];
|
||||||
extern int saved_measure_frequency;
|
extern int saved_measure_frequency;
|
||||||
extern int saved_post_frequency;
|
extern int saved_post_frequency;
|
||||||
|
extern int saved_mqtt_port;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user