feat: replace hardcoded wifi credentials with wifimanager library

This commit is contained in:
s3lph 2024-09-20 01:48:19 +02:00
parent 4f3b07bc1f
commit b006fff4a3
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
2 changed files with 22 additions and 48 deletions

View file

@ -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}

View file

@ -1,5 +1,6 @@
#include <Arduino.h>
#include <WiFiManager.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h>
#include <NTP.h>
@ -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();