From b006fff4a35dab9281c98b064272ea644cd0133c Mon Sep 17 00:00:00 2001 From: s3lph Date: Fri, 20 Sep 2024 01:48:19 +0200 Subject: [PATCH] feat: replace hardcoded wifi credentials with wifimanager library --- esp32/platformio.ini | 3 +- esp32/src/main.cpp | 67 ++++++++++++++------------------------------ 2 files changed, 22 insertions(+), 48 deletions(-) diff --git a/esp32/platformio.ini b/esp32/platformio.ini index 46c5de4..1ed0ce4 100644 --- a/esp32/platformio.ini +++ b/esp32/platformio.ini @@ -16,8 +16,7 @@ lib_deps = adafruit/Adafruit NeoPixel@^1.12.3 bblanchon/ArduinoJson@^7.1.0 sstaub/NTP@^1.6 + tzapu/WiFiManager@^2.0.17 build_flags = -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1 - -D WPA2_SSID=${sysenv.ESP32_WPA2_SSID} - -D WPA2_PSK=${sysenv.ESP32_WPA2_PSK} diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 1be59c8..c873bf6 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -8,14 +9,6 @@ #include "spacemap.h" -#ifndef WPA2_SSID -#define WPA2_SSID "my-wifi-ssid" -#endif -#ifndef WPA2_PSK -#define WPA2_PSK "change-me" -#endif -#define WIFI_RETRY 120 - #define WS2812_PIN 0 #define WS2812_LEN (sizeof(spaces)/sizeof(char*)) #define WS2812_BS 5 @@ -24,6 +17,7 @@ #define SPACEAPI_PATH "/" +WiFiManager wifiManager; WiFiClientSecure client; WiFiUDP wifiUdp; NTP ntp(wifiUdp); @@ -31,46 +25,25 @@ NTP ntp(wifiUdp); Adafruit_NeoPixel pixels(WS2812_LEN, WS2812_PIN, NEO_RGB | NEO_KHZ800); JsonDocument json, filter; -void connectAndWait() { - if (!WiFi.status() != WL_CONNECTED) { - pixels.clear(); - pixels.setPixelColor(0, pixels.Color(WS2812_BS*2, 0 , 0)); - pixels.show(); - Serial.println("\nConnecting to WiFi Network .."); - WiFi.mode(WIFI_STA); - WiFi.setTxPower(WIFI_POWER_15dBm); - WiFi.disconnect(); - WiFi.begin(WPA2_SSID, WPA2_PSK); - } - uint16_t i = 0; - for (uint16_t i = 0; WiFi.status() != WL_CONNECTED && i < WIFI_RETRY; ++i) { - Serial.println(WiFi.status()); - delay(250); - if (WiFi.status() == WL_DISCONNECTED) { - pixels.setPixelColor(0, pixels.Color(WS2812_BS*2*(i%2), 0, 0)); - } else { - pixels.setPixelColor(0, pixels.Color(WS2812_BS*2*(i%2), WS2812_BS*(i%2), 0)); - } - pixels.show(); - } - if (i >= WIFI_RETRY) { - pixels.setPixelColor(0, pixels.Color(WS2812_BS, 0 , WS2812_BS)); - pixels.show(); - ESP.restart(); - while (true) { Serial.print(""); }; - } - Serial.println("\nConnected to the WiFi network"); - Serial.print("Local ESP32 IP: "); - Serial.println(WiFi.localIP()); - pixels.setPixelColor(0, pixels.Color(0, WS2812_BS*2, 0)); - pixels.show(); -} - void setup() { Serial.begin(115200); - pixels.begin(); - connectAndWait(); + pixels.begin(); + pixels.clear(); + + WiFi.setHostname("spaceapimap"); + WiFi.setTxPower(WIFI_POWER_15dBm); + WiFi.enableIpV6(); + WiFi.setAutoReconnect(true); + + wifiManager.setDebugOutput(true); + wifiManager.setConnectTimeout(60); + wifiManager.setConfigPortalTimeout(300); + if (!wifiManager.autoConnect("spaceapimap", "12345678")) { + ESP.restart(); + while (true); + } + ntp.begin(); // setup json filter @@ -81,7 +54,9 @@ void setup() { } void loop() { - connectAndWait(); + uint32_t color0 = pixels.getPixelColor(0); + pixels.setPixelColor(0, color0); + ntp.update(); Serial.println(ntp.formattedTime("\nIt is %d.%m.%Y %H:%M UTC")); time_t now = ntp.epoch();