From 7617c06363dc597d4c04d6ea17c59144934600d3 Mon Sep 17 00:00:00 2001
From: Francois <thiebolt@irit.fr>
Date: Sat, 27 May 2023 01:06:57 +0200
Subject: [PATCH] update

---
 ChangeLog.txt                                 |   4 +-
 .../libraries/neocampus/wifiParametersMgt.cpp |   8 +-
 neosensor/neosensor.ino                       |   3 +-
 tests/nvs_namespace/nvs_namespace.ino         | 124 ++++++++++++++++++
 4 files changed, 135 insertions(+), 4 deletions(-)
 create mode 100644 tests/nvs_namespace/nvs_namespace.ino

diff --git a/ChangeLog.txt b/ChangeLog.txt
index d842cd13..86e1fd09 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -6,6 +6,8 @@
 Bumps to esp32 2.0.9
 Bumps to esp8266 3.1.2
 - switch WiFi to both 802.11b AND 802.11g (DSI cut down 802.11b !!)
+- make use of NVS namespace to save WiFi crdentials (in order to avoid SPIFFS
+	partitionning issues)
 - recompiled lwip for NTP server support from DHCP answer (IDF 4.4.4)
 
 
@@ -14,7 +16,7 @@ Bumps to esp32 2.0.6
 Bumps to esp8266 3.1.0
 - force 802.11b for both ESP8266 and ESP32 --> solved our DHCP issues :D
 - WARNING: breaks SPIFFS format@ESP32 ==> reformat @ reboot ==> per device setup :'(
-	no SPIFFS format change @ ESP8266
+	(hopefully) no SPIFFS format change @ ESP8266
 - WiFiManager 2.0.15-rc1 no debug --> revert to 2.0.14-beta
 - newer WiFiManager introduces ASYNC SCAN ==> very slow (almost unanswered)
 [esp32]
diff --git a/neosensor/libraries/neocampus/wifiParametersMgt.cpp b/neosensor/libraries/neocampus/wifiParametersMgt.cpp
index a88e7546..794a07d4 100644
--- a/neosensor/libraries/neocampus/wifiParametersMgt.cpp
+++ b/neosensor/libraries/neocampus/wifiParametersMgt.cpp
@@ -29,8 +29,14 @@
   #include <WiFi.h>
 #endif /* ESP8266 */
 
-#include "Preferences.h"          // NVS storage (instead of the DEPRECATED eeprom)
+/* NVS namespace @ EPS32 */
+#ifdef ESP32
+  #include "Preferences.h"          // NVS storage (instead of the DEPRECATED eeprom)
+#elif defined (ESP8266)
+  #warning "[esp8266] no NVS namespace available ... grab WiFi ssid/psk from last connection"
+#endif
 
+/* neOCampus related includes */
 #include "neocampus.h"
 
 #include "neocampus_debug.h"
diff --git a/neosensor/neosensor.ino b/neosensor/neosensor.ino
index 1a2b64c9..e847bf34 100644
--- a/neosensor/neosensor.ino
+++ b/neosensor/neosensor.ino
@@ -26,8 +26,7 @@
  * - loadSensoConfig --> avoid data duplication, implement an iterator
  * - remove DISABLE_SSL compilation flag
  * ---
- * F.Thiebolt   jan.23  some cleanup about MAX_TCP connection that is defined at LWIP compile time
- *                      WiFi.setPhyMode(WIFI_PHY_MODE_11G) SOLVE THE looonnngggg DHCP issue
+ * ======= SEE 'ChangeLog.txt' for information about updates starting 2023 =========
  * F.Thiebolt   nov.21  corrected timezone definition for esp32
  * F.Thiebolt   sep.21  added display module support (e.g oled or 7segment displays)
  * F.Thiebolt   aug.21  added digital inputs support (e.g PIR sensor)
