Added wifi credential caching (cheers sad)

This commit is contained in:
Dionysus 2024-05-27 18:00:37 -04:00
parent 20f3338187
commit 5fe9e4d240
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE

View File

@ -1,3 +1,4 @@
#include <Preferences.h>
#include <TFT_eSPI.h> #include <TFT_eSPI.h>
#include <WiFi.h> #include <WiFi.h>
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
@ -74,6 +75,8 @@ struct WiFiNetwork {
std::vector<WiFiNetwork> wifiNetworks; std::vector<WiFiNetwork> wifiNetworks;
int selectedNetworkIndex = 0; int selectedNetworkIndex = 0;
Preferences preferences;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println("Booting device..."); Serial.println("Booting device...");
@ -92,19 +95,30 @@ void setup() {
Serial.println("TFT initialized"); Serial.println("TFT initialized");
displayXBM(); preferences.begin("wifi", false); // Initialize preferences with the namespace "wifi"
delay(3000); ssid = preferences.getString("ssid", "");
displayCenteredText("SCANNING WIFI"); password = preferences.getString("password", "");
delay(1000);
scanWiFiNetworks(); if (ssid.length() > 0 && password.length() > 0) {
displayWiFiNetworks(); connectToWiFi();
if (WiFi.status() != WL_CONNECTED) {
scanWiFiNetworks();
displayWiFiNetworks();
}
} else {
displayXBM();
delay(3000);
displayCenteredText("SCANNING WIFI");
delay(1000);
scanWiFiNetworks();
displayWiFiNetworks();
}
randomSeed(analogRead(0)); randomSeed(analogRead(0));
int randomNum = random(1000, 10000); int randomNum = random(1000, 10000);
nick = "ACID_" + String(randomNum); nick = "ACID_" + String(randomNum);
} }
int renderFormattedMessage(String message, int cursorY, int lineHeight, bool highlightNick = false) { int renderFormattedMessage(String message, int cursorY, int lineHeight, bool highlightNick = false) {
uint16_t fgColor = TFT_WHITE; uint16_t fgColor = TFT_WHITE;
uint16_t bgColor = TFT_BLACK; uint16_t bgColor = TFT_BLACK;
@ -475,12 +489,25 @@ void connectToWiFi() {
displayCenteredText("CONNECTED TO " + ssid); displayCenteredText("CONNECTED TO " + ssid);
delay(1000); delay(1000);
updateTimeFromNTP(); updateTimeFromNTP();
// Save WiFi credentials to preferences
preferences.putString("ssid", ssid);
preferences.putString("password", password);
} else { } else {
displayCenteredText("WIFI CONNECTION FAILED"); displayCenteredText("WIFI CONNECTION FAILED");
Serial.println("Failed to connect to WiFi."); Serial.println("Failed to connect to WiFi.");
// Clear stored credentials
preferences.remove("ssid");
preferences.remove("password");
// Display WiFi networks for selection
scanWiFiNetworks();
displayWiFiNetworks();
} }
} }
void sendIRC(String command) { void sendIRC(String command) {
if (client.println(command)) if (client.println(command))
Serial.println("IRC: >>> " + command); Serial.println("IRC: >>> " + command);
@ -924,11 +951,13 @@ void handleWiFiSelection(char key) {
} }
} }
} else { } else {
password = ""; // Open networks have no password
connectToWiFi(); connectToWiFi();
} }
} }
} }
void updateStatusBar() { void updateStatusBar() {
Serial.println("Updating status bar..."); Serial.println("Updating status bar...");
uint16_t darkerGrey = tft.color565(25, 25, 25); uint16_t darkerGrey = tft.color565(25, 25, 25);