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