diff --git a/tests/nvs_namespace/nvs_namespace.ino b/tests/nvs_namespace/nvs_namespace.ino
new file mode 100644
index 00000000..03b6ed18
--- /dev/null
+++ b/tests/nvs_namespace/nvs_namespace.ino
@@ -0,0 +1,124 @@
+/*
+ * NVS namespace tests
+ * Note: no esp8266 NVS support ... instead grab WiFi ssid/psk from 
+ * 
+ * ---
+ * TODO:
+ * - add support for JSON structure to embedds several WiFi credentials
+ * ---
+ * F.Thiebolt may.23  initial release
+ */
+
+
+/*
+ * Includes
+ */
+#include <Arduino.h>
+
+#ifdef ESP8266
+  #warning "[esp8266] No NVS support, instead trying to read SSID/psk from last WiFi connection
+#elif defined (ESP32)
+  #include "Preferences.h"
+#endif
+
+
+/*
+ * Definitions
+*/
+
+// Debug related definitions
+#define SERIAL_BAUDRATE   115200
+
+// NVS namespace
+#define WIFI_NVS_NAMESPACE      "wifiCredentials"  // 15 chars max.
+#define WIFI_NVS_SSID_KEY       "ssid"
+#define WIFI_NVS_PASS_KEY       "pass"
+
+
+/*
+ * Global variables
+ */
+bool _need2reboot = false;              // flag to tell a reboot is requested
+
+char mySSID[64] = "";
+char myPSK[64]  = "";
+
+
+/*
+ * Functions
+*/
+
+void endLoop( void ) {
+  static unsigned long _lastCheck = 0;    // elapsed ms since last check
+
+  // check if a reboot has been requested ...
+  if( _need2reboot ) {
+    Serial.print(F("\n[REBOOT] a reboot has been asked ..."));Serial.flush();
+    delay(500);
+    ESP.restart();
+    delay(5000); // to avoid an infinite loop
+  }
+
+  // a second elapsed ?
+  if( ((millis() - _lastCheck) >= (unsigned long)1000UL) == true ) {
+    _lastCheck = millis();
+
+    // serial link activity marker ...
+    Serial.print(F("."));
+  }
+}
+
+
+// --- MAIN --------------------------------------------------------------------
+void setup() {
+  delay(5000);
+  Serial.begin(115200);   // debug link
+  Serial.println(F("\n\n\n[NVS] namespace demo ..."));Serial.flush();
+  delay(1000);
+
+#ifdef ESP32
+  /* NVS @ ESP32 */
+  Preferences nvs_wifi;
+  if( nvs_wifi.begin(WIFI_NVS_NAMESPACE,true) ) {  // readonly mode
+    Serial.print(F("\n[wifiParams] opened NVS WiFi credentials namespace ..."));Serial.flush();
+    // retrieve SSID/psk
+    nvs_wifi.getBytes(WIFI_NVS_SSID_KEY, mySSID, sizeof(mySSID)); 
+    nvs_wifi.getBytes(WIFI_NVS_PASS_KEY, myPSK, sizeof(myPSK)); 
+    if( strlen(mySSID) and strlen(myPSK) ) {
+      // using retrived credentials to connect
+      Serial.print(F("\n[NVS-WiFi] retreieved WiFi credentials from NVS :)"));Serial.flush();
+    }
+    else {
+      Serial.print(F("\n[NVS-WiFi] SSID and/or PSK missing ... cancel :("));Serial.flush();
+      mySSID[0] = '\0';
+      myPSK[0] = '\0';
+    }
+#elif defined (ESP8266)
+  /* ESP8266 retrieve WiFi credentials from previous connexion */
+
+  #error "NOT YET IMPLEMENTED
+  
+#endif
+  }
+  
+  // start WiFi connexion either with DEFAULTS WiFi credentials or from retrieved ones
+  //to be continued
+
+}
+
+
+// --- LOOP --------------------------------------------------------------------
+void loop() {
+
+
+  /*
+   * do something here ...
+   */
+
+
+  // fin de boucle
+  endLoop();
+
+  // do others stuff
+  delay(250);
+}
-- 
GitLab