feat: replace hardcoded wifi credentials with wifimanager library
This commit is contained in:
parent
4f3b07bc1f
commit
b006fff4a3
2 changed files with 22 additions and 48 deletions
|
@ -16,8 +16,7 @@ lib_deps =
|
||||||
adafruit/Adafruit NeoPixel@^1.12.3
|
adafruit/Adafruit NeoPixel@^1.12.3
|
||||||
bblanchon/ArduinoJson@^7.1.0
|
bblanchon/ArduinoJson@^7.1.0
|
||||||
sstaub/NTP@^1.6
|
sstaub/NTP@^1.6
|
||||||
|
tzapu/WiFiManager@^2.0.17
|
||||||
build_flags =
|
build_flags =
|
||||||
-D ARDUINO_USB_MODE=1
|
-D ARDUINO_USB_MODE=1
|
||||||
-D ARDUINO_USB_CDC_ON_BOOT=1
|
-D ARDUINO_USB_CDC_ON_BOOT=1
|
||||||
-D WPA2_SSID=${sysenv.ESP32_WPA2_SSID}
|
|
||||||
-D WPA2_PSK=${sysenv.ESP32_WPA2_PSK}
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <WiFiManager.h>
|
||||||
#include <WiFiClientSecure.h>
|
#include <WiFiClientSecure.h>
|
||||||
#include <WiFiUdp.h>
|
#include <WiFiUdp.h>
|
||||||
#include <NTP.h>
|
#include <NTP.h>
|
||||||
|
@ -8,14 +9,6 @@
|
||||||
|
|
||||||
#include "spacemap.h"
|
#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_PIN 0
|
||||||
#define WS2812_LEN (sizeof(spaces)/sizeof(char*))
|
#define WS2812_LEN (sizeof(spaces)/sizeof(char*))
|
||||||
#define WS2812_BS 5
|
#define WS2812_BS 5
|
||||||
|
@ -24,6 +17,7 @@
|
||||||
#define SPACEAPI_PATH "/"
|
#define SPACEAPI_PATH "/"
|
||||||
|
|
||||||
|
|
||||||
|
WiFiManager wifiManager;
|
||||||
WiFiClientSecure client;
|
WiFiClientSecure client;
|
||||||
WiFiUDP wifiUdp;
|
WiFiUDP wifiUdp;
|
||||||
NTP ntp(wifiUdp);
|
NTP ntp(wifiUdp);
|
||||||
|
@ -31,46 +25,25 @@ NTP ntp(wifiUdp);
|
||||||
Adafruit_NeoPixel pixels(WS2812_LEN, WS2812_PIN, NEO_RGB | NEO_KHZ800);
|
Adafruit_NeoPixel pixels(WS2812_LEN, WS2812_PIN, NEO_RGB | NEO_KHZ800);
|
||||||
JsonDocument json, filter;
|
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() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
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();
|
ntp.begin();
|
||||||
|
|
||||||
// setup json filter
|
// setup json filter
|
||||||
|
@ -81,7 +54,9 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
connectAndWait();
|
uint32_t color0 = pixels.getPixelColor(0);
|
||||||
|
pixels.setPixelColor(0, color0);
|
||||||
|
|
||||||
ntp.update();
|
ntp.update();
|
||||||
Serial.println(ntp.formattedTime("\nIt is %d.%m.%Y %H:%M UTC"));
|
Serial.println(ntp.formattedTime("\nIt is %d.%m.%Y %H:%M UTC"));
|
||||||
time_t now = ntp.epoch();
|
time_t now = ntp.epoch();
|
||||||
|
|
Loading…
Reference in a new issue