From f764f7d6125594de13a659a4ba8157f36bd1a47c Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 27 Dec 2022 17:55:37 +0100 Subject: [PATCH] Wifi setup improved. --- src/abfall.cpp | 63 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/src/abfall.cpp b/src/abfall.cpp index 63c466c..bc45b53 100644 --- a/src/abfall.cpp +++ b/src/abfall.cpp @@ -1,42 +1,71 @@ #include + +#include + #include "pico/stdlib.h" #include "pico/cyw43_arch.h" -#define WIFI_SSID "Apis cerana" -#define WIFI_PASSWORD "2JkJEh2vptVT" +using std::string; -bool wifi_connect() +int wifi_setup(uint32_t country, const string &ssid, const string &pw) { + if (cyw43_arch_init_with_country(country)) + { + return 1; + } + cyw43_arch_enable_sta_mode(); - printf("Connecting to WiFi...\n"); - if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) + if (cyw43_arch_wifi_connect_async(ssid.c_str(), pw.c_str(), CYW43_AUTH_WPA2_MIXED_PSK)) { - printf("Failed to connect.\n"); - return false; + return 2; + } + + int flashrate = 1000; + int status = CYW43_LINK_UP + 1; + + while (status >= 0 && status != CYW43_LINK_UP) + { + int status_new = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA); + + if (status_new != status) + { + status = status_new; + flashrate = flashrate / (status + 1); + printf("Connect status: %d %d\n", status, flashrate); + } + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); + sleep_ms(flashrate); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); + sleep_ms(flashrate); + } + + if (status < 0) + { + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); } else { - printf("Connected.\n"); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); } - return true; + return 0; } int main() { + const string ssid{"Apis cerana"}; + const string pw{"2JkJEh2vptVT"}; + const uint32_t country{CYW43_COUNTRY_GERMANY}; + stdio_init_all(); - if (cyw43_arch_init()) + wifi_setup(country, ssid, pw); + + while (true) { - printf("Failed to initialize\n"); - return 1; + sleep_ms(1); } - if (wifi_connect() == false) - { - return 1; - } - cyw43_arch_deinit(); }