From dd25b333d85dc3698c54707ff254bf53fb43f3e6 Mon Sep 17 00:00:00 2001
From: francois <francois@clever.amilab.irit.fr>
Date: Mon, 28 Mar 2022 17:58:25 +0200
Subject: [PATCH] save WiFi credentials typed by the user even if unable to
 connect to the GW

---
 ChangeLog.txt                                 |  6 ++
 ChangeLogHardware.txt                         |  7 +++
 neosensor/libraries/boards/neosensor.h        |  2 +-
 .../libraries/neocampus/neocampus_utils.cpp   | 13 +++--
 .../libraries/neocampus/wifiParametersMgt.cpp | 55 +++++++------------
 .../libraries/neocampus/wifiParametersMgt.h   |  3 +-
 6 files changed, 44 insertions(+), 42 deletions(-)
 create mode 100644 ChangeLog.txt
 create mode 100644 ChangeLogHardware.txt

diff --git a/ChangeLog.txt b/ChangeLog.txt
new file mode 100644
index 00000000..daf8bf8e
--- /dev/null
+++ b/ChangeLog.txt
@@ -0,0 +1,6 @@
+(c) F.Thiebolt Université Toulouse3, Laboratoire IRIT
+
+=== Release 220328
+- save WiFi credentials even if wrongly typed or end device has not been able
+to connect to WiFi gateway
+
diff --git a/ChangeLogHardware.txt b/ChangeLogHardware.txt
new file mode 100644
index 00000000..85c5b98b
--- /dev/null
+++ b/ChangeLogHardware.txt
@@ -0,0 +1,7 @@
+(c) F.Thiebolt Université Toulouse3, Laboratoire IRIT
+
+=== (upcoming) HW Release neOSensorV5.2
+- add 0805 cap for those ESP32 that miss it on EN pin
+- test DAC detect on ESP8266 (check no interference with SCD4x autodetect)
+- switch to ESP32 layout featuring unaligned pins
+
diff --git a/neosensor/libraries/boards/neosensor.h b/neosensor/libraries/boards/neosensor.h
index a18e7bc1..5516cc58 100644
--- a/neosensor/libraries/boards/neosensor.h
+++ b/neosensor/libraries/boards/neosensor.h
@@ -42,7 +42,7 @@
  */
 #define BOARD_NAME          "neOSensor"
 #define BOARD_REVISION      1.2
-#define BOARD_FWREV         220316          // Firmware revision <year><month><day> in 2 digits each
+#define BOARD_FWREV         220328          // Firmware revision <year><month><day> in 2 digits each
 
 
 /* #############################################################################
diff --git a/neosensor/libraries/neocampus/neocampus_utils.cpp b/neosensor/libraries/neocampus/neocampus_utils.cpp
index c23413af..5b33724d 100644
--- a/neosensor/libraries/neocampus/neocampus_utils.cpp
+++ b/neosensor/libraries/neocampus/neocampus_utils.cpp
@@ -308,11 +308,12 @@ bool setupWiFi( wifiParametersMgt *wp ) {
   // Getting posted form values and overriding local variables parameters
   // Config file is written regardless the connection state
   // TODO: find a way to parse ALL parameters added in wifi manager
+
+  // save wifi settings to wifi parameters object
+  wp->setWIFIsettings( wifiManager.getWiFiSSID().c_str(), wifiManager.getWiFiPass().c_str() );
+
   if( _WMsaveAddonConfigFlag ) {
     
-    // save wifi settings to wifi parameters object
-    wp->_getWIFIsettings();
-    
     // save OPTIONS to parameters object
     wp->_setopt_sandboxMode( strncmp(p_sandbox.getValue(),"T",1)==0 ? true : false );
     wp->_setopt_7segTM1637( strncmp(p_sevenSegTM1637.getValue(),"T",1)==0 ? true : false );
@@ -327,11 +328,11 @@ bool setupWiFi( wifiParametersMgt *wp ) {
     
     // Dangerous option !!
     wp->_setopt_eraseALL( strncmp(p_eraseALL.getValue(),"T",1)==0 ? true : false );
-    
-    // Writing JSON config file to flash for next boot
-    wp->saveConfigFile();
   }
 
+  // Writing JSON config file to flash for next boot ... if something has been updated :)
+  wp->saveConfigFile();
+
   if( WiFi.status() != WL_CONNECTED ) {
     log_error(F("\n[WiFi] failed to connect and hit timeout ... restart"));
     // reboot our sensor
diff --git a/neosensor/libraries/neocampus/wifiParametersMgt.cpp b/neosensor/libraries/neocampus/wifiParametersMgt.cpp
index 1f3b55ee..3158c4ec 100644
--- a/neosensor/libraries/neocampus/wifiParametersMgt.cpp
+++ b/neosensor/libraries/neocampus/wifiParametersMgt.cpp
@@ -201,46 +201,33 @@ bool wifiParametersMgt::saveConfigFile( void ) {
 
   
 /*
- * Grab WIFI settings from struct station_config
+ * Grab WIFI settings through Wifi global var or from parameters
+ *  and set within private attributes
  */
