acid-drop

- Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.acid.vegas/-c.git
Log | Files | Refs | Archive | README | LICENSE

commit 5fe9e4d240e507dd2e8ebf7a9cf1b3e39629e96f
parent 20f3338187b1ac206f3c5fdf46842521201c4af8
Author: acidvegas <acid.vegas@acid.vegas>
Date: Mon, 27 May 2024 18:00:37 -0400

Added wifi credential caching (cheers sad)

Diffstat:
Msrc/main.ino | 43++++++++++++++++++++++++++++++++++++-------

1 file changed, 36 insertions(+), 7 deletions(-)

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