Final Update

This commit is contained in:
2026-03-27 22:32:25 +01:00
parent e8f90661d3
commit 173bf74967

View File

@@ -15,9 +15,6 @@
static dhcp_server_t dhcp_server{}; static dhcp_server_t dhcp_server{};
/* ── WiFi ───────────────────────────────────────────────────────────────────
*/
static void ap_init() { static void ap_init() {
cyw43_arch_enable_ap_mode("SensorAP", "passwort123", CYW43_AUTH_WPA2_AES_PSK); cyw43_arch_enable_ap_mode("SensorAP", "passwort123", CYW43_AUTH_WPA2_AES_PSK);
ip_addr_t gw{}, mask{}; ip_addr_t gw{}, mask{};
@@ -27,8 +24,6 @@ static void ap_init() {
dhcp_server_init(&dhcp_server, &gw, &mask); dhcp_server_init(&dhcp_server, &gw, &mask);
} }
/* Open AP, wait for user to POST credentials, then connect STA.
Returns 0 on success. */
static int connect_via_ap() { static int connect_via_ap() {
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));
@@ -46,7 +41,6 @@ static int connect_via_ap() {
CYW43_AUTH_WPA2_MIXED_PSK, 30000); CYW43_AUTH_WPA2_MIXED_PSK, 30000);
} }
/* Connect STA with current saved credentials (no AP fallback). */
static int connect_sta() { static int connect_sta() {
cyw43_arch_disable_sta_mode(); cyw43_arch_disable_sta_mode();
sleep_ms(500); sleep_ms(500);
@@ -55,9 +49,6 @@ static int connect_sta() {
CYW43_AUTH_WPA2_MIXED_PSK, 30000); CYW43_AUTH_WPA2_MIXED_PSK, 30000);
} }
/* ── MQTT ───────────────────────────────────────────────────────────────────
*/
static void mqtt_cb(mqtt_client_t *client, void *arg, static void mqtt_cb(mqtt_client_t *client, void *arg,
mqtt_connection_status_t status) { mqtt_connection_status_t status) {
(void)client; (void)client;
@@ -103,9 +94,6 @@ static void publish_mqtt(mqtt_client_t *client, const char *payload) {
publish_cb, nullptr); publish_cb, nullptr);
} }
/* ── BME280 ─────────────────────────────────────────────────────────────────
*/
static BME280_READING_INTERVALS_MS convert_interval(int ms) { static BME280_READING_INTERVALS_MS convert_interval(int ms) {
if (ms < 1) if (ms < 1)
return INTERVAL_0_5MS; return INTERVAL_0_5MS;
@@ -165,9 +153,6 @@ static void build_sensor_payload(char *buffer, size_t size,
time_str, sensor_name, temperature, humidity, pressure); time_str, sensor_name, temperature, humidity, pressure);
} }
/* ── SNTP ───────────────────────────────────────────────────────────────────
*/
static bool is_time_synced() { static bool is_time_synced() {
time_t now = time(nullptr); time_t now = time(nullptr);
return localtime(&now)->tm_year > 70; return localtime(&now)->tm_year > 70;
@@ -185,9 +170,6 @@ static void start_sntp() {
cyw43_arch_lwip_end(); cyw43_arch_lwip_end();
} }
/* ── Main ───────────────────────────────────────────────────────────────────
*/
int main() { int main() {
stdio_init_all(); stdio_init_all();
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0", 1); setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0", 1);
@@ -196,10 +178,8 @@ int main() {
cyw43_arch_init(); cyw43_arch_init();
httpd_init(); httpd_init();
/* ── 1. Load config from flash ── */
bool has_saved_config = config_load(); bool has_saved_config = config_load();
/* ── 2. Connect to WiFi ── */
if (has_saved_config && saved_ssid[0] != '\0') { if (has_saved_config && saved_ssid[0] != '\0') {
printf("Trying saved WiFi: %s\n", saved_ssid); printf("Trying saved WiFi: %s\n", saved_ssid);
if (connect_sta() != 0) { if (connect_sta() != 0) {
@@ -219,13 +199,11 @@ int main() {
start_sntp(); start_sntp();
uint32_t last_sntp_retry_ms = to_ms_since_boot(get_absolute_time()); uint32_t last_sntp_retry_ms = to_ms_since_boot(get_absolute_time());
/* ── 3. Wait for MQTT config if not saved ── */
while (saved_mqtt_address[0] == '\0') { while (saved_mqtt_address[0] == '\0') {
cyw43_arch_poll(); cyw43_arch_poll();
sleep_ms(200); sleep_ms(200);
} }
/* ── 4. Connect to MQTT ── */
mqtt_client_t *client{}; mqtt_client_t *client{};
while (true) { while (true) {
client = connect_to_mqtt(); client = connect_to_mqtt();
@@ -240,7 +218,6 @@ int main() {
} }
printf("MQTT connected!\n"); printf("MQTT connected!\n");
/* ── 5. Init BME280 ── */
bme280_handle_t handle{}; bme280_handle_t handle{};
bool bme_ok = (bme280_init(&handle, 0x76, bool bme_ok = (bme280_init(&handle, 0x76,
convert_interval(saved_measure_frequency)) == 0); convert_interval(saved_measure_frequency)) == 0);
@@ -248,10 +225,8 @@ int main() {
char payload[256]; char payload[256];
uint32_t last_publish = to_ms_since_boot(get_absolute_time()); uint32_t last_publish = to_ms_since_boot(get_absolute_time());
/* ── 6. Main loop ── */
while (true) { while (true) {
/* ── Handle WiFi config change ── */
if (wlan_config_updated) { if (wlan_config_updated) {
wlan_config_updated = false; wlan_config_updated = false;
config_save(); config_save();
@@ -265,11 +240,9 @@ int main() {
} else { } else {
printf("WiFi reconnect failed!\n"); printf("WiFi reconnect failed!\n");
} }
/* Force MQTT reconnect after WiFi change */
mqtt_config_updated = true; mqtt_config_updated = true;
} }
/* ── Handle MQTT / interval config change ── */
if (mqtt_config_updated) { if (mqtt_config_updated) {
mqtt_config_updated = false; mqtt_config_updated = false;
config_save(); config_save();
@@ -287,7 +260,6 @@ int main() {
} }
} }
/* ── Periodic publish ── */
uint32_t now_ms = to_ms_since_boot(get_absolute_time()); uint32_t now_ms = to_ms_since_boot(get_absolute_time());
if (client && bme_ok && if (client && bme_ok &&
(now_ms - last_publish) >= uint32_t(saved_post_frequency) * 1000) { (now_ms - last_publish) >= uint32_t(saved_post_frequency) * 1000) {