Skip to content

Commit

Permalink
improved multiwifi fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lunokjod committed Aug 17, 2023
1 parent 9dc5238 commit b71340b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
29 changes: 14 additions & 15 deletions src/app/Provisioning2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const int16_t QRSize = 240;
#define PROV_QR_VERSION "v1"

//extern bool wifiOverride;
bool provisioned=false;
//bool provisioned=false;
extern TTGOClass *ttgo;

Provisioning2Application *lastProvisioning2Instance = nullptr;
Expand All @@ -85,11 +85,10 @@ void Provisioning2DestroyNVS() {
WiFi.mode(WIFI_OFF);
if ( ESP_OK == erased ) {
lLog("Provisioning: NVS: Provisioning data destroyed\n");
NVS.setInt("provisioned",0,false);
NVS.setInt("WiFiCredNo",0,false);
//NVS.setInt("provisioned",0,false);
//NVS.setInt("WiFiCredNo",0,false);
LoT().GetSettings()->SetInt(SystemSettings::SettingKey::WiFiCredentialsNumber,0);

provisioned=false;
//provisioned=false;
LaunchApplication(new ShutdownApplication(true,true));
}
}
Expand Down Expand Up @@ -127,36 +126,36 @@ void Provisioning2_SysProvEvent(arduino_event_t *sys_event) {
char passwd[BuffrSize+1];
snprintf(ssid,BuffrSize,"%s",sys_event->event_info.prov_cred_recv.ssid);
snprintf(passwd,BuffrSize,"%s",sys_event->event_info.prov_cred_recv.password);
lLog("Provisioning: Added '%s' pwd: '%s' offset: %u\n", ssid, passwd, LoT().GetWiFi()->GetConnections() );
lNetLog("WiFi: Provisioning: Added '%s' pwd: '%s' offset: %u\n", ssid, passwd, LoT().GetWiFi()->GetConnections() );
LoT().GetWiFi()->AddConnection(ssid,passwd);
//NVS.setString("provSSID",buffr,false);
//NVS.setString("provPWD",buffr,false);
break;
}
case ARDUINO_EVENT_PROV_CRED_FAIL: {
lLog("Provisioning: Failed, reason:\n");
lNetLog("WiFi: Provisioning: Failed, reason:\n");
if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) {
lLog("Wi-Fi AP password incorrect\n");
lNetLog("WiFi: AP password incorrect\n");
} else {
lLog("Wi-Fi AP not found\n");
lNetLog("WiFi: AP not found\n");
}
Provisioning2DestroyNVS();
NVS.setInt("provisioned",0,false);
provisioned = false;
//NVS.setInt("provisioned",0,false);
//provisioned = false;
Provisioning2Deinit();
break;
}
case ARDUINO_EVENT_PROV_CRED_SUCCESS:
lLog("Provisioning Successful\n");
lNetLog("WiFi: Provisioning Successful\n");
if ( nullptr != lastProvisioning2Instance ) {
lastProvisioning2Instance->provisioningStarted = false;
NVS.setInt("provisioned",1, false);
provisioned = true;
//NVS.setInt("provisioned",1, false);
//provisioned = true;
//LaunchApplication(new ShutdownApplication(true,true));
}
break;
case ARDUINO_EVENT_PROV_END:
lLog("Provisioning Ends\n");
lNetLog("WiFi: Provisioning Ends\n");
LaunchWatchface();
break;
default:
Expand Down
35 changes: 24 additions & 11 deletions src/system/Network/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

//void LoTWiFi::SuspendTasks() { xSemaphoreTake( taskLock, LUNOKIOT_EVENT_MANDATORY_TIME_TICKS); disabled=true; }
//void LoTWiFi::ResumeTasks() { xSemaphoreGive( taskLock ); disabled=false; }
extern bool provisioned;
//extern bool provisioned;