-bool wifiParametersMgt::_getWIFIsettings( void ) {
-  // grab WIFI station connexion parameters from current connexion ...
+bool wifiParametersMgt::setWIFIsettings( const char* ssid, const char* pass ) {
 
-  /*
-   * [sep.20] ESP8266 DEPRECATED CODE
-   *
-  struct station_config _conf;
-  if( wifi_station_get_config(&_conf) and strlen(reinterpret_cast<const char*>(_conf.ssid)) ) {
+  const char *tmpSSID = ssid;
+  const char *tmpPASS = pass;
+
+  if( !tmpSSID ) tmpSSID = WiFi.SSID().c_str();
+  log_debug(F("\n[wifiParams] retrieved current ssid = ")); log_debug(tmpSSID);
+
+  if( !tmpPASS ) tmpPASS = WiFi.psk().c_str();
+  log_debug(F("\n[wifiParams] retrieved current pass = ")); log_debug(tmpPASS);
+
+  log_flush();
   
-    log_debug(F("\n[wifiParams] retrieved current ssid = ")); log_debug(reinterpret_cast<const char*>(_conf.ssid));
-    log_debug(F("\n[wifiParams] retrieved current pass = ")); log_debug(reinterpret_cast<const char*>(_conf.password));
-    log_flush();
-    
-    if( strncmp(_ssid, reinterpret_cast<const char*>(_conf.ssid), sizeof(_ssid)) or
-        strncmp(_pass, reinterpret_cast<const char*>(_conf.password)), sizeof(_pass) ) {
-    
-      log_debug(F("\n[wifiParams] new credentials detected ... update!")); log_flush();
-      strncpy( _ssid, reinterpret_cast<const char*>(_conf.ssid), sizeof(_ssid) );
-      strncpy( _pass, reinterpret_cast<const char*>(_conf.password), sizeof(_pass) );
-      _updated = true;
-    }
-  }
-  else {
-    log_debug(F("\n[wifiParams] no SSID / PASS found neither in config file nor struct station ... probably first time connect ...")); log_flush();
+  if( !strlen(tmpSSID) ) {
+    log_debug(F("\n[wifiParams] empty SSID ...")); log_flush();
     return false;
   }
-   */
 
-  log_debug(F("\n[wifiParams] retrieved current ssid = ")); log_debug(WiFi.SSID());
-  log_debug(F("\n[wifiParams] retrieved current pass = ")); log_debug(WiFi.psk());
-  log_flush();
-  
-  if( strncmp(_ssid, WiFi.SSID().c_str(), sizeof(_ssid)) or
-      strncmp(_pass, WiFi.psk().c_str(), sizeof(_pass)) ) {
+  if( strncmp(_ssid, tmpSSID, sizeof(_ssid)) or
+      strncmp(_pass, tmpPASS, sizeof(_pass)) ) {
   
     log_debug(F("\n[wifiParams] new credentials detected ... update!")); log_flush();
-    strncpy( _ssid, WiFi.SSID().c_str(), sizeof(_ssid) );
-    strncpy( _pass, WiFi.psk().c_str(), sizeof(_pass) );
+    strncpy( _ssid, tmpSSID, sizeof(_ssid) );
+    strncpy( _pass, tmpPASS, sizeof(_pass) );
     _updated = true;
   }
   else {
@@ -503,8 +490,8 @@ for (JsonObject::iterator it=root.begin(); it!=root.end(); ++it) {
    */
   if( _wifiSet ) return true;
   
-  // grab from previous settings ...
-  _getWIFIsettings();
+  // grab WiFi from previous settings (Wifi global var)
+  setWIFIsettings();
   
   // over :)
   return true;
diff --git a/neosensor/libraries/neocampus/wifiParametersMgt.h b/neosensor/libraries/neocampus/wifiParametersMgt.h
index 5015e02b..7e0128c6 100644
--- a/neosensor/libraries/neocampus/wifiParametersMgt.h
+++ b/neosensor/libraries/neocampus/wifiParametersMgt.h
@@ -44,7 +44,8 @@ class wifiParametersMgt {
     
     const char * getWIFIssid( void );
     const char * getWIFIpass( void );
-    bool _getWIFIsettings( void );       // read struct station to extract WIFI parameters
+    bool setWIFIsettings( const char* ssid=nullptr, const char* pass=nullptr ); // extract WIFI parameters through Wifi global var
+                                                                                // or parameters and set our private attributes
     
     // wrapper for options querries
     bool isEnabledSandbox( void ) { return _opt_sandboxMode; };       // tell if neOCampus sandbox apply or if sensocampus is enabled (default)
-- 
GitLab