Skip to content

Fix wifi persistent #1404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 40 additions & 39 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,25 @@ static void _start_network_event_task(){
esp_event_loop_init(&_network_event_cb, NULL);
}

void tcpipInit(){
// -----------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------

typedef struct {
WiFiEventCb cb;
WiFiEventFullCb fcb;
WiFiEventSysCb scb;
system_event_id_t event;
} WiFiEventCbList_t;

// arduino dont like std::vectors move static here
static std::vector<WiFiEventCbList_t> cbEventList;

bool WiFiGenericClass::_esp_wifi_started = false;
bool WiFiGenericClass::_persistent = true;
wifi_mode_t WiFiGenericClass::_forceSleepLastMode = WIFI_MODE_NULL;

void WiFiGenericClass::tcpipInit(){
static bool initialized = false;
if(!initialized){
initialized = true;
Expand All @@ -108,32 +126,13 @@ void tcpipInit(){
}
}

static bool wifiLowLevelInit(){
static bool lowLevelInitDone = false;
if(!lowLevelInitDone){
tcpipInit();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_err_t err = esp_wifi_init(&cfg);
if(err){
log_e("esp_wifi_init %d", err);
return false;
}
esp_wifi_set_storage(WIFI_STORAGE_FLASH);
esp_wifi_set_mode(WIFI_MODE_NULL);
lowLevelInitDone = true;
}
return true;
}

static bool wifiLowLevelDeinit(){
bool WiFiGenericClass::wifiLowLevelDeinit(){
//deinit not working yet!
//esp_wifi_deinit();
return true;
}

static bool _esp_wifi_started = false;

static bool espWiFiStart(){
bool WiFiGenericClass::espWiFiStart(){
if(_esp_wifi_started){
return true;
}
Expand All @@ -150,7 +149,7 @@ static bool espWiFiStart(){
return true;
}

static bool espWiFiStop(){
bool WiFiGenericClass::espWiFiStop(){
esp_err_t err;
if(!_esp_wifi_started){
return true;
Expand All @@ -164,22 +163,24 @@ static bool espWiFiStop(){
return wifiLowLevelDeinit();
}

// -----------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------

typedef struct {
WiFiEventCb cb;
WiFiEventFullCb fcb;
WiFiEventSysCb scb;
system_event_id_t event;
} WiFiEventCbList_t;

// arduino dont like std::vectors move static here
static std::vector<WiFiEventCbList_t> cbEventList;

bool WiFiGenericClass::_persistent = true;
wifi_mode_t WiFiGenericClass::_forceSleepLastMode = WIFI_MODE_NULL;
bool WiFiGenericClass::wifiLowLevelInit(){
static bool lowLevelInitDone = false;
if(!lowLevelInitDone){
tcpipInit();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_err_t err = esp_wifi_init(&cfg);
if(err){
log_e("esp_wifi_init %d", err);
return false;
}
if (!_persistent) {
esp_wifi_set_storage(WIFI_STORAGE_RAM);
}
esp_wifi_set_mode(WIFI_MODE_NULL);
lowLevelInitDone = true;
}
return true;
}

WiFiGenericClass::WiFiGenericClass()
{
Expand Down
12 changes: 10 additions & 2 deletions libraries/WiFi/src/WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,26 @@ class WiFiGenericClass

void persistent(bool persistent);

static bool mode(wifi_mode_t);
static wifi_mode_t getMode();
bool mode(wifi_mode_t);
wifi_mode_t getMode();

bool enableSTA(bool enable);
bool enableAP(bool enable);

static esp_err_t _eventCallback(void *arg, system_event_t *event);

protected:

static bool _esp_wifi_started;
static bool _persistent;
static wifi_mode_t _forceSleepLastMode;

void tcpipInit();
bool espWiFiStart();
bool espWiFiStop();
bool wifiLowLevelInit();
bool wifiLowLevelDeinit();

public:

int hostByName(const char* aHostname, IPAddress& aResult);
Expand Down
2 changes: 1 addition & 1 deletion libraries/WiFi/src/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ bool WiFiSTAClass::getAutoReconnect()
uint8_t WiFiSTAClass::waitForConnectResult()
{
//1 and 3 have STA enabled
if((WiFiGenericClass::getMode() & WIFI_MODE_STA) == 0) {
if((WiFi.getMode() & WIFI_MODE_STA) == 0) {
return WL_DISCONNECTED;
}
int i = 0;
Expand Down