void LoTWiFi::Enable() {
xSemaphoreTake( taskLock, LUNOKIOT_EVENT_MANDATORY_TIME_TICKS);
Expand Down Expand Up @@ -120,15 +120,18 @@ void LoTWiFi::AddConnection(const char *SSID,const char *password) {
cred->Password=nPwd;
xSemaphoreTake( taskLock, LUNOKIOT_EVENT_MANDATORY_TIME_TICKS);
credentials.push_back(cred);
LoT().GetSettings()->SetInt(SystemSettings::SettingKey::WiFiCredentialsNumber,int(GetConnections()));
lNetLog("WiFi: %p Added connection %u SSID: '%s' Password: '%s'\n", this, GetConnections()-1, nSSID, nPwd);
NVS.setInt(String(SystemSettings::KeysAsString[SystemSettings::SettingKey::WiFiCredentialsNumber]),int(GetConnections()),true);
//LoT().GetSettings()->SetInt(SystemSettings::SettingKey::WiFiCredentialsNumber,int(GetConnections()));
lNetLog("WiFi: %p Added connection %u SSID: '%s' Password: '%s'\n", this, GetConnections(), nSSID, nPwd);
char labelName[255];
sprintf(labelName,"SSID_%u",GetConnections()-1);
NVS.setString(String(labelName),String(nSSID),false);
sprintf(labelName,"WIFIPWD_%u",GetConnections()-1);
NVS.setString(String(labelName),String(nPwd),false);
sprintf(labelName,"SSID_%u",GetConnections());
NVS.setString(String(labelName),String(nSSID));
sprintf(labelName,"WIFIPWD_%u",GetConnections());
NVS.setString(String(labelName),String(nPwd));
wifiMulti.addAP(nSSID,nPwd);
provisioned=true;
//provisioned=true;
int shit = LoT().GetSettings()->GetInt(SystemSettings::SettingKey::WiFiCredentialsNumber);
lLog("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA: %d\n",shit);
xSemaphoreGive( taskLock );
}

Expand Down Expand Up @@ -277,15 +280,25 @@ LoTWiFi::LoTWiFi() {
if ( canUse ) { Enable(); }
else { Disable(); }
int maxWifis = LoT().GetSettings()->GetInt(SystemSettings::SettingKey::WiFiCredentialsNumber);
/*
if ( -1 == maxWifis ) {
lNetLog("WiFi: not provisioned!\n");
return;
}*/
if ( 0 == maxWifis ) {
lNetLog("WiFi: not provisioned!\n");
return;
}
size_t currentWiFiOffset=0;
while ( currentWiFiOffset < maxWifis ) {
while ( currentWiFiOffset <= maxWifis ) {
char labelName[255];
sprintf(labelName,"SSID_%u",currentWiFiOffset);
String nSSID = NVS.getString(String(labelName));
sprintf(labelName,"WIFIPWD_%u",currentWiFiOffset);
String nPwd = NVS.getString(String(labelName));
//lNetLog("WiFi: %p Add connection: %u SSID: '%s' Pwd: '%s'\n", this, currentWiFiOffset,nSSID.c_str(),nPwd.c_str());
AddConnection(nSSID.c_str(),nPwd.c_str());
lNetLog("WiFi: %p Add connection: %u SSID: '%s' Pwd: '%s'\n", this, currentWiFiOffset,nSSID.c_str(),nPwd.c_str());
wifiMulti.addAP(nSSID.c_str(),nPwd.c_str());
//AddConnection(nSSID.c_str(),nPwd.c_str());
currentWiFiOffset++;
}
InstallTimer();
Expand Down
4 changes: 3 additions & 1 deletion src/system/Storage/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ SystemSettings::~SystemSettings() {

int64_t SystemSettings::GetInt(SettingKey what) {
int64_t result = NVS.getInt(KeysAsString[what]);
//lSysLog("SystemSettings %p: Get '%s' as: (signed: %d) (unsigned: %u)\n", this,KeysAsString[what],result,result);
lSysLog("SystemSettings %p: Get '%s' as: (signed: %d) (unsigned: %u)\n", this,KeysAsString[what],result,result);
return result;
}

bool SystemSettings::SetInt(SettingKey what, int32_t value) {
lLog("SystemSettings: DEBUG INT32\n");
bool result = NVS.setInt(String(KeysAsString[what]),value,false);
if (false == result) {
lSysLog("SystemSettings %p: ERROR: Unable to set key '%s'\n", this,KeysAsString[what]);
Expand All @@ -57,6 +58,7 @@ bool SystemSettings::SetInt(SettingKey what, int32_t value) {
}

bool SystemSettings::SetInt(SettingKey what, uint8_t value) {
lLog("SystemSettings: DEBUG UINT8\n");
bool result = NVS.setInt(String(KeysAsString[what]),value,false);
if (false == result) {
lSysLog("SystemSettings %p: ERROR: Unable to set key '%s'\n", this,KeysAsString[what]);
Expand Down
2 changes: 1 addition & 1 deletion src/system/SystemEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ extern bool provisioned;
extern void StopDatabase();

void SaveDataBeforeShutdown() {
NVS.setInt("provisioned", provisioned, false);
//NVS.setInt("provisioned", provisioned, false);

NVS.setInt("accXMax", accXMax, false);
NVS.setInt("accXMin", accXMin, false);
Expand Down
2 changes: 1 addition & 1 deletion tool/.buildCount.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26416
26477

0 comments on commit b71340b

Please sign in to comment.