Skip to content

Commit 4a68612

Browse files
committed
add API for connect / reconnect management
1 parent d9a7a81 commit 4a68612

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

+42-4
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ bool ESP8266WiFiSTAClass::_useStaticIp = false;
9191
* @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal).
9292
* @param bssid uint8_t[6] Optional. BSSID / MAC of AP
9393
* @param channel Optional. Channel of AP
94+
* @param connect Optional. call connect
9495
* @return
9596
*/
96-
wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) {
97+
wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid, bool connect) {
9798

9899
if(!WiFi.enableSTA(true)) {
99100
// enable STA failed
@@ -134,12 +135,17 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
134135
}
135136

136137
ETS_UART_INTR_DISABLE();
138+
137139
if(WiFi._persistent) {
138140
wifi_station_set_config(&conf);
139141
} else {
140142
wifi_station_set_config_current(&conf);
141143
}
142-
wifi_station_connect();
144+
145+
if(connect) {
146+
wifi_station_connect();
147+
}
148+
143149
ETS_UART_INTR_ENABLE();
144150

145151
if(channel > 0 && channel <= 13) {
@@ -153,8 +159,8 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
153159
return status();
154160
}
155161

156-
wl_status_t ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) {
157-
return begin((const char*) ssid, (const char*) passphrase, channel, bssid);
162+
wl_status_t ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid, bool connect) {
163+
return begin((const char*) ssid, (const char*) passphrase, channel, bssid, connect);
158164
}
159165

160166
/**
@@ -261,6 +267,38 @@ bool ESP8266WiFiSTAClass::disconnect(bool wifioff) {
261267
return ret;
262268
}
263269

270+
/**
271+
* Setting the ESP8266 station to connect to the AP (which is recorded)
272+
* automatically or not when powered on. Enable auto-connect by default.
273+
* @param autoConnect bool
274+
* @return if saved
275+
*/
276+
bool ESP8266WiFiSTAClass::setAutoConnect(bool autoConnect) {
277+
bool ret;
278+
ETS_UART_INTR_DISABLE();
279+
ret = wifi_station_set_auto_connect(autoConnect);
280+
ETS_UART_INTR_ENABLE();
281+
return ret;
282+
}
283+
284+
/**
285+
* Checks if ESP8266 station mode will connect to AP
286+
* automatically or not when it is powered on.
287+
* @return auto connect
288+
*/
289+
bool ESP8266WiFiSTAClass::getAutoConnect() {
290+
return (wifi_station_get_auto_connect() != 0);
291+
}
292+
293+
/**
294+
* Set whether reconnect or not when the ESP8266 station is disconnected from AP.
295+
* @param autoReconnect
296+
* @return
297+
*/
298+
bool ESP8266WiFiSTAClass::setAutoReconnect(bool autoReconnect) {
299+
return wifi_station_set_reconnect_policy(autoReconnect);
300+
}
301+
264302
/**
265303
* Wait for WiFi connection to reach a result
266304
* returns the status reached or disconnect if STA is off

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ class ESP8266WiFiSTAClass {
3535

3636
public:
3737

38-
wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);
39-
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);
38+
wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
39+
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
4040
wl_status_t begin();
4141

4242
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
4343

4444
bool reconnect();
4545
bool disconnect(bool wifioff = false);
4646

47+
bool setAutoConnect(bool autoConnect);
48+
bool getAutoConnect();
49+
50+
bool setAutoReconnect(bool autoReconnect);
51+
4752
uint8_t waitForConnectResult();
4853

4954
// STA network info

0 commit comments

Comments
 (0)